snipplets.dev/projects/CV/DetectFaces/__main__.py

34 lines
879 B
Python
Raw Permalink Normal View History

2023-09-29 21:51:53 +03:00
import cv2
image_path = './Assets/girl_faces.png'
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def viewImage(image, name_of_window):
cv2.namedWindow(name_of_window, cv2.WINDOW_NORMAL)
cv2.imshow(name_of_window, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(10, 10),
)
faces_detected_string = 'Лиц обнаружено: ' + format(len(faces))
print(faces_detected_string)
# Рисуем квадраты вокруг лиц
for x, y, w, h in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
viewImage(image, faces_detected_string)