我有一个脚本可以抓取应用程序的屏幕快照并显示出来。它在我的机器上运行得很好,就像一个60英尺左右的视频。现在,我想在这些帧上使用yolov5对象检测模型,并使用TorchHub,如建议的这里。
以下工作:
import os
os.getcwd()
from PIL import ImageGrab
import numpy as np
import cv2
import pyautogui
import win32gui
import time
from mss import mss
from PIL import Image
import tempfile
os.system('calc')
sct = mss()
xx=1
tstart = time.time()
while xx<10000:
hwnd = win32gui.FindWindow(None, 'Calculator')
left_x, top_y, right_x, bottom_y = win32gui.GetWindowRect(hwnd)
#screen = np.array(ImageGrab.grab( bbox = (left_x, top_y, right_x, bottom_y ) ) )
bbox = {'top': top_y, 'left': left_x, 'width': right_x-left_x, 'height':bottom_y-top_y }
screen = sct.grab(bbox)
scr = np.array(screen)
cv2.imshow('window', scr)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
xx+=1
cv2.destroyAllWindows()
tend = time.time()
print(xx/(tend-tstart))
print((tend-tstart))
os.system('taskkill /f /im calculator.exe')下面我尝试使用import torch并使用我以前受过训练的模型,
screen = sct.grab(bbox)
scr = np.array(screen)
result = model(scr, size=400)
result.save("test.png") #this gives a TypeError: save() takes 1 positional argument but 2 were given
result.show() #this opens a new Paint instance for every frame instead of keeping the same window.
# The shown image is also in a wrong color channel
scr = cv2.imread("test.png")
# How can I use the `result` as argument to cv2.imshow(),
# without saving to disk if possible?我的问题:
result.show()显示的图像与cv2.imshow()相比有错误的颜色通道,如何确保向model提供的图像位于正确的通道上?cv2.imshow()那样在单个窗口中显示带有边界框的结果模型图像吗?(result.show()为每个帧打开一个新的画图处理实例)?如何将此结果映像保存到磁盘,并找到有关如何与model对象进行一般交互的更多文档?发布于 2020-12-18 21:25:38
工作如下:result = model(cv2.cvtColor(scr, cv2.COLOR_BGR2RGB), size=400)这解决了准确性问题,model.save()有预定义的输出名称,这些名字目前是不可更改的,不需要任何参数。当输入正确的颜色通道时,model.show()会显示正确的颜色通道输出。
发布于 2020-12-20 18:07:34
我认为cvtColor操作应该与YOLOv5 PyTorch Hub 教程中显示的提供的信道顺序倒置相同。这将返回测试过的两个环境中的True (CoLab记事本python3.6和MacOS python3.9)
import cv2
import numpy as np
file = 'data/images/bus.jpg'
im1 = cv2.imread(file)[:, :, ::-1]
im2 = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB)
print(np.allclose(im1, im2))https://stackoverflow.com/questions/65363565
复制相似问题