Delete test_dual_cameras_fps.py
Browse files- test_dual_cameras_fps.py +0 -88
test_dual_cameras_fps.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import threading
|
| 3 |
-
import time
|
| 4 |
-
import sys
|
| 5 |
-
|
| 6 |
-
def capture_loop(device_id, width, height, fps, results, index):
|
| 7 |
-
print(f"[Cam {device_id}] Starting initialization...")
|
| 8 |
-
|
| 9 |
-
backend = cv2.CAP_V4L2 if hasattr(cv2, 'CAP_V4L2') else cv2.CAP_ANY
|
| 10 |
-
cap = cv2.VideoCapture(device_id, backend)
|
| 11 |
-
|
| 12 |
-
if not cap.isOpened():
|
| 13 |
-
print(f"[Cam {device_id}] FAILED to open.")
|
| 14 |
-
results[index] = 0.0
|
| 15 |
-
return
|
| 16 |
-
|
| 17 |
-
# 强制 MJPG
|
| 18 |
-
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
|
| 19 |
-
cap.set(cv2.CAP_PROP_FPS, fps)
|
| 20 |
-
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
|
| 21 |
-
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
|
| 22 |
-
|
| 23 |
-
# 检查实际 Codec
|
| 24 |
-
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
|
| 25 |
-
codec = "".join([chr((fourcc >> 8 * i) & 0xFF) for i in range(4)])
|
| 26 |
-
actual_fps = cap.get(cv2.CAP_PROP_FPS)
|
| 27 |
-
print(f"[Cam {device_id}] Initialized. Codec: {codec}, Target: {fps}, Actual Report: {actual_fps}")
|
| 28 |
-
|
| 29 |
-
# 热身
|
| 30 |
-
for _ in range(10):
|
| 31 |
-
cap.read()
|
| 32 |
-
|
| 33 |
-
print(f"[Cam {device_id}] Start capturing 100 frames...")
|
| 34 |
-
|
| 35 |
-
count = 0
|
| 36 |
-
start_time = time.time()
|
| 37 |
-
|
| 38 |
-
while count < 100:
|
| 39 |
-
ret, _ = cap.read()
|
| 40 |
-
if ret:
|
| 41 |
-
count += 1
|
| 42 |
-
else:
|
| 43 |
-
time.sleep(0.001)
|
| 44 |
-
|
| 45 |
-
end_time = time.time()
|
| 46 |
-
cap.release()
|
| 47 |
-
|
| 48 |
-
duration = end_time - start_time
|
| 49 |
-
avg_fps = count / duration
|
| 50 |
-
results[index] = avg_fps
|
| 51 |
-
print(f"[Cam {device_id}] FINISHED. Avg FPS: {avg_fps:.2f}")
|
| 52 |
-
|
| 53 |
-
def test_dual_cameras(dev1, dev2):
|
| 54 |
-
print(f"=== Testing Dual Cameras Concurrent: {dev1} & {dev2} ===")
|
| 55 |
-
print("NOTE: If total bandwidth exceeds USB limit, FPS will drop.\n")
|
| 56 |
-
|
| 57 |
-
results = [0.0, 0.0]
|
| 58 |
-
|
| 59 |
-
t1 = threading.Thread(target=capture_loop, args=(dev1, 640, 480, 30, results, 0))
|
| 60 |
-
t2 = threading.Thread(target=capture_loop, args=(dev2, 640, 480, 30, results, 1))
|
| 61 |
-
|
| 62 |
-
# 同时启动
|
| 63 |
-
t1.start()
|
| 64 |
-
t2.start()
|
| 65 |
-
|
| 66 |
-
t1.join()
|
| 67 |
-
t2.join()
|
| 68 |
-
|
| 69 |
-
print("\n=== FINAL RESULTS ===")
|
| 70 |
-
print(f"Camera {dev1}: {results[0]:.2f} FPS")
|
| 71 |
-
print(f"Camera {dev2}: {results[1]:.2f} FPS")
|
| 72 |
-
|
| 73 |
-
if results[0] < 20 or results[1] < 20:
|
| 74 |
-
print("\n[!] LOW FPS DETECTED.")
|
| 75 |
-
print("Possible causes:")
|
| 76 |
-
print("1. USB Bandwidth saturation (Try plugging into different USB ports/hubs).")
|
| 77 |
-
print("2. Low light condition triggering auto-exposure lag.")
|
| 78 |
-
print("3. One camera fell back to YUYV instead of MJPG.")
|
| 79 |
-
else:
|
| 80 |
-
print("\n[OK] Both cameras running smoothly.")
|
| 81 |
-
|
| 82 |
-
if __name__ == "__main__":
|
| 83 |
-
if len(sys.argv) < 3:
|
| 84 |
-
print("Usage: python backend/scripts/test_dual_cameras_fps.py <id1> <id2>")
|
| 85 |
-
print("Example: python backend/scripts/test_dual_cameras_fps.py 0 2")
|
| 86 |
-
else:
|
| 87 |
-
test_dual_cameras(int(sys.argv[1]), int(sys.argv[2]))
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|