【问题标题】:YOLO object detection in windowsWindows 中的 YOLO 对象检测
【发布时间】:2018-07-26 17:13:58
【问题描述】:

我想更改 Youtuber Mark Jay 获得的代码,该代码可以检测网络摄像头前的对象以检测 Windows 中的对象(如 pygta5)。 (我把代码改成了我(菜鸟)认为可以工作的东西)

import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
from PIL import ImageGrab
options = {
    'model': 'cfg/yolo.cfg',
    'load': 'bin/yolo.weights',
    'threshold': 0.2,
    'gpu': 1.0
}

tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]

#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()
    screen =  np.array(ImageGrab.grab(bbox=(0,0,1920,1080)))
    ret, frame = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
    results = tfnet.return_predict(frame)
    if ret:
        for color, result in zip(colors, results):
            tl = (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, tl, br, color, 5)
            frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow('frame', frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

此代码返回此错误

Traceback (most recent call last):
  File "D:\Python_Object_analyzis\YOLO Version\darkflow-master\Person_detection.py", line 23, in <module>
    ret, frame = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
ValueError: too many values to unpack (expected 2)

我必须更改哪些代码才能使其正常工作? (抱歉英语不好)

提前致谢

托比亚斯

【问题讨论】:

  • 你写if ret:,但是这里是ret,不要以为你曾经定义过它
  • 是的,我没有看到if ret 我现在改变了问题^^
  • 你写了ret, frame = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)cvtColor 只返回一个参数
  • 1.那么第二个论点是什么? 2.too many不是说我有太多吗?
  • 函数 cv2.cvtColor 只返回一个参数。在您的情况下,它是 RGB 图像。 Too many 表示您期望的不仅仅是函数返回。写代码前请参考documentationstackoverflow上的提问

标签: python opencv object-detection


【解决方案1】:

对于那些感兴趣的人,我让我的代码使用 python mss,这也更快。

import cv2
from darkflow.net.build import TFNet
import numpy as np
import mss
options = {
    'model': 'cfg/tiny-yolo-voc.cfg',
    'load': 'bin/tiny-yolo-voc.weights',
    'threshold': 0.23,
    'gpu': 0.26
}
tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]
with mss.mss() as sct:
    monitor = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
    while True:
        screen =  np.array(sct.grab(monitor))
        ret, frame = True, cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
        results = tfnet.return_predict(frame)
        if ret:
            for color, result in zip(colors, results):
                tl = (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, tl, br, color, 5)
                frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow('frame', cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

玩得开心

托比亚斯

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多