This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
forked from markrcote/eideticker
-
Notifications
You must be signed in to change notification settings - Fork 16
/
getdimensions.py
executable file
·163 lines (134 loc) · 5.89 KB
/
getdimensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
from PIL import ImageDraw
import eideticker
import json
import mozhttpd
import moznetwork
import os
import sys
import time
import videocapture
import videocapture.square as square
class CaptureServer(object):
finished = False
start_frame = None
end_frame = None
capture_controller = None
def __init__(self, capture_filename, options):
if options.capture:
self.capture_controller = videocapture.CaptureController(
capture_filename, options)
@mozhttpd.handlers.json_response
def start_capture(self, req):
if self.capture_controller:
self.capture_controller.start_capture()
self.start_frame = self.capture_controller.capture_framenum()
print "Start capture. Frame: %s. Time: %s" % (
self.start_frame, time.time())
return (200, {})
@mozhttpd.handlers.json_response
def end_capture(self, req):
if self.capture_controller:
self.end_frame = self.capture_controller.capture_framenum()
print "End capture. Frame: %s. Time: %s" % (
self.end_frame, time.time())
self.capture_controller.terminate_capture()
self.finished = True
return (200, {})
def convert_capture(self):
if self.capture_controller:
self.capture_controller.convert_capture(
self.start_frame, self.end_frame, create_webm=False)
CAPTURE_DIR = os.path.join(os.path.dirname(__file__), "../captures")
def run_capture(options, capture_filename):
device_prefs = eideticker.getDevicePrefs(options)
capture_server = CaptureServer(capture_filename, options)
host = moznetwork.get_ip()
docroot = eideticker.test.TEST_DIR
httpd = mozhttpd.MozHttpd(port=0, host=host, docroot=docroot, urlhandlers=[
{'method': 'GET',
'path': '/api/captures/start/?',
'function': capture_server.start_capture},
{'method': 'GET',
'path': '/api/captures/end/?',
'function': capture_server.end_capture}])
httpd.start(block=False)
print "Serving '%s' at %s:%s" % (
httpd.docroot, httpd.host, httpd.httpd.server_port)
device = eideticker.getDevice(**device_prefs)
url = "http://%s:%s/getdimensions.html" % (host, httpd.httpd.server_port)
device.executeCommand("tap", [100, 100])
if device_prefs['devicetype'] == 'android':
device.launchFennec(options.appname, url=url)
else:
if not options.wifi_settings_file:
print "WIFI settings file (see --help) required for B2G!"
sys.exit(1)
device.restartB2G()
device.connectWIFI(json.loads(open(options.wifi_settings_file).read()))
device.marionette.execute_script("window.location.href='%s';" % url)
while not capture_server.finished:
time.sleep(0.25)
capture_server.convert_capture()
httpd.stop()
def main(args=sys.argv[1:]):
usage = "usage: %prog [options] <app name>"
parser = eideticker.CaptureOptionParser(
usage=usage, capture_area_option=False)
parser.add_option("--no-capture", action="store_false",
dest="capture", default=True,
help="run through the test, but don't actually "
"capture anything")
parser.add_option("--capture-file", action="store",
type="string", dest="capture_filename",
help="Existing capture to analyze instead of running "
"test")
parser.add_option("--app-name", action="store",
type="string", dest="appname",
default="org.mozilla.fennec",
help="Specify an application name (android only)")
parser.add_option("--output-file", action="store",
type="string", dest="output_file",
help="Output the results to file")
parser.add_option("--output-screenshot", action="store",
type="string", dest="output_screenshot",
help="Output screenshot of a capture frame with capture "
"area overlayed")
options, args = parser.parse_args()
capture_filename = options.capture_filename
if not capture_filename:
if options.capture:
capture_filename = os.path.join(CAPTURE_DIR, "capture-test-%s.zip" %
time.time())
print "Capturing to file %s" % capture_filename
run_capture(options, capture_filename)
if not options.capture:
# we were just doing a test run through the steps here, we're done
return
print "Processing capture..."
capture = videocapture.Capture(capture_filename)
# create a difference. threshold differences above 32 to 255, then
# run our existing algorithm on it
framediff = capture.get_frame(0) - capture.get_frame(capture.num_frames-1)
for y,row in enumerate(framediff):
for x,px in enumerate(row):
if px[0] > 32 or px[1] > 32 or px[2] > 32:
framediff[y][x] = [255.0,255.0,255.0]
largest_square = square.get_biggest_square([255,255,255], framediff,
x_tolerance_min=100,
x_tolerance_max=100,
handle_multiple_scanlines=True)
if largest_square is not None:
print "Capture area: %s" % largest_square
if options.output_file:
with open(options.output_file, 'w+') as f:
f.write('CAPTURE_AREA=%s\n' % largest_square)
if options.output_screenshot:
im = capture.get_frame_image(int(capture.length / 2))
draw = ImageDraw.Draw(im)
draw.rectangle(largest_square, outline=(255, 0, 0))
im.save(options.output_screenshot)
else:
print "Couldn't find capture area"
sys.exit(1)
main()