python3.6 open3.4 face location case

python3.6 open3.4 face recognition. After a long time, I recorded the pits I walked through.
Can detect faces, as well as eyes.
First effect:

code show as below:

import cv2
import sys
import os
# Get user supplied values
imagePath = './heying5.jpg'#sys.argv[1]
# Create the haar cascade

faceCascade=cv2.CascadeClassifier('D:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')#
eyeCascade = cv2.CascadeClassifier("D:\opencv\sources\data\haarcascades\haarcascade_eye.xml")
if 
# Read the image
image = cv2.imread(imagePath)#2
# cv2.imshow("frame" , image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#3
# cv2.imshow("frame" , gray)
# Detect faces in the image
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.15,minNeighbors=5,minSize=(5,5))
    #flags=cv2.CASCADE_SCALE_IMAGE

#faceCascade.detectMultiScale(gray, scaleFactor = 1.3, minNeighbors = 4, minSize = (32, 32))
print("Found {0} faces!".format(len(faces)))#5
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) #6
    roi_gray_img = gray[y:y + h, x:x + w]
    roi_img = image[y:y + h, x:x + w]
    eyes = eyeCascade.detectMultiScale(roi_gray_img, 1.3, 5)
    for eye_x, eye_y, eye_w, eye_h in eyes:
        cv2.rectangle(roi_img, (eye_x, eye_y), (eye_x + eye_w, eye_y + eye_h), (255, 0, 0), 2)

cv2.imshow("Faces found", image)#7
cv2.waitKey(0) #8


Note:
Pit 1:

faceCascade=cv2.CascadeClassifier('D:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')

Many articles are written directly as:

faceCascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

If the code runs incorrectly and the haarcascade_frontalface_default.xml file cannot be found, you must install the opencv exe compressed package separately. After decompression, there will be this xml file in it, and then fill in the full address here.

Pit 2:
Many blogs are based on the opencv2.x version, and my environment is opencv3.4. In 3.4, cv2.cv is gone.... In general, simply change cv2.cv.xxx to cv2.xxx. If it doesn't work, Baidu will see if it can be replaced with something else.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324432166&siteId=291194637