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 handling by separating database insertion logic into a dedicated function
This commit is contained in:
@@ -162,22 +162,17 @@ def image():
|
|||||||
def yolo_results_endpoint():
|
def yolo_results_endpoint():
|
||||||
global yolo_results
|
global yolo_results
|
||||||
with lock:
|
with lock:
|
||||||
if yolo_results:
|
return jsonify(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)
|
|
||||||
|
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user