我写了一个Python脚本来收集两种数据。首先是视频,然后是Mouse的动作。我只执行屏幕捕获脚本即可获得Performance(我可以通过它观看YouTube视频)。因此,我假设使用MouseEvents脚本或主脚本中的其他命令来降低它的速度。我尝试了cProfile来查找性能问题,但是我也没有真正找到错误。 我的屏幕捕获脚本:
import cv2
import numpy as np
from mss import mss
#pip install --upgrade mss
class Record():
def __init__(self,monitor):
#Init all Values and Activate Video Capture
self.screen = mss()
self.monitor = {'top': monitor[0], 'left': monitor[1], 'width': monitor[2], 'height': monitor[3]}
print(self.monitor)
def getFrame(self):
#Read the frame and add them to the array
#_,img = self.frames.read()
img = np.array(self.screen.grab(self.monitor))
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
return img
class IniRecord():
def __init__(self,monitor):
self.r = Record(monitor)
def getFrame(self):
frame = self.r.getFrame()
return frame
#monitor = [71, 183, 841, 492]
#while True:
# k = IniRecord(monitor)
# view = k.getFrame()
#
# cv2.imshow('window',view)
#
#
# if cv2.waitKey(25) & 0xFF == ord('q'):
# cv2.destroyAllWindows()
# break
#
我的MouseEvents脚本:
import win32api as win
import win32con
import win32gui
class Mouse():
def getEvents(self):
curs_pos = win32gui.GetCursorPos()
if(win.GetAsyncKeyState(0x01) != 0):
click = 1
else:
click = 0
return (curs_pos[0],curs_pos[1],click)
主要代码:
import numpy as np
from PIL import ImageGrab
import cv2
import time
import os
import h5py
import ScreenRecorder
#import getKeys
import MouseEvents
class Data():
def __init__(self,monitor):
self.record_box = monitor
#Creating all neccesarry Numpy Arrays
self.video = np.empty((1,self.record_box[3],self.record_box[2]))
#self.key_strokes = np.empty((1,))
self.mouse_events = np.empty((1,3))
#Init all Other Addons
self.sc_rc = ScreenRecorder.IniRecord(self.record_box)
self.mouse = MouseEvents.Mouse()
def getFrame(self):
return self.sc_rc.getFrame()
def getKeyStrokes(self):
pass
def getMouseEvents(self):
return np.array(self.mouse.getEvents())
def collect(self):
self.frame = self.getFrame()
self.frame = np.expand_dims(self.frame,0)
self.mouse_ev = self.getMouseEvents()
self.mouse_ev = np.expand_dims(self.mouse_ev,0)
self.video = np.append(self.video,self.frame,0)
self.mouse_events = np.append(self.mouse_events,self.mouse_ev,0)
return self.video,self.mouse_events,self.frame
monitor = [0,0,1080,720]
pos = 1
while True:
#Getting the Position to Record
time.sleep(1)
x,y,c = np.array(MouseEvents.Mouse().getEvents())
if(c==1 and pos == 1):
print('pos1')
#Need to convert to int otherwise Screen Recoder gets problems
monitor[0] = int(x)
monitor[1] = int(y)
pos = 2
time.sleep(1)
elif(c==1 and pos == 2):
print('pos2')
#Getting the length and width of the Recorind area
monitor[2] = int(x-monitor[0])
monitor[3] = int(y-monitor[1])
break
d = Data(monitor)
while True:
video,mouse,frame= d.collect()
frame = np.squeeze(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
os.chdir('data')
length = os.listdir()
day =time.localtime()
tag = "{}_{}_{}_{}_{}".format(day[2],day[1],day[0],day[3],day[4])
with h5py.File('video_data_{}'.format(tag), 'w') as hf:
hf.create_dataset("video", data=video,compression='gzip')
hf.create_dataset("mouse", data = mouse)
break