logo 范 · 拾光录
网址收集 关于作者 Github Gitee
杂文随笔4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端编程3
前端基础知识16
UniApp15
Vue框架19
Java编程7
Java基础知识14
SpringBoot31
SpringMVC14
MyBatis8
SpringCloud15
RocketMQ1
MyCat1
数据库4
MySQL6
Redis7
MongoDB10
H21
Java技术栈4
知识整理分布式锁的实现方案MySQL事务MySQL的锁
软件工具箱14
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaPicGoRustFSVSCodeDockerObsidianObs录制
Linux知识11
树莓派安装及使用ArchLinux:常用软件ArchLinux:基础系统安装ArchLinux:深度优化ArchLInux:图形化界面安装ArchLinux:Nirifrp内网穿透Jar启动脚本Linux常用命令VirtualBox安装CentOSVirtualBox安装Ubuntu
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

简单检测

import time

from ultralytics import YOLO

# 加载模型
model = YOLO('yolo11n.pt')
startTime = time.time()
# 检测图片
model('1.png', save=True)
print(time.time()-startTime)

实时屏幕检测

import time
import cv2
import numpy as np
import win32gui
import win32con
from mss import mss
from ultralytics import YOLO
from PIL import Image
import torch  # 添加torch库用于GPU检测

# 依赖
# pip install ultralytics opencv-python mss pywin32 pillow
# cuda环境:https://www.bilibili.com/video/BV1RDZqY2Ejs?spm_id_from=333.788.videopod.sections&vd_source=db6ea3f4f428d304a4503f2f8b25c70d

# 检查GPU可用性
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"Using device: {device}")

# 加载YOLO模型并启用GPU
model = YOLO('yolo11n.pt').to(device)  # 将模型移至GPU

# 获取屏幕尺寸
with mss() as sct:
    monitor = sct.monitors[1]  # 主显示器
    screen_width, screen_height = monitor["width"], monitor["height"]

# 计算中心区域坐标 (640x480)
crop_width, crop_height = 320, 320
crop_x = (screen_width - crop_width) // 2
crop_y = (screen_height - crop_height) // 2

# 创建透明覆盖窗口
window_name = "YOLO Detection Overlay"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_TOPMOST, 1)  # 置顶窗口
cv2.resizeWindow(window_name, screen_width, screen_height)
cv2.moveWindow(window_name, 0, 0)

# 设置窗口透明属性 (仅Windows)
hwnd = win32gui.FindWindow(None, window_name)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
                       win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) |
                       win32con.WS_EX_LAYERED |
                       win32con.WS_EX_TRANSPARENT)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 0, win32con.LWA_COLORKEY)

# 创建透明画布 (全屏尺寸)
overlay = np.zeros((screen_height, screen_width, 4), dtype=np.uint8)

# 主循环
with mss() as sct:
    monitor = {"top": crop_y, "left": crop_x, "width": crop_width, "height": crop_height}

    # GPU预热 - 首次推理通常较慢
    warmup_img = Image.new('RGB', (crop_width, crop_height), (0, 0, 0))
    _ = model(warmup_img, device=device, verbose=False)

    while True:
        start_time = time.perf_counter()

        # 捕获屏幕中心区域
        screenshot = sct.grab(monitor)
        img = Image.frombytes("RGB", screenshot.size, screenshot.rgb)

        # YOLO目标检测 - 使用GPU加速
        results = model(img,
                        device=device,  # 指定使用GPU
                        half=True,  # 启用半精度推理(仅GPU有效)
                        verbose=False)  # 禁用详细日志

        # 重置覆盖层
        overlay.fill(0)

        # 绘制检测区域边框(灰色边框)
        cv2.rectangle(overlay, (crop_x, crop_y),
                      (crop_x + crop_width, crop_y + crop_height),
                      (150, 150, 150, 255), 2)  # 灰色边框 (BGR: 150,150,150)

        # 添加检测区域标签
        cv2.putText(overlay, "Detection Area",
                    (crop_x + 10, crop_y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, (200, 200, 200, 255), 2)

        # 绘制检测结果
        for result in results:
            for box in result.boxes:
                # 转换坐标到全屏坐标系
                x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
                x1 += crop_x
                y1 += crop_y
                x2 += crop_x
                y2 += crop_y

                # 绘制边界框 (BGR颜色 + 透明度)
                cv2.rectangle(overlay, (x1, y1), (x2, y2), (0, 255, 0, 255), 2)

                # 获取类别和置信度
                cls_id = int(box.cls)
                conf = float(box.conf)
                label = f"{result.names[cls_id]} {conf:.2f}"

                # 绘制标签
                cv2.putText(overlay, label, (x1, y1 - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0, 255), 2)

        # 计算并显示FPS
        fps = 1 / (time.perf_counter() - start_time)
        cv2.putText(overlay, f"FPS: {fps:.1f} | Delay: {1000 / fps:.1f}ms | Device: {device.upper()}",
                    (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255, 255), 2)

        # 显示覆盖层
        cv2.imshow(window_name, overlay)

        # 退出条件
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

# 清理资源
cv2.destroyAllWindows()

数据标注工具

https://github.com/HumanSignal/labelImg

简单检测
实时屏幕检测
数据标注工具