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