From 721180db261161f393d0f006ac4819d3acab811f Mon Sep 17 00:00:00 2001 From: Glydric Date: Thu, 1 Feb 2024 17:26:51 +0100 Subject: [PATCH] frame rate and fixes, with hot plugging --- .../machineLearning.controller.ts | 4 +- ImageRecognition/main.py | 13 +-- ImageRecognition/src/ObjectDetectionYOLO.py | 83 +++++++++++-------- docker-compose.yml | 16 ++-- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/Backend/src/app/machineLearning/machineLearning.controller.ts b/Backend/src/app/machineLearning/machineLearning.controller.ts index 3dd70ab..938cae2 100644 --- a/Backend/src/app/machineLearning/machineLearning.controller.ts +++ b/Backend/src/app/machineLearning/machineLearning.controller.ts @@ -124,9 +124,9 @@ export class MachineLearningController { ) { const date = new Date(); - console.log('Adding data to database'); + // console.log('Adding data to database'); await this.database.addData(new DataType(cameraId, date, null, file)); - console.log('Sending intrusion detection notification'); + // console.log('Sending intrusion detection notification'); await this.telegramApi.sendIntrusionDetectionNotification( cameraId, date, diff --git a/ImageRecognition/main.py b/ImageRecognition/main.py index fbbd5a1..f7052a3 100644 --- a/ImageRecognition/main.py +++ b/ImageRecognition/main.py @@ -7,18 +7,11 @@ initBufferSize(MAX) with concurrent.futures.ThreadPoolExecutor() as executor: - # args: (int, int) = [(i, i) for i in range(0, 8)] - args: (int, int) = [(i, i) for i in range(2, 4)] + args: (int, int) = [(i, i) for i in range(0, MAX)] + # args: (int, int) = [(i, i) for i in range(0, 1)] results = [executor.submit(detection, *arg) for arg in args] concurrent.futures.wait(results) - +# example # cam1 = "rtsp://192.168.1.41:80/ch0_0.264" -# cam2 = "rtsp://192.168.1.41:80/ch1_0.264" -# cam3 = "rtsp://192.168.1.41:80/ch2_0.264" -# cam4 = "rtsp://192.168.1.41:80/ch3_0.264" -# cam5 = "rtsp://192.168.1.41:80/ch4_0.264" -# cam6 = "rtsp://192.168.1.41:80/ch5_0.264" -# cam7 = "rtsp://192.168.1.41:80/ch6_0.264" -# cam8 = "rtsp://192.168.1.41:80/ch7_0.264" diff --git a/ImageRecognition/src/ObjectDetectionYOLO.py b/ImageRecognition/src/ObjectDetectionYOLO.py index 1d7b9bd..0cf49a9 100644 --- a/ImageRecognition/src/ObjectDetectionYOLO.py +++ b/ImageRecognition/src/ObjectDetectionYOLO.py @@ -13,13 +13,12 @@ MSE_VALUE = 105.80 last_sent_image = [] +frame_rate = 3 def post_request(camera_id: int, image): global last_sent_image - # image = cv2.resize(image, (0.5, 0.5)) - if last_sent_image[camera_id] is not None: # Resizing flat_last_sent_image = last_sent_image[camera_id].flatten() @@ -69,51 +68,63 @@ def detection(camera_id: int, _: int): print(f"capturing {camera_id}") - # FIXME camera capture takes to much time to load the camera connection (first time) - cap = cv2.VideoCapture(f"rtsp://192.168.1.41:80/ch{camera_id}_0.264") - cap.set(cv2.CAP_PROP_FPS, 5) + # cap = cv2.VideoCapture(f"rtsp://172.20.14.1:80/ch{camera_id}_0.264") + + while True: + cap = cv2.VideoCapture(f"rtsp://192.168.1.41:80/ch{camera_id}_0.264") + + prev_time = 0 + + id = 0 + + # TODO handle status + status = "offline" - # TODO handle status - status = "offline" + print(f"started {camera_id}") - print(f"started {camera_id}") + while cap.isOpened(): + success, img = cap.read() - while cap.isOpened(): - success, img = cap.read() + time_elapsed = time.time() - prev_time - if not success: - print(f"No frame {camera_id}") - continue + if time_elapsed <= 1.0 / frame_rate: + continue - print(f"Captured frame from {camera_id}") + prev_time = time.time() - # cv2.imshow(f"Camera {camera_id}", img) - # cv2.waitKey(0) - # cv2.destroyAllWindows() + if not success: + cap.release() + continue + print(f"Captured frame from {camera_id}") - status = "online" - results = model(img, stream=True, classes=0, conf=0.5, verbose=False) - foundPerson = False - # coordinates - for r in results: - boxes = r.boxes - for box in boxes: - # bounding box - x1, y1, x2, y2 = box.xyxy[0] - x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) - cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3) - # confidence - confidence = math.ceil((box.conf[0] * 100)) / 100 - print("Confidence --->", confidence) + status = "online" + results = model(img, stream=True, classes=0, conf=0.5, verbose=False) + foundPerson = False + # coordinates + for r in results: + boxes = r.boxes + for box in boxes: + # bounding box + x1, y1, x2, y2 = box.xyxy[0] + x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) + cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3) + # confidence + confidence = math.ceil((box.conf[0] * 100)) / 100 + print( + f" Person detected in {camera_id} with Confidence ---> {confidence}" + ) - foundPerson = True + foundPerson = True - if foundPerson: - _, image = cv2.imencode(".jpg", img) + if foundPerson: + _, image = cv2.imencode(".jpg", img) + # cv2.imwrite(f"img/test.{camera_id}_frame_{id}.jpeg", img) + cv2.imwrite(f"img/test.{camera_id}_frame.jpeg", img) + id += 1 - post_request(camera_id, image) + post_request(camera_id, image) - cap.release() + cap.release() def initBufferSize(size: int): diff --git a/docker-compose.yml b/docker-compose.yml index a323cb5..bf5507d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,20 +12,20 @@ services: env_file: - .secrets.yml - openvidu: - image: openvidu/openvidu-dev:2.29.0 - restart: on-failure # always - ports: - - "4443:4443" - env_file: - - ./Backend/.env-openvidu + # openvidu: + # image: openvidu/openvidu-dev:2.29.0 + # restart: on-failure # always + # ports: + # - "4443:4443" + # env_file: + # - ./Backend/.env-openvidu backend: container_name: backend image: node:21-alpine depends_on: - mongo - - openvidu + # - openvidu restart: on-failure # always ports: - "8080:8080"