Use YOLOv3 (Python - Keras) real-time detection screen tutorial (to detect GTA5 game screen as an example)

A graduation project done automatically driving in the game GTA5 the project, which is a function of automobile collision detection. In order to achieve this I first need to achieve recognition of automobile, compared the use of SSD and Faster-RCNN, I chose YOLOv3 to achieve this function, because it is both speed and accuracy, but also very flexible, is simply the industry conscience.

At that time online access to tutorials, a lot of tutorials just talked about calling file enable detection of the video file. However, I need the game screen displayed on the screen to detect, so a lot of tutorials do not apply.

Now do the complete set relatively idle, so the record about the way people help later, this tutorial will tell how the picture on the screen with YOLOv3 detection.

 

This article is designed to help novices get started quickly YOLOv3, it will not involve detailed code and corresponding algorithm principle, after specific YOLO principle so have the time to write a detailed article ~

 

1, download YOLOv3 of Keras version ( https://github.com/qqwweee/keras-yolo3 ) there are mature code.

2, generated Keras of .h5 file,

need:

  •  1. yolo network structure profile .cfg, as yolov3.cfg
  •  2. yolo trained weights file .weights, such as yolov3.weights

Weights file: https://pjreddie.com/media/files/yolov3.weights , this file contains the parameters of some weight pre-trained.

Network configuration file structure: The project comes with a default .cfg YOLOv3-416 network. If other networks, txt file open places .cfg file, the parameter lines 8 and 9, line 320 or 608 is modified.

The figure below shows the various versions of the YOLOv3 accuracy and speed comparison based on accuracy and speed demands of their own projects, to choose their own network (change the .cfg)

下面为官网对比(https://pjreddie.com/darknet/yolo/

 

下载好上述两个文件后,将权重文件放到项目根目录下

3、运行convert.py,将 yolov3.weights转换成Keras能使用的.h5文件

使用方法:

python convert.py yolov3.cfg yolov3.weights yolov3.h5

执行完成示意图

执行完成之后在当前目录下生成yolov3.h5文件,将其重命名为yolo.h5并放入model_data文件夹中。

4、新建grabscreen.py文件,复制以下内容,此文件里实现用Win32API截取屏幕。相比于使用PIL,这种方式截屏速度更快

import cv2
import numpy as np
import win32gui
import win32ui
import win32con
import win32api


def grab_screen(region=None):

    hwin = win32gui.GetDesktopWindow()

    if region:
        left, top, x2, y2 = region
        width = x2 - left + 1
        height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)


    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height, width, 4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)

5、新建example.py文件,复制以下内容 ,并运行。

import numpy as np
import cv2
import time
from grabscreen import grab_screen
from PIL import Image
from yolo import YOLO
import tensorflow as tf

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.8   #爆显存的话可以在此调整程序的显存占用情况
session = tf.Session(config=config)

yolo = YOLO()

while True:

    image_array = grab_screen(region=(0, 0, 1280, 720))
    # 获取屏幕,(0, 0, 1280, 720)表示从屏幕坐标(0,0)即左上角,截取往右1280和往下720的画面
    array_to_image = Image.fromarray(image_array, mode='RGB') #将array转成图像,才能送入yolo进行预测
    img = yolo.detect_image(array_to_image)  #调用yolo文件里的函数进行检测

    img = np.asarray(img) #将图像转成array

    cv2.imshow('window',cv2.cvtColor(img, cv2.COLOR_BGR2RGB))  #将截取的画面从另一窗口显示出来,对速度会有一点点影响,不过也就截取每帧多了大约0.01s的时间
    if cv2.waitKey(25) & 0xFF == ord('q'):  #按q退出,记得输入切成英语再按q
        cv2.destroyAllWindows()
        break

6、检测结果:

左边为GTA5的游戏画面,右边为检测结果,使用一个窗口显示出来

 

发布了10 篇原创文章 · 获赞 13 · 访问量 1万+

Guess you like

Origin blog.csdn.net/ACL_lihan/article/details/104011405