0
当我运行的人脸检测代码,我现在面临以下错误:学习Python和OpenCV的
Traceback (most recent call last):
File "E:\python program\s1.py", line 11, in <module> ret, frame=cap.read()
AttributeError: 'numpy.ndarray' object has no attribute 'read'
当我运行的代码最有可能具有ret, frame=cap.read()
这种类型的代码,我得到的错误。
请提供解决方案。
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('..\haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(0)
scaling_factor=0.5
while True:
ret, frame=cap.read()
frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor,
interpolation=cv2.INTER_AREA)
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_rects=face_cascade.detectMultiScale(gray, 1.3,5)
for(x,y,w,h) in face_rects:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0),3)
cv2.imshow('Face Detector', frame)
c=cv2.waitKey(1)
if c==27:
break
cap.release()
cv2.destroyAllWindows()
欢迎计算器,请加以修改问题和修复缩进;它不可能对你的问题产生影响,但它在Python中很重要,正如你所知道的...并且在这里给网站的常客 – alexis
请阅读[“当有人回答我的问题时该怎么办? ?“](http://stackoverflow.com/help/someone-answers)。 – thewaywewere