diff --git a/alpr_api.py b/alpr_api.py index 8a07908..086ce01 100644 --- a/alpr_api.py +++ b/alpr_api.py @@ -2,6 +2,7 @@ import json import os import sys import threading +import time from time import sleep import ultimateAlprSdk @@ -126,6 +127,7 @@ def create_rest_server_flask(): def alpr(domain, module): # Only care about the ALPR endpoint if domain == 'image' and module == 'alpr': + interference = time.time() if 'upload' not in request.files: return jsonify({'error': 'No image found'}) @@ -134,9 +136,65 @@ def create_rest_server_flask(): return jsonify({'error': 'No selected file'}) image = Image.open(image) - result = process_image(image) - result = convert_to_cpai_compatible(result) + result = convert_to_cpai_compatible(process_image(image)) + if len(result['predictions']) == 0: + print("No plate found in the image, trying to split the image") + + predictions_found = [] + + width, height = image.size + cell_width = width // 3 + cell_height = height // 3 + + # Define which cells to process (2, 4, 5, 6, 8, 9) + cells_to_process = [2, 4, 5, 6, 8, 9] + + # Loop through each cell + for cell_index in range(1, 10): + # Calculate row and column of the cell + row = (cell_index - 1) // 3 + col = (cell_index - 1) % 3 + + # Calculate bounding box of the cell + left = col * cell_width + upper = row * cell_height + right = left + cell_width + lower = upper + cell_height + + # Check if this cell should be processed + if cell_index in cells_to_process: + # Extract the cell as a new image + cell_image = image.crop((left, upper, right, lower)) + + result_cell = json.loads(process_image(cell_image)) + + if 'plates' in result_cell: + for plate in result_cell['plates']: + warpedBox = plate['warpedBox'] + x_coords = warpedBox[0::2] + y_coords = warpedBox[1::2] + x_min = min(x_coords) + left + x_max = max(x_coords) + left + y_min = min(y_coords) + upper + y_max = max(y_coords) + upper + + predictions_found.append({ + 'confidence': plate['confidences'][0] / 100, + 'label': "Plate: " + plate['text'], + 'plate': plate['text'], + 'x_min': x_min, + 'x_max': x_max, + 'y_min': y_min, + 'y_max': y_max + }) + + if len(predictions_found) > 0: + # add the prediction with the highest confidence + result['predictions'].append(max(predictions_found, key=lambda x: x['confidence'])) + + result['processMs'] = round((time.time() - interference) * 1000, 2) + result['inferenceMs'] = result['processMs'] # same as processMs return jsonify(result) else: return jsonify({'error': 'Endpoint not implemented'}), 404 diff --git a/static/logo.webp b/static/logo_black.webp similarity index 100% rename from static/logo.webp rename to static/logo_black.webp diff --git a/static/logo_white.webp b/static/logo_white.webp new file mode 100644 index 0000000..4b7ed5e Binary files /dev/null and b/static/logo_white.webp differ diff --git a/static/styles.css b/static/styles.css deleted file mode 100644 index 686cd2c..0000000 --- a/static/styles.css +++ /dev/null @@ -1,25 +0,0 @@ -body { - font-family: 'Arial', sans-serif; - background-color: #f4f4f9; - margin: 0; - padding: 0; -} - -.container { - background: #fff; - padding: 20px; - border-radius: 10px; - box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); - max-width: 600px; - margin: auto; -} - -h1, h2 { - color: #333; -} - -pre { - background-color: #f8f9fa; - border: 1px solid #ddd; - border-radius: 5px; -} diff --git a/templates/index.html b/templates/index.html index 926f7ab..aa1e433 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,44 +5,45 @@