This repository has been archived on 2022-12-09. You can view files and clone it, but cannot push or open issues or pull requests.

44 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
# Загружаем обученные модели
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0) # Открываем видеопоток с камеры
cap.set(cv2.CAP_PROP_FPS, 60) # Частота кадров
while True:
ret, video = cap.read() # Считываем видео
gray = cv2.cvtColor(video, cv2.COLOR_BGR2GRAY) # Красим все, так удобнее
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(20, 20)
)
for (x, y, w, h) in faces:
roi_gray = gray[y:y + h, x:x + w] # Вырезаем область с лицами
roi_color = video[y:y + h, x:x + w]
cv2.rectangle(video, (x, y), (x + w, y + h), (255, 0, 0), 2) #
eyes = eyeCascade.detectMultiScale(
roi_gray,
scaleFactor = 1.2,
minNeighbors = 4,
minSize = (5, 5),
)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow("output", video) # Выводим видео в окно
if cv2.waitKey(10) == 27: # Выходим по кнопке Esc
break
cap.release()
cv2.destroyAllWindows()