-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatency.py
79 lines (65 loc) · 2.33 KB
/
latency.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
"""
Run this script then
point the camera to look at the window,
watch the color flips between black and white.
Slightly increase "THRESHOLD" value if it doesn't flip.
"""
import cv2
import numpy as np
from subprocess import Popen, PIPE
import shlex
rtsp_url = 'rtsp://admin:[email protected]:554/Streaming/Channels/1/'
# rtsp_url = 'v4l2src'
gstreamer_exe = 'gst-launch-1.0'
width = 1280
height = 720
# Initialize USB webcam feed
# CAM_INDEX = 'rtsp://admin:[email protected]/4'
# Adjust this value if it doesn't flip. 0~255
THRESHOLD = 50
# Set up camera constants
IM_WIDTH = 1280
IM_HEIGHT = 720
# IM_WIDTH = 640
# IM_HEIGHT = 480
### USB webcam ###
# camera = cv2.VideoCapture(CAM_INDEX)
p = Popen(shlex.split(f'{gstreamer_exe} --quiet rtspsrc location={rtsp_url} latency=0 ! queue2 ! rtph264depay ! avdec_h264 ! videoconvert ! capsfilter caps="video/x-raw, format=BGR" ! fdsink'), stdout=PIPE)
# if ((camera == None) or (not camera.isOpened())):
# print('\n\n')
# print('Error - could not open video device.')
# print('\n\n')
# exit(0)
# camera.set(cv2.CAP_PROP_FRAME_WIDTH, IM_WIDTH)
# camera.set(cv2.CAP_PROP_FRAME_HEIGHT, IM_HEIGHT)
# save the actual dimensions
# actual_video_width = camera.get(cv2.CAP_PROP_FRAME_WIDTH)
# actual_video_height = camera.get(cv2.CAP_PROP_FRAME_HEIGHT)
# print('actual video resolution:{:.0f}x{:.0f}'.format(actual_video_width, actual_video_height))
prev_tick = cv2.getTickCount()
frame_number, prev_change_frame = 0, 0
is_dark = True
while True:
frame_number += 1
raw_image = p.stdout.read(width * height * 3)
frame = np.frombuffer(raw_image, np.uint8).reshape((height, width, 3))
# _, frame = camera.read()
#cv2.imshow("Frame2",frame)
img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
is_now_dark = np.average(img) < THRESHOLD
if is_dark != is_now_dark:
is_dark = is_now_dark
new = cv2.getTickCount()
print("{:.3f} sec, {:.3f} frames".format(
(new - prev_tick) / cv2.getTickFrequency(),
frame_number - prev_change_frame
))
prev_tick = new
prev_change_frame = frame_number
fill_color = 255 if is_dark else 0
show = np.full(img.shape, fill_color, dtype=img.dtype)
cv2.imshow('frame', show)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
p.release()
cv2.destroyAllWindows()