mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-05 12:54:57 +00:00
Refactor YOLO results endpoint to handle empty results and improve database insertion logic
This commit is contained in:
@@ -162,19 +162,22 @@ def image():
|
|||||||
def yolo_results_endpoint():
|
def yolo_results_endpoint():
|
||||||
global yolo_results
|
global yolo_results
|
||||||
with lock:
|
with lock:
|
||||||
return jsonify(yolo_results)
|
if yolo_results:
|
||||||
try:
|
try:
|
||||||
data = json.loads(yolo_results)
|
data = json.loads(yolo_results)
|
||||||
db = get_db()
|
db = get_db()
|
||||||
with db.cursor() as cursor:
|
camera_data_tuples = [(item['class'], float(item['confidence'])) for item in data]
|
||||||
sql_yolo = "INSERT INTO image (class, confidence) VALUES (%s, %s)"
|
with db.cursor() as cursor:
|
||||||
cursor.executemany(sql_yolo, data)
|
sql_yolo = "INSERT INTO image (object, confidence) VALUES (%s, %s)"
|
||||||
db.commit()
|
cursor.executemany(sql_yolo, camera_data_tuples)
|
||||||
cursor.close()
|
db.commit()
|
||||||
except json.JSONDecodeError as e:
|
cursor.close()
|
||||||
print(f"JSON decode error: {e}")
|
except json.JSONDecodeError as e:
|
||||||
except mysql.connector.Error as err:
|
print(f"JSON decode error: {e}")
|
||||||
print(f"Database error: {err}")
|
except mysql.connector.Error as err:
|
||||||
|
print(f"Database error: {err}")
|
||||||
|
return jsonify(yolo_results)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user