本文主要是介绍关于detectMultiScale返回值为空元组的解决办法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
关于detectMultiScale返回值为元组的解决办法
- 1 网上纠错
1 网上纠错
完整代码:
import os
import cv2def face_dateset():cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)cam.set(3, 640) # set video widthcam.set(4, 480) # set video heighti = 0face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')# For each person, enter one numeric face idface_id = input('\n enter user id end press <return> ==> ')#face_id = 10print("\n [INFO] Initializing face capture. Look the camera and wait ...")# Initialize individual sampling face countcount = 0files = './dataset/'+str(face_id)folder = os.path.exists(files)if not folder:os.makedirs(files)os.chmod(files, 0o777)while (True):ret, img = cam.read()img = cv2.flip(img, 1) # flip video image verticallygray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = face_detector.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(20, 20))if len(faces)>0:for (x, y, w, h) in faces:cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)count += 1# Save the captured image into the datasets foldercv2.imwrite(files + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w])cv2.imshow('image', img)k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting videoif k == 27:breakelif count >= 30: # Take 30 face sample and stop videobreak# Do a bit of cleanupprint("\n [INFO] Exiting Program and cleanup stuff")cam.release()cv2.destroyAllWindows()return face_id
找了很多网上教程,说什么face_detector = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’) 没有正确调用。
最后发现问题是:
cv2.imshow('image', img)
需要把这个写在循环if len(faces)>0:的外面,当启动这个界面后,需要把人脸放到摄像头下面,当检测到人脸后,detectMultiScale的返回元组就不为空咯
这篇关于关于detectMultiScale返回值为空元组的解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!