实时馈送形状检测TypeError

时间:2016-11-21 14:56:38

标签: python-2.7 opencv numpy shape live-streaming

Spyder @ Anaconda IDE发现的问题如下所示:

  

文件“C:/Users/zhiyong/Desktop/Y3_S2/programming/blue_tri_det.py”,第91行,       cv2.drawContours(roi,shape_contour,-1,(0,255,0),3,(-1,2))

     

TypeError:需要一个整数

而且,当我没有在网络摄像头的前面放置任何蓝色三角形时它工作正常,但是当我在它前面粘贴一个蓝色三角形时,整个检测代码都掉了。

def run(image, c):
    # convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)
    # if the shape is a triangle, it will have 3 vertices
    if len(approx) == 3:
        shape = "triangle"
        print(shape)

        # blur and threshold the image
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)

        # find the contours in the thresholded image
        (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                 cv2.CHAIN_APPROX_SIMPLE)

        # if no contours were found, return None
        if len(cnts) == 0:
            return None

        # otherwise, sort the contours by area and compute the rotated
        # bounding box of the largest contour
        c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
        rect = cv2.minAreaRect(c)
        box = np.int0(cv2.cv.BoxPoints(rect))


    else:
        shape = "No triangle is detected"
        print(shape)

    # return the bounding box of the barcode
    return box

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help = "path to the (optional) video file")
args = vars(ap.parse_args())

# if the video path was not supplied, grab the reference to the
# camera
if not args.get("video", False):
    camera = cv2.VideoCapture(0)

# otherwise, load the video
else:
    camera = cv2.VideoCapture(args["video"])

# keep looping over the frames
while True:
    # grab the current frame
    (grabbed, frame) = camera.read()

    # check to see if we have reached the end of the
    # video
    if not grabbed:
        break

    # detect the barcode in the image
    box = run(frame, c="unidentified")

    # if a barcode was found, draw a bounding box on the frame
    cv2.drawContours(frame, [box], -1, (0, 255, 0), 3, (-1,2))

    # show the frame and record if the user presses a key
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break

# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

0 个答案:

没有答案