2017-05-28 40 views
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() 
+2

歡迎計算器,請加以修改問題和修復縮進;它不可能對你的問題產生影響,但它在Python中很重要,正如你所知道的...並且在這裏給網站的常客 – alexis

+0

請閱讀[「當有人回答我的問題時該怎麼辦? ?「](http://stackoverflow.com/help/someone-answers)。 – thewaywewere

回答

0

您是否驗證了您的攝像頭的工作原理:

我的全代碼這個代碼,我只從網站採取了?錯誤告知cv2.VideoCapture(0)對象尚未創建爲cap,因此它不是cap.read()屬性。您可以設置網絡攝像頭號碼。到cv2.VideoCapture(-1)進行試用,如果您的攝像頭與其他應用程序一起使用。

除了錯誤,face_cascade數據路徑應在斜槓被指定爲「/」作爲(../haarcascade_frontalface_default.xml)或雙反斜線「\\(..\\haarcascade_frontalface_default.xml)Windows OS而不是反斜槓」 \」。