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 并修改对应的图片路径,就能实时查看当前目标检测结果了

运行效果截图!!

相关推荐
豆浩宇10 分钟前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
嵌入式杂谈19 分钟前
OpenCV计算机视觉:探索图片处理的多种操作
人工智能·opencv·计算机视觉
红米煮粥21 分钟前
图像处理-掩码
图像处理·opencv·计算机视觉
Zhangci]1 小时前
OpenCv(一)
人工智能·opencv·计算机视觉
史努比.10 小时前
redis群集三种模式:主从复制、哨兵、集群
前端·bootstrap·html
天高任鸟飞dyz10 小时前
html加载页面
css·html·css3
miao_zz10 小时前
基于HTML5的下拉刷新效果
前端·html·html5
重生之我在20年代敲代码11 小时前
HTML讲解(一)body部分
服务器·前端·html
陈小唬11 小时前
html页面整合vue2或vue3
前端·vue.js·html
18资源12 小时前
H5白色大方图形ui设计公司网站HTML模板源码
前端·javascript·html