opencv对象检测需要一个整数(got类型元组)

时间:2019-06-07 11:19:13

标签: python opencv

当我运行代码以使用网络摄像头进行对象检测时,收到以下错误消息:

 frame = cv2.rectangle(open_cv_stream, t1, br, color, 5)

TypeError:必须为整数(元组类型为元组)

capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

while True:
    stime = time.time()
    ret, frame = capture.read()

    if ret:
        results = tfnet.return_predict(frame)

        for color, result in zip(colors, results):
            t1 = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright'])
            label = result['label']
            confidence = result['confidence']
            text = '{}: {:.0f}%'.format(label,confidence * 100)
            frame = cv2.rectangle(frame, t1, br, color, 5)
            frame = cv2.putText(frame, text, t1, cv2.FONT_ITALIC, 1, (0,0,0),2)
        cv2.imshow('frame',frame)
        print('FPS {:.1f}'.format(1/(time.time() - stime)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
capture.release()
cv2.destroyAllWindows()

可能是什么问题?

在此追溯:

Building net ...
Source | Train? | Layer description                | Output size
-------+--------+----------------------------------+---------------
WARNING:tensorflow:From C:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
       |        | input                            | (?, 608, 608, 3)
 Load  |  Yep!  | conv 3x3p1_1  +bnorm  leaky      | (?, 608, 608, 32)
…
 Load  |  Yep!  | conv 1x1p0_1    linear           | (?, 19, 19, 425)
-------+--------+----------------------------------+---------------
GPU mode with 1.0 usage
Finished in 28.833083629608154s

Traceback (most recent call last):
  File "webcam.py", line 38, in <module>
    frame = cv2.rectangle(open_cv_stream, t1, br, color, 5)
TypeError: an integer is required (got type tuple)

1 个答案:

答案 0 :(得分:1)

我注意到您错过了br变量中的['y']。

 for color, result in zip(colors, results):
        t1 = (result['topleft']['x'], result['topleft']['y'])
        br = (result['bottomright']['x'], result['bottomright']['y'])
        label = result['label']
        confidence = result['confidence']
        text = '{}: {:.0f}%'.format(label,confidence * 100)
        frame = cv2.rectangle(frame, t1, br, color, 5)
        frame = cv2.putText(frame, text, t1, cv2.FONT_ITALIC, 1, (0,0,0),2)

看看是否可行!