CodeLibrary/03_pycharm_python_coursework_20240427/opencv_04.py
JRNitre 2092240e04 计算机专业前沿作业 Python
Date: 20240427
Design by JRNitre
2024-04-27 20:40:24 +08:00

17 lines
551 B
Python

import cv2
img = cv2.imread("source/data_02.jpg")
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.09, minNeighbors=3, minSize=(5, 5))
print(faces)
print("该图一共有{0}人脸".format(len(faces)))
for(x, y, w, h) in faces:
cv2.circle(img, (int((2 * x + w) / 2), int((2 * y + h) / 2)),
int(w / 2), (0, 255, 0), 2)
cv2.imshow("result", img)
cv2.waitKey()
cv2.destroyWindow()