2015-12-31 105 views
0

我已經在Spyder Python 2.7上安裝了Opencv 3.1.4,它們都運行在Windows Vista 32位上。錯誤Spyder Python + opencv 3

我的代碼是

import cv2 

import sys 

cascPath = "C:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml" 
faceCascade = cv2.CascadeClassifier(cascPath) 

video_capture = cv2.VideoCapture(0) 

while True: 
    # Capture frame-by-frame 
    ret, frame = video_capture.read() 

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

    faces = faceCascade.detectMultiScale(
     gray, 
     scaleFactor=1.1, 
     minNeighbors=5, 
     minSize=(30, 30), 
     flags=cv2.cv.CV_HAAR_DO_CANNY_PRUNING 
    ) 

    # Draw a rectangle around the faces 
    for (x, y, w, h) in faces: 
     cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) 

    # Display the resulting frame 
    cv2.imshow('Video', frame) 
    print faces 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

# When everything is done, release the capture 
video_capture.release() 
cv2.destroyAllWindows() 

但我有這個錯誤

runfile('C:/Anaconda2/Scripts/tracking-video-fonctionnel.py', wdir='C:/Anaconda2/Scripts') 
 
Traceback (most recent call last): 
 

 
    File "<ipython-input-2-3b1671aa3a09>", line 1, in <module> 
 
    runfile('C:/Anaconda2/Scripts/tracking-video-fonctionnel.py', wdir='C:/Anaconda2/Scripts') 
 

 
    File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile 
 
    execfile(filename, namespace) 
 

 
    File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile 
 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 
 

 
    File "C:/Anaconda2/Scripts/tracking-video-fonctionnel.py", line 20, in <module> 
 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
 

 
error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function cv::ipp_cvtColor

然而,這段代碼的其他計算機上運行使用相同的Spyder和OpenCV但在Windows 7 64位上

我認爲這個問題是因爲OpenCV的我無法導入cv2.cv但我可以導入CV2

感謝的

回答

0

我已經找到了解決辦法,這是要安裝OpenCV

的2.x版
+0

版本3.1應該可以工作。在你的代碼中,我只能看到需要更改的flags屬性。 – ibininja

0

cv2.cv在opencv3.0 +中不存在。你可以用索引來代替它:試試這個:

flags=3 

無論如何,我懷疑這是錯誤顯示的主要原因。根據你發佈的錯誤,它沒有達到該線。我懷疑別的東西。可能相機源/幀有問題,COLOR_無法將其轉換。換句話說,幀沒有正確加載。嘗試使用更好的相機或在光線良好的環境中使用。最好還是用圖像而不是視頻來試試看。