Skip to content

Instantly share code, notes, and snippets.

@tommy-mor
Last active August 3, 2021 17:28
Show Gist options
  • Save tommy-mor/8b50bb7d42f01b09942b449687344b84 to your computer and use it in GitHub Desktop.
Save tommy-mor/8b50bb7d42f01b09942b449687344b84 to your computer and use it in GitHub Desktop.
script to automate Photosensitive Epilepsy Analysis Tool (PEAT) using pywinauto
usage:
put a bunch of .avi files in folder called DIR
```
pip install pywinauto
python automate.py DIR
```
output:
this will output a bunch of html files in DIR with results for each file.
from pywinauto import Desktop, Application
import time
import sys
import os
windowname = ".* - UW Trace Center Photosensitive Epilepsy Analysis Tool (PEAT) (Version 1.6)"
# connect to peat process that is already running.
# make sure that you are running this program as administrator so that it can attach
app = Application(backend="uia").connect(path='PEAT.exe', title=windowname)
window = app.window(title_re=".* - UW Trace Center .*")
# rewind the video to 000:00:00 so that we can find the timer text element on following line
window.descendants(title='Rewind')[0].click_input()
timerelement = window.descendants(title="000:00.00")[0] # this will reset to 0 when analysis is done, so we can tell we are finished
def open_file(fname):
window.menu_select("File->Open Video Clip")
# replace "drop currently unsaved progress" dialog
blocker = window.descendants(title="OK")
if blocker:
blocker[0].click()
file_dlg = window.children(title="Open video clip")[0]
edit = file_dlg.descendants(title="File name:", control_type="Edit")[0]
edit.set_edit_text(fname)
file_dlg.children(title="Open", control_type="Button")[0].click()
def save_report(fname):
print('saving report')
window.menu_select("File->Save HTML Report")
file_dlg = window.descendants(title="Save HTML report file")[0]
edit = file_dlg.descendants(title="File name:", control_type="Edit")[0]
edit.set_edit_text(fname.replace('avi','htm'))
file_dlg.children(title="Save", control_type="Button")[0].click()
# detect "replace file" dialog
confirm = window.descendants(title="Yes")
if confirm:
confirm[0].click()
def analyze_file(fname):
open_file(fname)
window.menu_select("Analysis->Analyze Video")
print('starting analysis of {}'.format(fname))
while True:
time.sleep(1)
# wait until timer element reaches 0 again, which hapens when analysis is done
if timerelement.window_text() == "000:00.00":
print('done with analysis of {}'.format(fname))
break
save_report(fname)
if __name__ == '__main__':
# reads the given dir. reads all avi files.
# if an avi file has already been analyzised (there exists a corresponding htm file, skip it)
if len(sys.argv) == 2:
dir = sys.argv[1]
else:
raise Exception("need a directory cli arg")
htmlfiles = set()
avidirs = []
for dirpath, b, filenames in os.walk(dir):
for f in filenames:
abspath = os.path.abspath(os.path.join(dirpath, f))
withoutexn = os.path.splitext(abspath)[0]
if f.endswith('avi'):
avidirs.append(abspath)
elif f.endswith('htm'):
htmlfiles.add(withoutexn)
for avipath in avidirs:
if os.path.splitext(avipath)[0] in htmlfiles:
print('skipping {}, we already analyzed/exported that one'.format(avipath))
else:
analyze_file(avipath)
print('done')
import sys
import os
def generate_report(dir, logfile):
with open(logfile, 'w') as f:
f.write('filename, passing\n')
def read_file(fname, shortname):
with open(fname, 'r') as f:
content = f.read()
if 'Passed' in content:
passing = True
elif 'Failed' in content:
passing = False
else:
raise Exception("invalid file")
assert passing is not None
with open(logfile, 'a') as f:
f.write(shortname + ', ' + str(passing) + '\n')
for dirpath, _, filenames in os.walk(dir):
for f in filenames:
if f.endswith('htm'):
read_file(os.path.abspath(os.path.join(dirpath, f)), f.replace('.htm', ''))
print('done')
if __name__ == '__main__':
# reads the given dir. reads all avi files.
# if an avi file has already been analyzised (there exists a corresponding htm file, skip it)
if len(sys.argv) == 3:
dir = sys.argv[1]
logfile = sys.argv[2]
assert logfile.endswith('.csv')
generate_report(dir, logfile)
else:
raise Exception("need a directory cli arg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment