I trained a model using the Yolov5 Google Colab notebook with custom data and classes. I used the detect.py script to do object detection on a video with no issues.
我下载了. pt文件,并将其加载到我存储在机器上的脚本中,该脚本基于Yolov5的Github截图推理示例,当我在PC上的图像上使用它时,它工作得很好:https://github.com/ultralytics/yolov5/issues/36
现在我知道了PyTorch文件可以毫无问题地加载,并且检测工作正常,我尝试通过截图来执行实时检测。我可以通过截图来记录我的屏幕,但当我尝试对模型这样做时,我得到了一个错误。以下是我的代码:
import time
import torch
import cv2
import mss
import numpy
# Load model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='last_210_epochs_Size1280.pt')
with mss.mss() as sct:
# Part of the screen to capture
monitor = sct.monitors[2]
while "Screen capturing":
last_time = time.time()
# Get raw pixels from the screen, save it to a Numpy array
img = numpy.array(sct.grab(monitor))
results = model(img)
# Display the picture
cv2.imshow('test', results)
# Display the picture in grayscale
# cv2.imshow('OpenCV/Numpy grayscale',
# cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))
print("fps: {}".format(1 / (time.time() - last_time)))
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
这是我得到的错误:
不知道我做错了什么。
1条答案
按热度按时间t9aqgxwy1#
希望对你有帮助。