windows 中的 YOLO 对象检测
YOLO object detection in windows
我想更改我从 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()
这个代码returns这个错误
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
玩得开心
托比亚斯
我想更改我从 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()
这个代码returns这个错误
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
玩得开心
托比亚斯