python - 如何将 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)
解决方案
推荐阅读
- c# - 如何在 DataList 中对饼图进行数据绑定
- python - 网格搜索分类的自定义评分功能
- javascript - 在 React 渲染输入中动态添加属性
- php - 使静态函数仅通过子类可见
- excel - Power Query(M语言)中如何引用动态列名
- jquery - 更改 PayPal 付款按钮
- jquery - 我可以在 VueJs 中使用 BS4+jQuery 模板吗
- vba - Excel VBA - CreateObject 和新 Internet Explorer 之间的区别
- java - 解析 JSON 中的错误
- android - 在“最近”屏幕上更改应用程序的预览图像