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

运行效果截图!!

相关推荐
djk88881 小时前
支持手机屏幕的layui后台html模板
前端·html·layui
Daydream.V1 小时前
Opencv高端操作——上采样/下采样及拉普拉斯金字塔
人工智能·opencv·计算机视觉
光羽隹衡1 小时前
计算机视觉——Opencv(物体跟踪)
人工智能·opencv·计算机视觉
张老师带你学2 小时前
unity船资源,快艇,帆船,游轮
科技·游戏·unity·游戏引擎·模型
sali-tec2 小时前
C# 基于OpenCv的视觉工作流-章37-区域截图
图像处理·人工智能·opencv·算法·计算机视觉
云边散步3 小时前
godot2D游戏教程系列二(17)
笔记·学习·游戏
大囚长3 小时前
游戏主机神经纹理压缩与AI重建技术的综合应用方案分析
人工智能·游戏
我材不敲代码3 小时前
OpenCV 实战:从可乐标志识别到银行卡、身份证号识别(模板匹配 + 轮廓检测)
人工智能·opencv·计算机视觉
H Journey4 小时前
OpenCV常用函数分析之bitwise_and
opencv·bitwise_and
H Journey4 小时前
OpenCV之Canny 边缘检测与MediaPipe 人物分割
人工智能·opencv·计算机视觉·mediapipe