Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 警告:0在cv2中终止异步回调错误_Python_Python 3.x_Python 2.7_Opencv_Cv2 - Fatal编程技术网

Python 警告:0在cv2中终止异步回调错误

Python 警告:0在cv2中终止异步回调错误,python,python-3.x,python-2.7,opencv,cv2,Python,Python 3.x,Python 2.7,Opencv,Cv2,我陷入了下面的代码中,请帮助我解决错误:cv2中的0终止异步回调错误 # this is a client program which run on client side import cv2 import socket import numpy as np import math import time try: start_time = time.time() state1 = "off" state2 = "off" state3 = "off"

我陷入了下面的代码中,请帮助我解决错误:cv2中的0终止异步回调错误

# this is a client program which run on client side
import cv2
import socket
import numpy as np
import math
import time


try:
    start_time = time.time()
    state1 = "off"
    state2 = "off"
    state3 = "off"
    mode = "on"
    host = "192.168.0.106" # socket which acording server in our case #ip address of  rapberry Pi 
    port = 9345
    mySocket = socket.socket()
    mySocket.connect((host,port))
    cap = cv2.VideoCapture(0)
    while(cap.isOpened()):
    # read image
        ret, img = cap.read()

    # get hand data from the rectangle sub window on the screen
        cv2.rectangle(img, (300,300), (100,100), (0,255,0),0)
        crop_img = img[100:300, 100:300]

    # convert to grayscale
        grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)

    # applying gaussian blur
        value = (35, 35)
        blurred = cv2.GaussianBlur(grey, value, 0)

    # thresholdin: Otsu's Binarization method
        _, thresh1 = cv2.threshold(blurred, 127, 255,
                               cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    # show thresholded image
        cv2.imshow('Thresholded', thresh1)

    # check OpenCV version to avoid unpacking error
        (version, _, _) = cv2.__version__.split('.')

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

    # find contour with max area
        cnt = max(contours, key = lambda x: cv2.contourArea(x))

    # create bounding rectangle around the contour (can skip below two lines)
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(crop_img, (x, y), (x+w, y+h), (0, 0, 255), 0)

    # finding convex hull
        hull = cv2.convexHull(cnt)

    # drawing contours
        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)

    # finding convex hull
        hull = cv2.convexHull(cnt, returnPoints=False)

    # finding convexity defects
        defects = cv2.convexityDefects(cnt, hull)
        count_defects = 0
        cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3)

    # applying Cosine Rule to find angle for all defects (between fingers)
    # with angle > 90 degrees and ignore defects
        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])

        # find length of all sides of triangle
            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)

        # apply cosine rule here
            angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57

        # ignore angles > 90 and highlight rest with red dots
            if angle <= 90:
                count_defects += 1
                cv2.circle(crop_img, far, 1, [0,0,255], -1)
        #dist = cv2.pointPolygonTest(cnt,far,True)

        # draw a line from start to end i.e. the convex points (finger tips)
        # (can skip this part)
            cv2.line(crop_img,start, end, [0,255,0], 2)
        #cv2.circle(crop_img,far,5,[0,0,255],-1)

    # define actions required

        if count_defects == 1:
            if (time.time()>start_time+2):
                if mode == "on":
                    mySocket.send("on_led1".encode())
                    state1 = "on"
                    print("led 1 is on")
                else:
                    mySocket.send("off_led1".encode())
                    state1 = "off"
                    print("led 1 is off")
                start_time = time.time()
            cv2.putText(img, "led 1 is "+state1, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2)


        elif count_defects == 2:
            if (time.time()>start_time+2):
                if mode == "on":
                    mySocket.send("on_led2".encode())
                    state2 = "on"
                    print("led 2 is on")
                else:
                    mySocket.send("off_led2".encode())
                    state2 = "off"
                    print("led 2 is off")
                start_time = time.time()
            cv2.putText(img, "led 2 is "+state2, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2)

        elif count_defects == 3:
            if (time.time()>start_time+2):
                if mode == "on":
                    mySocket.send("on_led3".encode())
                    state3 = "on"
                    print("led 3 is on")
                else:
                    mySocket.send("off_led3".encode())
                    state3 = "off"
                    print("led 3 is off")
                start_time = time.time()
            cv2.putText(img, "led 3 is "+state3, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
        elif count_defects == 4:
            cv2.putText(img,"mode is "+mode, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2)

            if(time.time() > start_time+2):
                if mode == "on":
                    mode = "off"
                else:
                    mode = "on"
                start_time = time.time()
                print(mode)

        else:
            cv2.putText(img, "use your fingure for turn On/Off lights current mode is "+mode, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2)

    # show appropriate images in windows
        cv2.imshow('Gesture', img)
        all_img = np.hstack((drawing, crop_img))
        cv2.imshow('Contours', all_img)

        k = cv2.waitKey(10)
        if k == 27:
            break
    mySocket.close()

except:

    mySocket.send("close_all".encode())
    mySocket.close()
#这是一个在客户端运行的客户端程序
进口cv2
导入套接字
将numpy作为np导入
输入数学
导入时间
尝试:
开始时间=time.time()
state1=“关”
state2=“关”
state3=“关”
mode=“on”
host=“192.168.0.106”#在我们的例子中记录服务器的套接字#Rapperry Pi的ip地址
端口=9345
mySocket=socket.socket()
mySocket.connect((主机、端口))
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)
#thresholdin:Otsu二值化方法
_,thresh1=cv2。阈值(模糊,127255,
cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
#显示阈值图像
cv2.imshow(“阈值化”,阈值1)
#检查OpenCV版本以避免解包错误
(版本,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,0,255),0)
#寻找凸壳
外壳=cv2.凸形外壳(cnt)
#绘制等高线
图纸=np.零(裁剪形状,np.uint8)
cv2.图纸轮廓(图纸[cnt],0,(0,255,0),0)
cv2.图纸轮廓(图纸[hull],0,(0,0,255),0)
#寻找凸壳
外壳=cv2.凸形外壳(cnt,返回点=假)
#寻找凸性缺陷
缺陷=cv2.凸面缺陷(碳纳米管、外壳)
缺陷计数=0
cv2.绘制等高线(阈值1,等高线,-1,(0,255,0),3)
#应用余弦规则查找所有缺陷的角度(指间)
#角度>90度且忽略缺陷
对于范围内的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
#忽略大于90的角度,并用红点突出显示其余部分
如果角度开始时间+2):
如果模式==“打开”:
mySocket.send(“on_led1.encode())
state1=“on”
打印(“led 1亮起”)
其他:
mySocket.send(“off_led1.encode())
state1=“关”
打印(“led 1熄灭”)
开始时间=time.time()
cv2.putText(img,“led 1为”+状态1,(5,50),cv2.FONT\u HERSHEY\u SIMPLEX,1,2)
elif计数_缺陷==2:
如果(time.time()>开始时间+2):
如果模式==“打开”:
mySocket.send(“on_led2.encode())
state2=“on”
打印(“led 2亮起”)
其他:
mySocket.send(“off_led2.encode())
state2=“关”
打印(“led 2熄灭”)
开始时间=time.time()
cv2.putText(img,“led 2为”+状态2,(5,50),cv2.FONT\u HERSHEY\u SIMPLEX,1,2)
elif计数_缺陷==3:
如果(time.time()>开始时间+2):
如果模式==“打开”:
mySocket.send(“on_led3.encode())
state3=“on”
打印(“led 3亮起”)
其他:
mySocket.send(“off_led3.encode())
state3=“关”
打印(“led 3熄灭”)
开始时间=time.time()
cv2.putText(img,“led 3为”+状态3,(5,50),cv2.FONT\u HERSHEY\u SIMPLEX,1,2)
elif计数_缺陷==4:
cv2.putText(img,“mode is”+mode,(50,50),cv2.FONT\u HERSHEY\u SIMPLEX,2,2)
如果(time.time()>开始时间+2):
如果模式==“打开”:
模式=“关闭”
其他:
mode=“on”
开始时间=time.time()
打印(模式)
其他:
cv2.putText(img,“使用您的fingure打开/关闭灯电流模式为”+模式,(5,50),cv2.FONT\u HERSHEY\u SIMPLEX,1,2)
#在windows中显示适当的图像
cv2.imshow(“手势”,img)
all_img=np.hstack((图纸、裁剪)
cv2.imshow(“轮廓”,所有图像)
k=cv2.等待键(10)
如果k==27:
打破
mySocket.close()
除:
mySocket.send(“全部关闭”.encode())
mySocket.close()
上面是我为使用cv2访问嵌入式设备而创建的一个程序。所有东西都正常工作,但我仍然收到一个错误,
[WARN:0]终止异步回调
我还使用了
camera.release()
cv2.destroyAllWindows()
函数,但它不起作用,请提供任何帮助

我也尝试过这个建议,但仍然不起作用,因为我使用的是windows
cap = cv2.VideoCapture(cv2.CAP_DSHOW)
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    if version == '3':
        image, contours, hierarchy = cv2.findContours(thresh1.copy(), \
               cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    elif version == '2':
        contours, hierarchy = cv2.findContours(thresh1.copy(),cv2.RETR_TREE, \
               cv2.CHAIN_APPROX_NONE)