face_search.py/face_search_cam.py

36 lines
1.1 KiB
Python
Raw 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
# Загрузка модели HOG + SVM
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Функция для поиска лиц на изображении
def find_faces(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Рисование прямоугольников вокруг обнаруженных лиц
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
return image
# Захват видео с веб-камеры
cap = cv2.VideoCapture(0)
while(True):
# Чтение кадра с камеры
ret, frame = cap.read()
# Поиск лиц на кадре
detected_frame = find_faces(frame)
# Отображение результата
cv2.imshow('Detected Faces', detected_frame)
# Прерывание цикла при нажатии клавиши 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Освобождение ресурсов
cap.release()
cv2.destroyAllWindows()