gpt4 book ai didi

python - Windows中的YOLO对象检测

转载 作者:行者123 更新时间:2023-12-02 17:37:29 25 4
gpt4 key购买 nike

我想更改由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)

我必须更改代码才能使其正常工作?
(对不起,英语不好)

提前致谢

托比亚斯

最佳答案

对于那些有兴趣的人,我让我的代码与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

玩的很开心

托比亚斯

关于python - Windows中的YOLO对象检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48815849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com