OpenCV错误:断言失败:无法使视频源正常工作

时间:2018-04-01 12:36:43

标签: python opencv

我正在尝试使用OpenCV进行手势识别的相机输入。我有模型训练我只需要让相机工作,我应该能够识别手语。下面是完整的错误:

OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in cv::Mat::locateROI, file C:\ci\opencv_1512688052760\work\modules\core\src\matrix.cpp, line 991
Traceback (most recent call last):
  File "ASL.py", line 28, in <module>
    blur = cv2.GaussianBlur(grey, value, 0)
cv2.error: C:\ci\opencv_1512688052760\work\modules\core\src\matrix.cpp:991: error: (-215) dims <= 2 && step[0] > 0 in function cv::Mat::locateROI

我的代码:

while(cap.isOpened()):
    print("while entered")
    _,img=cap.read()
    cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3) # bounding box which captures ASL sign to be detected by the system
    crop_img = img[100:500,900:1300]
    # convert to grayscale
    grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
    # applying gaussian blur
    value = (11, 11)
    blur = cv2.GaussianBlur(grey, value, 0)
    skin_ycrcb_min = np.array((0, 138, 67))
    skin_ycrcb_max = np.array((255, 173, 133))
    mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max)  # detecting the hand in the bounding box using skin detection
    contours,hierarchy = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL, 2) 
    cnt=ut.getMaxContour(contours,4000)             
    if cnt!=None:
        gesture,label=ut.getGestureImg(cnt,img1,mask,model)   # passing the trained model for prediction and fetching the result
        if(label!=None):
            if(temp==0):
                previouslabel=label
            elif previouslabel==label:
                previouslabel=label
                temp+=1
            else:
                temp=0
            if(temp==40):
                if(label=='P'):
                    label=" "
                    text= text + label
                if(label=='Q'):
                    words = re.split(" +",text)
                    words.pop()
                    text = " ".join(words)
                    #text=previousText
            print(text)

    cv2.imshow('PredictedGesture',gesture)                # showing the best match or prediction
    cv2.putText(img,label,(50,150), font,8,(0,125,155),2)  # displaying the predicted letter on the main screen
    cv2.putText(img,text,(50,450), font,3,(0,0,255),2)
    cv2.imshow('Frame',img)
    cv2.imshow('Mask',mask)
    k = 0xFF & cv2.waitKey(10)
    if k == 27:
        break


cap.release()        
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

几个月前我遇到了同样的问题。您所要做的就是确保您的“灰色”变量不是无。

将此代码放在模糊之前:

if grey is not None:
    blur = cv2.GaussianBlur(grey, value, 0)

保留其他所有内容。 希望这有帮助!