Python文件未在Linux中运行-OpenCV isOpened()问题

Python文件未在Linux中运行-OpenCV isOpened()问题,python,linux,Python,Linux,我试图在Ubuntu中运行python文件。但它没有运行,我也没有收到任何错误消息。虽然loop没有在Linux中运行,但它在windows中工作 #!/usr/bin/env python 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

我试图在Ubuntu中运行python文件。但它没有运行,我也没有收到任何错误消息。虽然loop没有在Linux中运行,但它在windows中工作

#!/usr/bin/env python
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)

    (version, _, _) = cv2.__version__.split('.')

    if version is '3':
        image, contours, hierarchy = cv2.findContours(thresh1.copy(), \
               cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    elif version is '2':
        contours, hierarchy = cv2.findContours(thresh1.copy(),cv2.RETR_TREE, \
           cv2.CHAIN_APPROX_NONE)

    cnt = max(contours, key = lambda x: cv2.contourArea(x))

    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,"I am Vipul", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2)
    elif count_defects == 2:
        str = "This is a basic hand gesture recognizer"
        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,"Hi!!!", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2)
    else:
        cv2.putText(img,"Hello World!!!", (50,50),\
                cv2.FONT_HERSHEY_SIMPLEX, 2, 2)
    cv2.imshow('Gesture', img)
    all_img = np.hstack((drawing, crop_img))
    cv2.imshow('Contours', all_img)
    k = cv2.waitKey(10)
    if k == 27:
        break

kathir@kathir-VirtualBox:~/Desktop/Handgesture$ python BasicHandgesture.py
kathir@kathir-VirtualBox:~/Desktop/Handgesture$ python 
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cv2
>>> 
KeyboardInterrupt
>>> 
kathir@kathir-VirtualBox:~/Desktop/Handgesture$ python BasicHandgesture.py
kathir@kathir-VirtualBox:~/Desktop/Handgesture$ 
#/usr/bin/env python
进口cv2
将numpy作为np导入
输入数学
cap=cv2.视频捕获(0)
while(cap.isOpened()):
ret,img=cap.read()
cv2.矩形(img,(300300),(100100),(0255,0),0)
作物_img=img[100:300,100:300]
灰色=cv2.CVT颜色(裁剪图像,cv2.COLOR\U BGR2GRAY)
值=(35,35)
模糊=cv2.高斯模糊(灰色,值,0)
_,thresh1=cv2。阈值(模糊,127255,
cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
cv2.imshow(“阈值化”,阈值1)
(版本,uu,uu)=cv2.uu版本uu.split('.'))
如果版本为“3”:
图像、轮廓、层次=cv2.findContours(thresh1.copy()\
cv2.RETR_树,cv2.CHAIN_近似值(无)
elif版本为“2”:
轮廓,层次结构=cv2.findContours(thresh1.copy(),cv2.RETR_树\
cv2.链条(约无)
cnt=最大值(轮廓,关键点=λx:cv2.轮廓面积(x))
x、 y,w,h=cv2.boundingRect(cnt)
cv2.矩形(裁剪图(x,y),(x+w,y+h),(0,0255),0)
外壳=cv2.凸形外壳(cnt)
图纸=np.零(裁剪形状,np.uint8)
cv2.图纸轮廓(图纸[cnt],0,(0255,0),0)
cv2.图纸轮廓(图纸[hull],0,(0,0255),0)
外壳=cv2.凸形外壳(cnt,返回点=假)
缺陷=cv2.凸面缺陷(碳纳米管、外壳)
缺陷计数=0
cv2.牵引轮廓(阈值1,轮廓-1,(0255,0),3)
对于范围内的i(缺陷形状[0]):
s、 e,f,d=缺陷[i,0]
start=tuple(cnt[s][0])
end=元组(cnt[e][0])
far=元组(cnt[f][0])
a=数学.sqrt((结束[0]-开始[0])**2+(结束[1]-开始[1])**2)
b=math.sqrt((远[0]-start[0])**2+(远[1]-start[1])**2)
c=数学sqrt((end[0]-far[0])**2+(end[1]-far[1])**2)
角度=数学acos((b**2+c**2-a**2)/(2*b*c))*57
如果角度>>导入cv2
>>> 
键盘中断
>>> 
kathir@kathir-VirtualBox:~/Desktop/handspirate$python basichandspirate.py
kathir@kathir-VirtualBox:~/桌面/手势$
我的密码是这个。
任何帮助都是值得感激的

问题在于虚拟盒


正如@Ram所说。我更改了VideoCapture()的源代码,现在可以使用了。

basichandspire.py是否确实有代码要执行?还是只包含类和/或函数?另外,通过执行
echo$?
检查返回代码是什么,以查看
python basichandspire.py
是否实际运行时没有错误。我认为(几乎)不可能回答您的问题,因为我们没有
basichandgenstrure.py
的任何源代码。如果它没有代码ini(或者类似于退出(0)),那么它运行正常。编辑您的问题
cap.isOpened()
可能是
False
@Kathir我看到您也在Virtualbox中运行它。您必须通过Virtualbox中的“机器设置”将摄像头“连接”到来宾操作系统。