Python windows环境下的YOLO目标检测
我想更改Youtuber Mark Jay的代码,它可以检测网络摄像头前的对象,从而检测pygta5等窗口中的对象。 我把代码改了一点,改成了我认为行得通的代码Python windows环境下的YOLO目标检测,python,opencv,object-detection,Python,Opencv,Object Detection,我想更改Youtuber Mark Jay的代码,它可以检测网络摄像头前的对象,从而检测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.w
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)
我需要怎样更改代码才能使其正常工作?
对不起,英语不好
提前谢谢
Tobias对于那些感兴趣的人,我的代码与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
玩得开心
Tobias你写的是if-ret:,但是这里什么是ret,不要认为你曾经定义过它是的,我没有看到if-ret我现在改变了问题^^你写的是ret,frame=cv2.cvtColorscreen,cv2.COLOR\u BGR2RGB,但是cvtColor只返回一个参数1。那么第二个论点是什么?2.太多不意味着我有太多吗?函数cv2.cvtColor只返回一个参数。在您的情况下,它是RGB图像。太多意味着您期望的函数返回次数多于。在编写代码和询问stackoverflow之前,请参阅