| 
					
				 | 
			
			
				@@ -0,0 +1,77 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import os 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import gc 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import cv2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import imutils 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import threading 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from AIDetector_pytorch import Detector 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+# 用于控制检测线程退出的事件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+stop_event = threading.Event() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def start_camera_detector(camera_id, detector, output_directory): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    name = 'Demo Camera {}'.format(camera_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    #cap = cv2.VideoCapture(camera_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    cap = cv2.VideoCapture(camera_id, cv2.CAP_V4L2) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if not cap.isOpened(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        print('Error: Unable to open camera {}.'.format(camera_id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    fps = cap.get(cv2.CAP_PROP_FPS) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if fps <= 0: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        fps = 30 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    t = int(1000 / fps) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    print(f'{name} fps:', fps) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    frame_count = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    while not stop_event.is_set():  # 检查停止事件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ret, im = cap.read() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if not ret or im is None: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            break 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if frame_count % 3 == 0: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            result = detector.feedCap(im) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            result_frame = result['frame'] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            result_frame = imutils.resize(result_frame, height=500) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # Save the result image 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            filename = os.path.join(output_directory, f'camera_{camera_id}_frame_{frame_count}.jpg') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            cv2.imwrite(filename, result_frame) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            print(f'Saved image: {filename}') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        frame_count += 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if frame_count % 30 == 0: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            gc.collect() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    cap.release() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+def main(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    detector = Detector() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    output_directory = 'output_images' 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    os.makedirs(output_directory, exist_ok=True)  # Create output directory if not exists 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    threads = [] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    #for i in range(1):  # camera 数量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    i=1  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    thread = threading.Thread(target=start_camera_detector, args=(i, detector, output_directory)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    thread.start() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    threads.append(thread) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    # 等待用户输入停止程序 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    input("Press Enter to stop the camera detection...\n") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    # 设置停止事件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    stop_event.set() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    # 等待所有线程结束 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for thread in threads: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        thread.join() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+if __name__ == "__main__": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    main() 
			 |