ariankhalfani commited on
Commit
b4e7526
1 Parent(s): 6344cc8

Create database.py

Browse files
Files changed (1) hide show
  1. database.py +136 -0
database.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ from io import BytesIO
3
+ import base64
4
+
5
+ def create_connection(db_file):
6
+ """ Create a database connection to the SQLite database """
7
+ conn = None
8
+ try:
9
+ conn = sqlite3.connect(db_file)
10
+ return conn
11
+ except sqlite3.Error as e:
12
+ print(e)
13
+ return conn
14
+
15
+ def create_table(conn):
16
+ """ Create the results tables if they do not exist """
17
+ create_glaucoma_table_sql = """ CREATE TABLE IF NOT EXISTS results (
18
+ id integer PRIMARY KEY,
19
+ image blob,
20
+ cup_area real,
21
+ disk_area real,
22
+ rim_area real,
23
+ rim_to_disc_line_ratio real,
24
+ ddls_stage integer
25
+ ); """
26
+ create_cataract_table_sql = """ CREATE TABLE IF NOT EXISTS cataract_results (
27
+ id integer PRIMARY KEY,
28
+ image blob,
29
+ red_quantity real,
30
+ green_quantity real,
31
+ blue_quantity real,
32
+ stage text
33
+ ); """
34
+ try:
35
+ cursor = conn.cursor()
36
+ cursor.execute(create_glaucoma_table_sql)
37
+ cursor.execute(create_cataract_table_sql)
38
+ except sqlite3.Error as e:
39
+ print(e)
40
+
41
+ def save_prediction_to_db(image, cup_area, disk_area, rim_area, rim_to_disc_line_ratio, ddls_stage):
42
+ database = "glaucoma_results.db"
43
+ conn = create_connection(database)
44
+ if conn:
45
+ create_table(conn)
46
+ sql = ''' INSERT INTO results(image, cup_area, disk_area, rim_area, rim_to_disc_line_ratio, ddls_stage)
47
+ VALUES(?,?,?,?,?,?) '''
48
+ cur = conn.cursor()
49
+
50
+ # Convert the image to bytes
51
+ buffered = BytesIO()
52
+ image.save(buffered, format="PNG")
53
+ img_bytes = buffered.getvalue()
54
+
55
+ cur.execute(sql, (img_bytes, cup_area, disk_area, rim_area, rim_to_disc_line_ratio, ddls_stage))
56
+ conn.commit()
57
+ conn.close()
58
+
59
+ def fetch_all_data(conn, table_name):
60
+ """ Fetch all data from the given table """
61
+ cursor = conn.cursor()
62
+ cursor.execute(f"SELECT * FROM {table_name}")
63
+ rows = cursor.fetchall()
64
+ return rows
65
+
66
+ def get_db_data(db_path_glaucoma, db_path_cataract):
67
+ conn_glaucoma = create_connection(db_path_glaucoma)
68
+ conn_cataract = create_connection(db_path_cataract)
69
+
70
+ if conn_glaucoma and conn_cataract:
71
+ create_table(conn_glaucoma)
72
+ create_table(conn_cataract)
73
+ glaucoma_data = fetch_all_data(conn_glaucoma, "results")
74
+ cataract_data = fetch_all_data(conn_cataract, "cataract_results")
75
+ conn_glaucoma.close()
76
+ conn_cataract.close()
77
+ return glaucoma_data, cataract_data
78
+ else:
79
+ return [], []
80
+
81
+ def get_context_db_data(db_path_context):
82
+ conn = create_connection(db_path_context)
83
+ if conn:
84
+ cursor = conn.cursor()
85
+ cursor.execute("SELECT * FROM context_info")
86
+ context_data = cursor.fetchall()
87
+ conn.close()
88
+ return context_data
89
+ else:
90
+ return []
91
+
92
+ def format_db_data(glaucoma_data, cataract_data, context_data):
93
+ """ Format the database data for display """
94
+ formatted_data = ""
95
+
96
+ if not glaucoma_data and not cataract_data and not context_data:
97
+ return "No data available in the database."
98
+
99
+ if glaucoma_data:
100
+ headers = ["ID", "Image", "Cup Area", "Disk Area", "Rim Area", "Rim to Disc Line Ratio", "DDLS Stage"]
101
+ formatted_data += "<h2>Glaucoma Data</h2><table border='1'><tr>" + "".join([f"<th>{header}</th>" for header in headers]) + "</tr>"
102
+
103
+ for row in glaucoma_data:
104
+ image_html = "No image"
105
+ if row[1] is not None:
106
+ image = base64.b64encode(row[1]).decode('utf-8')
107
+ image_html = f"<img src='data:image/png;base64,{image}' width='100'/>"
108
+
109
+ formatted_data += "<tr>" + "".join([f"<td>{image_html if i == 1 else row[i]}</td>" for i in range(len(row))]) + "</tr>"
110
+
111
+ formatted_data += "</table>"
112
+
113
+ if cataract_data:
114
+ headers = ["ID", "Image", "Red Quantity", "Green Quantity", "Blue Quantity", "Stage"]
115
+ formatted_data += "<h2>Cataract Data</h2><table border='1'><tr>" + "".join([f"<th>{header}</th>" for header in headers]) + "</tr>"
116
+
117
+ for row in cataract_data:
118
+ image_html = "No image"
119
+ if row[1] is not None:
120
+ image = base64.b64encode(row[1]).decode('utf-8')
121
+ image_html = f"<img src='data:image/png;base64,{image}' width='100'/>"
122
+
123
+ formatted_data += "<tr>" + "".join([f"<td>{image_html if i == 1 else row[i]}</td>" for i in range(len(row))]) + "</tr>"
124
+
125
+ formatted_data += "</table>"
126
+
127
+ if context_data:
128
+ headers = ["ID", "Text Content"]
129
+ formatted_data += "<h2>Context Data</h2><table border='1'><tr>" + "".join([f"<th>{header}</th>" for header in headers]) + "</tr>"
130
+
131
+ for row in context_data:
132
+ formatted_data += "<tr>" + "".join([f"<td>{row[i]}</td>" for i in range(len(row))]) + "</tr>"
133
+
134
+ formatted_data += "</table>"
135
+
136
+ return formatted_data