From 967bc8247c1deef37d62dffc29736375c6b85787 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Tue, 14 Jan 2025 15:37:08 +0100 Subject: [PATCH] Refactor YOLO results handling by separating database insertion logic into a dedicated function --- src/Python/flask/web/app.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 97e2a11..98ec1ba 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -162,22 +162,17 @@ def image(): def yolo_results_endpoint(): global yolo_results with lock: - if yolo_results: - try: - data = json.loads(yolo_results) - db = get_db() - camera_data_tuples = [(item['class'], float(item['confidence'])) for item in data] - with db.cursor() as cursor: - sql_yolo = "INSERT INTO image (object, confidence) VALUES (%s, %s)" - cursor.executemany(sql_yolo, camera_data_tuples) - db.commit() - cursor.close() - except json.JSONDecodeError as e: - print(f"JSON decode error: {e}") - except mysql.connector.Error as err: - print(f"Database error: {err}") - return jsonify(yolo_results) + return jsonify(yolo_results) +def yolo_results_db(): + global yolo_results + db = get_db() + with db.cursor() as cursor: + sql_yolo = "INSERT INTO yolo_results (object, confidence) VALUES (%s, %s)" + yolo_tuples = [(result["class"], result["confidence"]) for result in yolo_results] + cursor.executemany(sql_yolo, yolo_tuples) + db.commit() + cursor.close() if __name__ == '__main__':