From e20f0311a029e2e08907ce80dc3324ab6b369c60 Mon Sep 17 00:00:00 2001 From: YiHungWONG <40423264+yihong1120@users.noreply.github.com> Date: Fri, 9 Aug 2024 03:51:06 +0000 Subject: [PATCH] Reduce complexity --- examples/YOLOv8_server_api/detection.py | 31 ++++++++++++++++------- examples/streaming_web/static/js/label.js | 7 ++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/examples/YOLOv8_server_api/detection.py b/examples/YOLOv8_server_api/detection.py index 627b772..6963f5f 100644 --- a/examples/YOLOv8_server_api/detection.py +++ b/examples/YOLOv8_server_api/detection.py @@ -178,11 +178,7 @@ def find_overlapping_indices(index1, indices2, datas, threshold): Returns: set: Indices of overlapping labels to remove. """ - to_remove = set() - for index2 in indices2: - if calculate_overlap(datas[index1][:4], datas[index2][:4]) > threshold: - to_remove.add(index2) - return to_remove + return {index2 for index2 in indices2 if calculate_overlap(datas[index1][:4], datas[index2][:4]) > threshold} def calculate_overlap(bbox1, bbox2): @@ -313,8 +309,25 @@ def find_contained_indices(index1, indices2, datas): """ to_remove = set() for index2 in indices2: - if is_contained(datas[index2][:4], datas[index1][:4]): - to_remove.add(index2) - elif is_contained(datas[index1][:4], datas[index2][:4]): - to_remove.add(index1) + to_remove.update(check_containment(index1, index2, datas)) + return to_remove + + +def check_containment(index1, index2, datas): + """ + Check if one bounding box is contained within another. + + Args: + index1 (int): The first index. + index2 (int): The second index. + datas (list): Detection data. + + Returns: + set: Indices of contained labels to remove. + """ + to_remove = set() + if is_contained(datas[index2][:4], datas[index1][:4]): + to_remove.add(index2) + elif is_contained(datas[index1][:4], datas[index2][:4]): + to_remove.add(index1) return to_remove \ No newline at end of file diff --git a/examples/streaming_web/static/js/label.js b/examples/streaming_web/static/js/label.js index cc0cb89..5dc6b79 100644 --- a/examples/streaming_web/static/js/label.js +++ b/examples/streaming_web/static/js/label.js @@ -1,6 +1,11 @@ $(document).ready(() => { // Automatically detect the current page protocol to decide between ws and wss const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://'; + // Ensure io is defined + if (typeof io === 'undefined') { + console.error('Socket.IO is not defined. Please ensure it is included in your HTML.'); + return; + } // Create WebSocket connection and configure reconnection strategy const socket = io.connect(protocol + document.domain + ':' + location.port, { transports: ['websocket'], @@ -82,7 +87,7 @@ function createCameraDiv({ image, imageName, label }) { * @param {...any} messages - The messages to log */ function debugLog(...messages) { - if (process.env.NODE_ENV === 'development') { + if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'development') { console.log(...messages); } } \ No newline at end of file