首页 > 解决方案 > 如何将 png 截图实时转换为 CSV 文件?

问题描述

我正在制作一个神经网络 AI,它将获取游戏的像素信息并决定按下哪些按钮。这是一个格斗游戏,所以我要求数据是实时给出的。

在我的代码中,我截取了游戏的屏幕截图,这部分速度很快,然后我将其转换为 CSV 文件,大约需要半秒。关于如何加快速度的任何想法?或者您知道我不需要使用 CSV 文件将训练/输入数据提供给 AI 的方式吗?

这是代码:

from PIL import Image
import os
import csv
import numpy as np
import glob
import pandas as pd
import pygetwindow
import pyautogui
from PIL import Image
import threading
import os
import PIL
import glob
import keyboard
import time
import cv2
import mss
import numpy

number = 0
path = r'C:\Users\samko\Desktop\AI - super figters deluxe\\'
windwonName = 'Superfighters Deluxe v.1.3.7c'



def csvWriter(fil_name, nparray, number): # this bit converts the screenshot into a csv file and writes it into a folder
  example = nparray.tolist()
  with open(fil_name+'.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerows(example)
    print('Done: '+str(number))
    number += 1
    return number

with mss.mss() as sct:

    while 1<2:
        name = 'temp_data'
        window = pygetwindow.getWindowsWithTitle('Superfighters Deluxe v.1.3.7c')[0] # gets the location of the window to specify screenshot location
        window.activate()
        x1, y1 = window.topleft
        x2, y2 = window.bottomright

        monitor = {"top": y1, "left": x1, "width": x2-x1, "height": y2-y1}

        # Get raw pixels from the screen, save it to a Numpy array

        img = numpy.array(sct.grab(monitor))
        numpy.resize(img, (800, 600)) # scales the image so that all are same size

        number = csvWriter(r"C:\Users\samko\Desktop\AI - super figters deluxe\\"+name, img, number)

标签: pythoncsvartificial-intelligencereal-timefile-conversion

解决方案


推荐阅读