【发布时间】: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