Search code examples
pythonartificial-intelligenceobject-detectionyolov5

How to make YoloV5 detect by screen grab?


I am using the following in order to grab screenshots from an open application in realtime. How can I run detect.py that only detect input from my grab screen? Thanks.

My grab screen

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 detect.py

For now it only detect images instead of my Realtime grab screen

# 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()

Expected Result:

enter image description here


Solution

  • Jut pass the screen grab to the model:

    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