WARN:0终止cv2中的异步回调错误

时间:2019-02-27 11:17:32

标签: python python-3.x python-2.7 opencv cv2

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

# 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访问嵌入式设备的程序,所有事情都正常运行,但仍然出现错误,[ WARN:0] terminating async callback我也使用camera.release() cv2.destroyAllWindows()函数,但是不起作用,将不胜感激

我也尝试了此建议suggestion,但在使用Windows 10操作系统的情况下仍然无法使用

这是完整的代码,带有解释code

2 个答案:

答案 0 :(得分:1)

我在您的代码中发现了问题。也许您已将cv2库更新为版本4。这就是为什么两个条件都不能满足的原因,下面给出的条件不能满足。

    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) 

您应该删除elif version == '2':并仅使用else:可能会有帮助。

答案 1 :(得分:0)

此警告来自MSMF后端。试试:

cap = cv2.VideoCapture(cv2.CAP_DSHOW)

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)