如何让YoloV5通过屏幕抓取进行检测?

2024-04-29 05:01:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的方法从一个开放的应用程序中实时抓取屏幕截图。如何运行detect.py以仅检测抓取屏幕中的输入?谢谢

我的抓取屏幕

import cv2 as cv
import numpy as np

import numpy as np
import cv2
from mss import mss
from PIL import Image

bounding_box = {'top': 340, 'left': 800, 'width': 350, 'height': 400}

sct = mss()


while True:
    sct_img = sct.grab(bounding_box)
    scr_img = np.array(sct_img)

    #cv2.imshow('screen', scr_img) # display screen in box
    cv.imshow('Testing', scr_img)

    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        cv2.destroyAllWindows()
        break

YoloV5.py

现在它只检测图像,而不是我的实时抓取屏幕

# PyTorch Hub
import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
dir = 'https://ultralytics.com/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()

预期结果:

enter image description here


Tags: frompyimportnumpyboximg屏幕as
1条回答
网友
1楼 · 发布于 2024-04-29 05:01:56

将屏幕抓取传递给模型:

import cv2 as cv
import numpy as np

import numpy as np
import cv2
from mss import mss
from PIL import Image

# PyTorch Hub
import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

bounding_box = {'top': 340, 'left': 800, 'width': 350, 'height': 400}

sct = mss()


while True:
    sct_img = sct.grab(bounding_box)
    scr_img = np.array(sct_img)

    #cv2.imshow('screen', scr_img) # display screen in box
    scr_img = model(scr_img)
    cv.imshow('Testing', scr_img)

    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        cv2.destroyAllWindows()
        break

相关问题 更多 >