yyyyyt commited on
Commit
8af2987
·
verified ·
1 Parent(s): 4f44903

Delete test_camera_fps.py

Browse files
Files changed (1) hide show
  1. test_camera_fps.py +0 -85
test_camera_fps.py DELETED
@@ -1,85 +0,0 @@
1
- import cv2
2
- import time
3
- import sys
4
-
5
- def test_camera(device_id, width=640, height=480, fps=30):
6
- print(f"\n=== Testing Camera Index {device_id} ===")
7
-
8
- # 优先使用 V4L2 后端
9
- backend = cv2.CAP_V4L2 if hasattr(cv2, 'CAP_V4L2') else cv2.CAP_ANY
10
- backend_name = "V4L2" if backend == cv2.CAP_V4L2 else "ANY"
11
- print(f"Opening with backend: {backend_name}")
12
-
13
- cap = cv2.VideoCapture(device_id, backend)
14
-
15
- if not cap.isOpened():
16
- print(f"Error: Could not open camera {device_id}")
17
- return
18
-
19
- # 1. 强制 MJPG
20
- print("Attempting to set MJPG format...")
21
- cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
22
-
23
- # 2. 设置 FPS 和 分辨率
24
- cap.set(cv2.CAP_PROP_FPS, fps)
25
- cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
26
- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
27
-
28
- # 3. 关闭自动曝光 (Linux V4L2 特有,尝试通过 OpenCV 属性设置)
29
- # 0.25 通常对应 'Manual',但在不同驱动下可能不同,这里仅作为尝试
30
- # cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25)
31
-
32
- # 4. 读取实际生效的设置
33
- actual_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
34
- actual_h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
35
- actual_fps = cap.get(cv2.CAP_PROP_FPS)
36
- fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
37
- codec = "".join([chr((fourcc >> 8 * i) & 0xFF) for i in range(4)])
38
-
39
- print(f"----------------------------------------")
40
- print(f"Requested: {width}x{height} @ {fps} FPS, MJPG")
41
- print(f"Actual : {actual_w}x{actual_h} @ {actual_fps} FPS, Codec: {codec}")
42
- print(f"----------------------------------------")
43
-
44
- if codec.upper() != "MJPG":
45
- print("WARNING: Codec is NOT MJPG! This is likely the cause of low FPS.")
46
-
47
- print("Warming up camera (skip 10 frames)...")
48
- for _ in range(10):
49
- cap.read()
50
-
51
- print(f"Starting capture loop for 100 frames...")
52
- count = 0
53
- start_time = time.time()
54
-
55
- while count < 100:
56
- ret, frame = cap.read()
57
- if not ret:
58
- print("Error: Failed to read frame during loop")
59
- break
60
- count += 1
61
- # 显示简略进度
62
- if count % 10 == 0:
63
- sys.stdout.write(f"{count}..")
64
- sys.stdout.flush()
65
-
66
- end_time = time.time()
67
- duration = end_time - start_time
68
- avg_fps = count / duration
69
-
70
- print(f"\n\nRESULT: Captured {count} frames in {duration:.2f} seconds.")
71
- print(f"AVERAGE FPS: {avg_fps:.2f}")
72
-
73
- cap.release()
74
-
75
- if __name__ == "__main__":
76
- if len(sys.argv) < 2:
77
- print("Usage: python test_camera_fps.py <device_id> [width] [height] [fps]")
78
- print("Example: python backend/scripts/test_camera_fps.py 0 640 480 30")
79
- else:
80
- dev_id = int(sys.argv[1])
81
- w = int(sys.argv[2]) if len(sys.argv) > 2 else 640
82
- h = int(sys.argv[3]) if len(sys.argv) > 3 else 480
83
- f = int(sys.argv[4]) if len(sys.argv) > 4 else 30
84
- test_camera(dev_id, w, h, f)
85
-