Skip to content

Commit

Permalink
Reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Aug 9, 2024
1 parent ed992d3 commit 47677e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
51 changes: 43 additions & 8 deletions examples/YOLOv8_server_api/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,27 @@ def find_overlaps(indices1, indices2, datas, threshold):
"""
to_remove = set()
for index1 in indices1:
for index2 in indices2:
if calculate_overlap(datas[index1][:4], datas[index2][:4]) > threshold:
to_remove.add(index2)
to_remove.update(find_overlapping_indices(index1, indices2, datas, threshold))
return to_remove


def find_overlapping_indices(index1, indices2, datas, threshold):
"""
Find overlapping indices for a given index.
Args:
index1 (int): The index to check for overlaps.
indices2 (list): The list of indices to check against.
datas (list): Detection data.
threshold (float): Overlap 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


Expand Down Expand Up @@ -277,9 +295,26 @@ def find_contained_labels(indices1, indices2, datas):
"""
to_remove = set()
for index1 in indices1:
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(find_contained_indices(index1, indices2, datas))
return to_remove


def find_contained_indices(index1, indices2, datas):
"""
Find contained indices for a given index.
Args:
index1 (int): The index to check for containment.
indices2 (list): The list of indices to check against.
datas (list): Detection data.
Returns:
set: Indices of completely contained labels to remove.
"""
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)
return to_remove
24 changes: 16 additions & 8 deletions examples/streaming_web/static/js/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ $(document).ready(() => {
const currentPageLabel = $('h1').text(); // Assuming the <h1> tag contains the current label name

socket.on('connect', () => {
console.log('WebSocket connected!');
debugLog('WebSocket connected!');
});

socket.on('connect_error', (error) => {
console.error('WebSocket connection error:', error);
debugLog('WebSocket connection error:', error);
});

socket.on('reconnect_attempt', () => {
console.log('Attempting to reconnect...');
debugLog('Attempting to reconnect...');
});

socket.on('update', (data) => {
Expand All @@ -36,10 +36,10 @@ $(document).ready(() => {
function handleUpdate(data, currentPageLabel) {
// Check if the received data is applicable to the current page's label
if (data.label === currentPageLabel) {
console.log('Received update for current label:', data.label);
debugLog('Received update for current label:', data.label);
updateCameraGrid(data);
} else {
console.log('Received update for different label:', data.label);
debugLog('Received update for different label:', data.label);
}
}

Expand All @@ -52,7 +52,6 @@ function updateCameraGrid(data) {
data.images.forEach((image, index) => {
const cameraData = {
image: image,
index: index,
imageName: data.image_names[index],
label: data.label
};
Expand All @@ -66,15 +65,24 @@ function updateCameraGrid(data) {
* Create a camera div element
* @param {Object} cameraData - The data for creating the camera div
* @param {string} cameraData.image - The image data
* @param {number} cameraData.index - The image index
* @param {string} cameraData.imageName - The image name
* @param {string} cameraData.label - The label name
* @returns {HTMLElement} - The div element containing the image and title
*/
function createCameraDiv({ image, index, imageName, label }) {
function createCameraDiv({ image, imageName, label }) {
const cameraDiv = $('<div>').addClass('camera');
const title = $('<h2>').text(imageName);
const img = $('<img>').attr('src', `data:image/png;base64,${image}`).attr('alt', `${label} image`);
cameraDiv.append(title).append(img);
return cameraDiv[0];
}

/**
* Log messages for debugging purposes
* @param {...any} messages - The messages to log
*/
function debugLog(...messages) {
if (process.env.NODE_ENV === 'development') {
console.log(...messages);
}
}

0 comments on commit 47677e4

Please sign in to comment.