-
Notifications
You must be signed in to change notification settings - Fork 2
/
latency_test.py
executable file
·65 lines (53 loc) · 1.94 KB
/
latency_test.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
import time
from pylab import *
from numpy.random import *
import cv2
import cv
if __name__ == "__main__":
# Parameters
CAM = 0
H_SIZE = 640
V_SIZE = 480
DISP_H = 1280
DISP_V = 1024
THRESHOLD = 100
N_SAMPLES = 100
WINDOW = 30
# Open camera
cap = cv2.VideoCapture(CAM)
cap.set(cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
retval, frame = cap.read()
# Open target window
target = zeros((DISP_H, DISP_V, 3), uint8)
cv2.imshow("target", target)
cv2.waitKey(1)
# Allow user to adjust camera
while True:
retval, frame = cap.read()
cv2.imshow("camera", frame)
if cv2.waitKey(1) > 0:
break;
cv2.destroyWindow("camera") # close camera preview window
cv2.waitKey(1)
# Run latency test
times = ones(WINDOW)
samples = randint(0, V_SIZE, (N_SAMPLES, 2))
while True:
retval, frame = cap.read() # get original camera frame
c0 = frame[tuple(samples.T)] # sample starting colors
target = ~target # invert display
cv2.imshow("target", target)
cv2.waitKey(1)
start = time.time() # start timer
color_distance = 0;
while color_distance < THRESHOLD:
retval, frame = cap.read()
c = frame[tuple(samples.T)] # sample current colors
color_distance = sum(sum((c0-c)**2, axis=1))/N_SAMPLES
#print "Color Distance: " + str(color_distance) + "\n"
end = time.time() # stop timer
times = append(times[1:end], end-start) # update moving average
print "Latency: %f" % (sum(times)/times.size) # print average latency
if cv2.waitKey(5) > 0:
break;