yolov5监视应用

import numpy as np
import cv2 as cv
import cv2
from mss import mss  #实测比PIL库效率更高
import torch
import win32gui  #找窗口句柄

model = torch.hub.load('ultralytics/yolov5', 'custom', 'best.pt')

#找根据父类名或窗口查找子窗口
hwnd = win32gui.FindWindow(None, '视频文件.mp4 - VLC media player') #第一个参数类名,第二参数标题
hwnd = win32gui.FindWindowEx(hwnd, 0, None, 'QWidgetClassWindow')
hwnd = win32gui.FindWindowEx(hwnd, 0, None, 'QStackedWidgetClassWindow')
hwnd = win32gui.FindWindowEx(hwnd, 0, None, None)
hwnd = win32gui.FindWindowEx(hwnd, 0, None, None)
hwnd = win32gui.FindWindowEx(hwnd, 0, None, None)
hwnd = win32gui.FindWindowEx(hwnd, 0, None, None)

# #获取目标窗口大小
bbox = win32gui.GetWindowRect(hwnd)

sct = mss()
number = 0
#指定监视窗口大小
cv.namedWindow('testing', cv.WINDOW_NORMAL)
cv.resizeWindow('testing', 420, 240)

while True:
    sct_img = sct.grab(bbox)
    scr_img = np.array(sct_img)
    results = model(scr_img)
    results.render()  #使用边界框更新
    cv.imshow('testing', results.ims[0])  #展示实时监视窗口
    # 检查按键
    key = cv2.waitKey(25)
    if key == 27:  # ESC键退出
        cv2.destroyAllWindows()
        break