Veb-kamera yordamida Python-da yuzni aniqlash
import cv2
import sys
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Endi uni parchalab olaylik...
import cv2
import sys
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)
Bu sizga tanish bo'lishi kerak. Biz tasvir misolida qilganimizdek, yuz kaskadini yaratmoqdamiz.
video_capture = cv2.VideoCapture(0)
Ushbu qator video manbasini standart veb-kameraga o'rnatadi, OpenCV uni osongina suratga oladi.
QAYD : Bu yerda fayl nomini ham ko'rsatishingiz mumkin va Python video faylda o'qiydi. Biroq, buning uchun sizda ffmpeg o'rnatilgan bo'lishi kerak, chunki OpenCV o'zi siqilgan videoni dekodlay olmaydi. Ffmpeg OpenCV uchun old qism vazifasini bajaradi va ideal holda u to'g'ridan-to'g'ri OpenCVga kompilyatsiya qilinishi kerak. Buni qilish oson emas, ayniqsa Windows-da.
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
Mana, biz videoni suratga olamiz. Funktsiya read()video manbasidan bitta kadrni o'qiydi, bu misolda veb-kamera. Bu qaytaradi:
Haqiqiy o'qilgan video ramka (har bir tsiklda bitta kadr)
Qaytish kodi
Qaytish kodi, agar biz fayldan o'qiyotgan bo'lsak, bu sodir bo'ladigan ramkalar tugab qolganligini bildiradi. Veb-kameradan o'qishda bu muhim emas, chunki biz abadiy yozib olishimiz mumkin, shuning uchun biz buni e'tiborsiz qoldiramiz.
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
Shunga qaramay, bu kod tanish bo'lishi kerak. Biz shunchaki suratga olingan kadrimizdagi yuzni qidirmoqdamiz.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Biz "q" tugmachasini bosishini kutamiz. Agar shunday bo'lsa, biz skriptdan chiqamiz.
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Mana, biz shunchaki tozalayapmiz.
Do'stlaringiz bilan baham: |