Browse Source

demo1,one camera test,output photo and logg,enter exit

Curious 7 months ago
parent
commit
44d923b312

+ 3 - 0
test/test-yolov5-deepsort/AIDetector_pytorch.py

@@ -5,7 +5,10 @@ import onnxruntime as ort
 from utils.general import non_max_suppression, scale_coords
 from utils.BaseDetector import baseDet
 from utils.datasets import letterbox
+import logging
 
+logging.basicConfig(filename='detection_log.txt', level=logging.INFO, 
+                    format='%(asctime)s - %(message)s')
 
 class Detector(baseDet):
     def __init__(self):

+ 77 - 0
test/test-yolov5-deepsort/demo1.py

@@ -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()

+ 8 - 19
test/test-yolov5-deepsort/requirements.txt

@@ -1,24 +1,13 @@
-easydict==1.13
+python==3.8
 imutils==0.5.4
-ipdb==0.13.13
-matplotlib==3.2.2
-motmetrics==1.4.0
-numpy==1.21.6
-onnxruntime==1.14.1
+torch==1.10.1
+numpy==1.24.4
 onnxruntime_gpu==1.10.0
-opencv_python==4.1.2.30
-pafy==0.5.5
 pandas==1.1.5
-Pillow==9.5.0
-protobuf==4.24.2
+torchvision==0.11.2
 PyYAML==6.0.1
 requests==2.27.1
-scipy==1.4.1
-seaborn==0.11.0
-setuptools==58.0.4
-thop==0.1.1.post2209072238
-torch==1.10.1
-torchvision==0.11.2
-tqdm==4.41.0
-wandb==0.17.5
-python==3.7
+matplotlib==3.4.0
+easydict==1.13
+scipy==1.10.1
+tqdm==4.41.0