YOLO系列——实时屏幕检测

通过PIL的ImageGrab.grab可以截取屏幕,转换成BGR格式后就可以给YOLO进行检测,一旦屏幕上出现指定的内容,就会标记出来。

python 复制代码
import cv2
from ultralytics import YOLO
from PIL import ImageGrab
import numpy as np
import cv2 as cv

model = YOLO("../yolov8n.pt")
a = (100,200,800,1000)#None 全屏,[100,200,800,1000]
a=None
while 1:
    scrn = ImageGrab.grab(bbox=a)
    #把RGB->BGR
    scrn = np.array(scrn)
    scrn = cv.cvtColor(scrn,cv2.COLOR_RGB2BGR)
    #这下YOLO可以用了 GBR
    results = model.predict(scrn)
    #results[0]保存了第0张图片的x,y,x,y坐标,conf每个目标的置信度,cls每个飙的泪飙

    # for box in results[0].boxes:
    #     print("坐标:",box.xyxy[0].tolist())
    #     print("置信度:",box.conf[0].item())
    #     print("类别ID:",box.cls[0].item())

    annotated_frame=results[0].plot()
    cv2.imshow('jian',annotated_frame) #窗口名jian,后面是检测到的帧信息
    if cv2.waitKey(1) &0xFF == ord('q'):
        break

cv2.destroyAllWindows()

屏幕一旦出现关注的目标就把屏幕保存,比如抓舞弊:

python 复制代码
import cv2
from ultralytics import YOLO
from PIL import ImageGrab
import numpy as np
import cv2 as cv

model = YOLO(r"D:\PyCharm\LearningYOLO\da_fa_yolo\runs\detect\train4\weights\best.pt")
a = (100,200,800,1000)#None 全屏,[100,200,800,1000]
a=None
while 1:
    scrn = ImageGrab.grab(bbox=a)
    #把RGB->BGR
    scrn1 = np.array(scrn)
    scrn1 = cv.cvtColor(scrn1,cv2.COLOR_RGB2BGR)
    #这下YOLO可以用了 GBR
    results = model.predict(scrn)
    #results[0]保存了第0张图片的x,y,x,y坐标,conf每个目标的置信度,cls每个飙的泪飙

    # for box in results[0].boxes:
    #     print("坐标:",box.xyxy[0].tolist())
    #     print("置信度:",box.conf[0].item())
    #     print("类别ID:",box.cls[0].item())
    c=0
    t=500
    for box in results[0].boxes:
        if box.cls[0]== 0:
            print("找到了目标")
            # 保存截图
            scrn.save(fr"./t/{c}.png")
            # 发出蜂鸣
            winsound.Beep(1000,t) #蜂鸣的频率1000,维持时间ms
            c+=1
    annotated_frame = results[0].plot()
    cv2.imshow('jian',annotated_frame)
    if cv2.waitKey(1)&0xFF==ord('q'):
        break





cv2.destroyAllWindows()
相关推荐
程序员龙叔21 小时前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户8356290780511 天前
使用 Python 操作 Word 内容控件
后端·python
LDR0061 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术1 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园1 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob1 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
qq_369224331 天前
Windows全系通用!ntdll.dll文件丢失、报错、闪退问题的完整排查与修复教程
windows·dll·dll修复·dll丢失·dll错误
源分享1 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.1 天前
C语言--day30
c语言·开发语言