Eth*_*mas 6 python screenshot artificial-intelligence python-3.x
我需要为 Python 脚本(一些机器学习、AI 与视频游戏)截取游戏截图,并且我被引导到 FRAPS。FRAPS之类的软件可以随心所欲地截取屏幕截图——但据我所知,我需要知道要命名的文件,更重要的是,我需要将其与其他屏幕截图区分开来或删除这些屏幕截图一旦我完成了他们。FRAPS 的命名系统(以及 Bandicam 的)要求我知道拍摄时间,精确到千分之一秒(据我所知)。该程序将在动态的、对时间敏感的情况下运行,所以我只想有一个像我在 Greenshot 中可以做的命名方案(也就是说,除非很容易找到要保存的文件,否则我不感兴趣在游戏过程中的卷积文件搜索中,否则我可能会接受。拍摄 FRAPS 游戏视频已经足够滞后,会因随机错误而破坏游戏中脚本同步的功能)。我既关心游戏过程中的一般系统性能,又关心算法响应能力。毕竟,我已经知道下一步是在屏幕上查找图像或其中的一部分,这会很费力。
我会对任何可以将图像文件(我一直计划使用 .png,但任何无损文件类型都可以)作为图像放入程序目录的软件、模块或代码感到满意。将它保存到剪贴板也可以,因为它可以从那里保存和操作。无论如何,我对大多数这些屏幕截图都没有长期使用。如果它可以只截取屏幕的一部分,那也是可取的。
我怀疑这是否重要,但这里有一些关于我的程序外观的精简伪代码。我真的不需要任何故障排除。
import os,sys,subprocess,mss,mss.tools,time as sleepy,keyboard,sys,pyscreenshot as Grabby
from random import randint as spinny
from PIL import Image
from pyautogui import * # Just to help with what I've been using, a lot of these aren't currently in use
class nameOfGame:
magicNumbers = {'whereStuffIs': (500,500),'generalDelay':0.05,'Hotkey':'f10','fileName':'lmno.png'}
def __init__(self,magicNumbers):
os.startfile(gameLocation)
def Play(self):
if(self.goAhead()):
moveTo(self.magicNumbers['whereStuffIs'])
click(self.magicNumbers['whereStuffIs']) #How do I format this correctly on StackOverflow?
def ScreenShot(self):
if(softWareScreenSshot):# Outside software is getting the screenshot
press(self.magicNumbers['Hotkey'])
else: # Python module or other method...?
PseudoModule.takeAScreenshot()
load(self.magicNumbers['fileName'])
def goAhead(self):
return True # This is a placeholder for AI.
Run Code Online (Sandbox Code Playgroud)
人们通常如何获得这种人工智能?如果可能的话,我不想为此使用任何更高级别的编码,因为我主要是将其作为个人挑战来做。到目前为止,我已经学到了很多东西,我不希望程序为我做所有事情,只是屏幕截图。不过,为此,如果有办法解决这个问题,我不介意编写脚本。但是,根据我所读到的内容,我认为我需要在游戏运行时进入游戏才能做到这一点,这可能实用,也可能不实用。提前致谢!
我发现这个问题相当老了,但仍然没有答案。您可以使用 python 库mss来抓取屏幕。正如我所看到的,您已经导入了它,但没有使用它。
import time
import cv2
import mss
import numpy
with mss.mss() as sct:
# Part of the screen to capture
monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
while "Screen capturing":
last_time = time.time()
# Get raw pixels from the screen, save it to a Numpy array
img = numpy.array(sct.grab(monitor))
# Display the picture
cv2.imshow("OpenCV/Numpy normal", img)
# Display the picture in grayscale
# cv2.imshow('OpenCV/Numpy grayscale',
# cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))
print("fps: {}".format(1 / (time.time() - last_time)))
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
Run Code Online (Sandbox Code Playgroud)
正如我所看到的,您还找到了PyAutoGui。它有一个定位功能来查找屏幕上的区域,但速度相当慢。他们在文档中写道
在 1920 x 1080 屏幕上,定位函数调用大约需要 1 或 2 秒。
我认为通过将屏幕截图缩小到感兴趣的区域,您可以加快速度。
如果您需要更多信息,那里有很多教程。例如,这是一个使用 Python 玩 GTA V 的git 存储库。请参阅相关 YouTube 视频和文章的链接。