Yolo V8 模型训练初学者笔记 -- 目标检测应用到实际游戏程序中

衔接上一篇:# YOLO V8 模型训练和目标检测初学者笔记

环境要求(同上一篇)

  • Anaconda
  • Anaconda 中安装并激活 python39(因为 labelimg 工具目前似乎最多支持到 python39)
python 复制代码
pip install ultralytics
# 本篇文章中涉及到使用 pyautogui 实现区域截图,没安装的话需要安装一下
pip install pyautogui

前言:

  • 在上一篇文章中,我粗糙得训练出了一个简单的模型,且能够在目标图片上识别出我想要的游戏人物了
  • 那么接下来我要继续迈出下一步,也就是本文的内容了:
  • 【对我正在玩的游戏窗体进行目标检测,并实时显示检测结果】

目标检测代码在此!!!

python 复制代码
from ultralytics import YOLO
import pyautogui
import pygetwindow
import time

# 通过窗体标题,获取窗口位置和宽高
def window_sywh(window_title):
    macthing_windows = pygetwindow.getWindowsWithTitle(window_title)
    if len(macthing_windows) == 0:
        raise Exception("未找到窗口 %", window_title)
    win = macthing_windows[0]
    return win.left, win.top, win.width, win.height

# 对指定区域截图
def window_screenshot(region):
    return pyautogui.screenshot(region=region)

# 程序开始
def main(window_title):
    start_wait = 3
    print("程序在%d秒后开始运行", start_wait)
    model = YOLO("fcmario_last.pt")
    time.sleep(start_wait)

    region = window_sywh(window_title=window_title)
    # -- 循环
    while True:
        start_time = time.time()
        screenshot = window_screenshot(region=region)
        result = model(source=screenshot, save=True)
        print("识别结果:", result)
        print(result)



if __name__ == '__main__':
    # 游戏窗体的标题
    window_title = "NNNesterJ 0.23"
    main(window_title)

这里 window_title 就是如下图所示的窗体标题

实时检测图片变化的 html 在此!!!

  • 上面的 python 脚本中,会通过 yolov8 不断把当前游戏区域的截图保存到 runs/detect/predict[N] 文件夹中,我这里通过 html 实时刷新来实现实时监控检测结果
html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>实时图像更新</title>
    <style>
        #image-display {
            max-width: 100%;
            height: auto;
            display: block;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <h1>实时图像更新</h1>
    <div>predict[N]: <input id="predictinput" value="2" type="number"/></div>
    <div>predict image: <input id="predictimg" value="image0.jpg" type="text"/></div>
    <img id="image-display" src="">
    <script>
        const imageElement = document.getElementById('image-display');
        const predictInput = document.getElementById('predictinput');
        const predictImg = document.getElementById('predictimg');

        function updateImage() {
            const currentTime = new Date().getTime();
            // 由于每次运行检测脚本,对于输出的图片路径会变
            // 这里每次看图片时,需要修改成需要实时监看的图片路径(即 predict1 修改成 predict2 或 predict3 或 predict[N])
            const pi = predictInput.value || '2'
            const pi_img = predictImg.value || 'image0.jpg'
            imageElement.src = `D:\\_codingWorkspace\\spidervideo_py\\ai_traning_for_game\\fc_mario_game\\runs\\detect\\predict${pi}\\${pi_img}?t=${currentTime}`;
            requestAnimationFrame(updateImage);
        }

        updateImage();
    </script>
</body>
</html>

代码执行!

  • 在 anaconda prompt 终端,cd 定位到脚本所在目录,然后执行脚本
shell 复制代码
python yolo_predit_forapplication.py
  • 先运行脚本后,会在 runs/detect/predict[N] 下生成目标检测结果图,再打开 html 并修改对应的图片路径,就能实时查看当前目标检测结果了

运行效果截图!!

相关推荐
子夜江寒42 分钟前
基于 OpenCV 的图像形态学与边缘检测
python·opencv·计算机视觉
saoys1 小时前
Opencv 学习笔记:创建与原图等尺寸的空白图像
笔记·opencv·学习
智驱力人工智能8 小时前
守护流动的规则 基于视觉分析的穿越导流线区检测技术工程实践 交通路口导流区穿越实时预警技术 智慧交通部署指南
人工智能·opencv·安全·目标检测·计算机视觉·cnn·边缘计算
@areok@10 小时前
C++opencv图片(mat)传入C#bitmap图片
开发语言·c++·opencv
柠檬071114 小时前
fillPoly 函数
opencv
i橡皮擦16 小时前
TheIsle恐龙岛游戏管理员命令
游戏·恐龙岛·theisle
dazzle16 小时前
计算机视觉处理(OpenCV基础教学(十九):图像轮廓特征查找技术详解)
人工智能·opencv·计算机视觉
zhutoutoutousan19 小时前
氛围数学学习:用游戏化思维征服抽象数学
学习·算法·游戏
科雷软件测试19 小时前
推荐几个常用的校验yaml、json、xml、md等多种文件格式的在线网站
xml·html·md·yaml
Serendipity-Solitude20 小时前
HTML 五子棋实现方法
前端·html