-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtsp_urls.py
More file actions
70 lines (60 loc) · 1.96 KB
/
rtsp_urls.py
File metadata and controls
70 lines (60 loc) · 1.96 KB
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
import subprocess, time, numpy as np
# CONFIG
RTSP = "rtsp://127.0.0.1:8554/stream1" # change if needed
WIDTH = 640
HEIGHT = 360
CHANNELS = 3 # rgb24
FRAME_BYTES = WIDTH * HEIGHT * CHANNELS
DURATION = 30 # seconds
CONV_ITERS = 4 # make workload heavier
# FFmpeg command: decode to raw RGB24
cmd = [
"ffmpeg",
"-hide_banner", "-loglevel", "error",
"-rtsp_transport", "tcp",
"-i", RTSP,
"-vf", f"scale={WIDTH}:{HEIGHT}",
"-f", "rawvideo",
"-pix_fmt", "rgb24",
"-"
]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
frame_count = 0
dropped = 0
proc_times = []
start = time.time()
def grayscale_and_conv(rgb_buf, w, h):
arr = np.frombuffer(rgb_buf, dtype=np.uint8).reshape((h, w, 3))
gray = (0.299*arr[:,:,0] + 0.587*arr[:,:,1] + 0.114*arr[:,:,2]).astype(np.float32)
kernel = np.array([[1,2,1],[2,4,2],[1,2,1]], dtype=np.float32)
ksum = kernel.sum()
for _ in range(CONV_ITERS):
padded = np.pad(gray, ((1,1),(1,1)), mode='edge')
out = np.empty_like(gray)
for y in range(h):
for x in range(w):
region = padded[y:y+3, x:x+3]
out[y,x] = (region * kernel).sum() / ksum
gray = out
return float(gray[0,0])
try:
while time.time() - start < DURATION:
raw = proc.stdout.read(FRAME_BYTES)
if not raw or len(raw) < FRAME_BYTES:
dropped += 1
time.sleep(0.002)
continue
t0 = time.time()
_ = grayscale_and_conv(raw, WIDTH, HEIGHT)
t1 = time.time()
frame_count += 1
proc_times.append(t1 - t0)
finally:
proc.kill()
elapsed = time.time() - start
fps = frame_count / elapsed if elapsed > 0 else 0.0
avg_time = sum(proc_times)/len(proc_times) if proc_times else 0.0
print("Python -> Frames:", frame_count)
print("Python -> Dropped:", dropped)
print(f"Python -> FPS: {fps:.2f}")
print(f"Python -> Avg process time/frame: {avg_time:.4f}s")