2016-03-28 72 views
0

我有從GitHub下載的代碼用於OpenCV項目。一切都工作得很好的第一次,但之後也不會公開,並不斷顯示我下面的錯誤在第8行:無法在python中打開VideoCapture

Traceback (most recent call last): 
    File "hand.py", line 8, in <module> 
    crop_img = img[100:300,100:300] 
TypeError: 'NoneType' object is not subscriptable 

下面是代碼:

import cv2 
import numpy as np 
import math 
cap = cv2.VideoCapture(0) 
while(cap.isOpened()): 
    ret, img = cap.read() 
    cv2.rectangle(img,(300,300),(100,100),(0,255,0),0) 
    crop_img = img[100:300,100:300] 
    grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) 
    value = (35, 35) 
    blurred = cv2.GaussianBlur(grey, value, 0) 
    _, thresh1 = cv2.threshold(blurred, 127, 255, 
           cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 
    cv2.imshow('Thresholded', thresh1) 
    contours, hierarchy = cv2.findContours(thresh1.copy(),cv2.RETR_TREE, \ 
      cv2.CHAIN_APPROX_NONE) 
    max_area = -1 
    for i in range(len(contours)): 
     cnt=contours[i] 
     area = cv2.contourArea(cnt) 
     if(area>max_area): 
      max_area=area 
      ci=i 
    cnt=contours[ci] 
    x,y,w,h = cv2.boundingRect(cnt) 
    cv2.rectangle(crop_img,(x,y),(x+w,y+h),(0,0,255),0) 
    hull = cv2.convexHull(cnt) 
    drawing = np.zeros(crop_img.shape,np.uint8) 
    cv2.drawContours(drawing,[cnt],0,(0,255,0),0) 
    cv2.drawContours(drawing,[hull],0,(0,0,255),0) 
    hull = cv2.convexHull(cnt,returnPoints = False) 
    defects = cv2.convexityDefects(cnt,hull) 
    count_defects = 0 
    cv2.drawContours(thresh1, contours, -1, (0,255,0), 3) 
    for i in range(defects.shape[0]): 
     s,e,f,d = defects[i,0] 
     start = tuple(cnt[s][0]) 
     end = tuple(cnt[e][0]) 
     far = tuple(cnt[f][0]) 
     a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) 
     b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) 
     c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) 
     angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57 
     if angle <= 90: 
      count_defects += 1 
      cv2.circle(crop_img,far,1,[0,0,255],-1) 
     #dist = cv2.pointPolygonTest(cnt,far,True) 
     cv2.line(crop_img,start,end,[0,255,0],2) 
     #cv2.circle(crop_img,far,5,[0,0,255],-1) 
    if count_defects == 1: 
     cv2.putText(img,"this is 2", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) 
    elif count_defects == 2: 
     str = "this is 3 !!!" 
     cv2.putText(img, str, (5,50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2) 
    elif count_defects == 3: 
     cv2.putText(img,"This is 4 :P", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) 
    elif count_defects == 4: 
     cv2.putText(img,"this is 5 !!!", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) 
    else: 
     cv2.putText(img,"this is 0 !!!", (50,50),\ 
        cv2.FONT_HERSHEY_SIMPLEX, 2, 2) 
    #cv2.imshow('drawing', drawing) 
    #cv2.imshow('end', crop_img) 
    cv2.imshow('Gesture', img) 
    all_img = np.hstack((drawing, crop_img)) 
    cv2.imshow('Contours', all_img) 
    k = cv2.waitKey(10) 
    if k == 27: 
     break 
+0

歡迎來到Stack Overflow!爲了讓人們回答你的問題,你能否提供[最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)?這將允許人們回答,而無需通過您提供的所有代碼(您可以在解決問題的同時解決您的問題)。 –

+0

沒有工作,它的工作有時和somtimes沒有!爲什麼?我使用版本2.4.9 – Walid

回答

1

我找到了解決方案,它似乎有些蹩腳的視頻驅動程序返回無效的第一幀。我所做的只是檢查ret並繼續,如果它是假的,它工作正常。

0

的異常指示cap.read返回Noneimg。你應該注意的事項是:

cap = cv2.VideoCapture(0) 
while(cap.isOpened()): 
    ret, img = cap.read() 
    if not ret: break 

的單證說:

如果沒有框架已經搶下(相機已斷開,或者有視頻文件沒有更多的幀),該方法返回假並且函數返回NULL指針。

因此,如果您使用USB網絡攝像頭,請確保您的USB連接穩定。特別是,如果您使用任何USB集線器/擴展或您的端口狀態不佳。

+0

我使用我的筆記本電腦攝像頭,但爲什麼代碼有時會正確執行,有時會顯示此錯誤? – Walid