face_search.py/haarcascade.py

23 lines
865 B
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
# Загрузка изображения
image = cv2.imread('image.jpg')
print(image)
# Преобразование изображения в оттенки серого
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Загрузка каскадного классификатора для поиска лиц
face_cascade = cv2.CascadeClassifier(r'haarcascade_frontalface_default.xml')
print(face_cascade)
# Обнаружение лиц на изображении
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Рисование прямоугольников вокруг найденных лиц
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Отображение результата
cv2.imshow('Результат', image)
cv2.waitKey(0)
cv2.destroyAllWindows()