Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
5a203eb
1
Parent(s):
e65f39b
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ import pandas as pd
|
|
12 |
# Load YOLOv8 model
|
13 |
model = YOLO("best.pt")
|
14 |
|
15 |
-
def predict_image(input_image, name,
|
16 |
if input_image is None:
|
17 |
return None, "Please Input The Image"
|
18 |
|
@@ -73,7 +73,7 @@ def predict_image(input_image, name, patient_id):
|
|
73 |
pil_image_with_boxes = Image.fromarray(image_with_boxes)
|
74 |
|
75 |
# Add text and watermark
|
76 |
-
pil_image_with_boxes = add_text_and_watermark(pil_image_with_boxes, name,
|
77 |
|
78 |
return pil_image_with_boxes, raw_predictions_str
|
79 |
|
@@ -103,7 +103,7 @@ def add_watermark(image):
|
|
103 |
return image
|
104 |
|
105 |
# Function to add text and watermark
|
106 |
-
def add_text_and_watermark(image, name,
|
107 |
draw = ImageDraw.Draw(image)
|
108 |
|
109 |
# Load a larger font (adjust the size as needed)
|
@@ -114,7 +114,7 @@ def add_text_and_watermark(image, name, patient_id, label):
|
|
114 |
font = ImageFont.load_default()
|
115 |
print("Error: cannot open resource, using default font.")
|
116 |
|
117 |
-
text = f"Name: {name},
|
118 |
|
119 |
# Calculate text bounding box
|
120 |
text_bbox = draw.textbbox((0, 0), text, font=font)
|
@@ -142,12 +142,12 @@ def init_db():
|
|
142 |
conn = sqlite3.connect('results.db')
|
143 |
c = conn.cursor()
|
144 |
c.execute('''CREATE TABLE IF NOT EXISTS results
|
145 |
-
(id INTEGER PRIMARY KEY, name TEXT,
|
146 |
conn.commit()
|
147 |
conn.close()
|
148 |
|
149 |
# Function to submit result to the database
|
150 |
-
def submit_result(name,
|
151 |
conn = sqlite3.connect('results.db')
|
152 |
c = conn.cursor()
|
153 |
|
@@ -160,8 +160,8 @@ def submit_result(name, patient_id, input_image, predicted_image, result):
|
|
160 |
_, predicted_buffer = cv2.imencode('.png', predicted_image_rgb)
|
161 |
predicted_image_bytes = predicted_buffer.tobytes()
|
162 |
|
163 |
-
c.execute("INSERT INTO results (name,
|
164 |
-
(name,
|
165 |
conn.commit()
|
166 |
conn.close()
|
167 |
return "Result submitted to database."
|
@@ -170,12 +170,12 @@ def submit_result(name, patient_id, input_image, predicted_image, result):
|
|
170 |
def view_database():
|
171 |
conn = sqlite3.connect('results.db')
|
172 |
c = conn.cursor()
|
173 |
-
c.execute("SELECT name,
|
174 |
rows = c.fetchall()
|
175 |
conn.close()
|
176 |
|
177 |
# Convert to pandas DataFrame for better display in Gradio
|
178 |
-
df = pd.DataFrame(rows, columns=["Name", "
|
179 |
|
180 |
return df
|
181 |
|
@@ -203,12 +203,12 @@ def download_file(choice):
|
|
203 |
init_db()
|
204 |
|
205 |
# Gradio Interface
|
206 |
-
def interface(name,
|
207 |
if input_image is None:
|
208 |
return "Please upload an image."
|
209 |
|
210 |
-
output_image, raw_result = predict_image(input_image, name,
|
211 |
-
submit_status = submit_result(name,
|
212 |
|
213 |
return output_image, raw_result, submit_status
|
214 |
|
@@ -236,7 +236,9 @@ with gr.Blocks() as demo:
|
|
236 |
|
237 |
with gr.Column():
|
238 |
name = gr.Textbox(label="Name")
|
239 |
-
|
|
|
|
|
240 |
input_image = gr.Image(type="pil", label="Upload an Image", image_mode="RGB")
|
241 |
|
242 |
with gr.Column():
|
@@ -247,7 +249,7 @@ with gr.Blocks() as demo:
|
|
247 |
raw_result = gr.Textbox(label="Raw Result", lines=5)
|
248 |
submit_status = gr.Textbox(label="Submission Status")
|
249 |
|
250 |
-
submit_btn.click(fn=interface, inputs=[name,
|
251 |
|
252 |
with gr.Column():
|
253 |
view_db_btn = gr.Button("View Database")
|
|
|
12 |
# Load YOLOv8 model
|
13 |
model = YOLO("best.pt")
|
14 |
|
15 |
+
def predict_image(input_image, name, age, medical_record, sex):
|
16 |
if input_image is None:
|
17 |
return None, "Please Input The Image"
|
18 |
|
|
|
73 |
pil_image_with_boxes = Image.fromarray(image_with_boxes)
|
74 |
|
75 |
# Add text and watermark
|
76 |
+
pil_image_with_boxes = add_text_and_watermark(pil_image_with_boxes, name, age, medical_record, sex, label)
|
77 |
|
78 |
return pil_image_with_boxes, raw_predictions_str
|
79 |
|
|
|
103 |
return image
|
104 |
|
105 |
# Function to add text and watermark
|
106 |
+
def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
107 |
draw = ImageDraw.Draw(image)
|
108 |
|
109 |
# Load a larger font (adjust the size as needed)
|
|
|
114 |
font = ImageFont.load_default()
|
115 |
print("Error: cannot open resource, using default font.")
|
116 |
|
117 |
+
text = f"Name: {name}, Age: {age}, Medical Record: {medical_record}, Sex: {sex}, Result: {label}"
|
118 |
|
119 |
# Calculate text bounding box
|
120 |
text_bbox = draw.textbbox((0, 0), text, font=font)
|
|
|
142 |
conn = sqlite3.connect('results.db')
|
143 |
c = conn.cursor()
|
144 |
c.execute('''CREATE TABLE IF NOT EXISTS results
|
145 |
+
(id INTEGER PRIMARY KEY, name TEXT, age INTEGER, medical_record INTEGER, sex TEXT, input_image BLOB, predicted_image BLOB, result TEXT)''')
|
146 |
conn.commit()
|
147 |
conn.close()
|
148 |
|
149 |
# Function to submit result to the database
|
150 |
+
def submit_result(name, age, medical_record, sex, input_image, predicted_image, result):
|
151 |
conn = sqlite3.connect('results.db')
|
152 |
c = conn.cursor()
|
153 |
|
|
|
160 |
_, predicted_buffer = cv2.imencode('.png', predicted_image_rgb)
|
161 |
predicted_image_bytes = predicted_buffer.tobytes()
|
162 |
|
163 |
+
c.execute("INSERT INTO results (name, age, medical_record, sex, input_image, predicted_image, result) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
164 |
+
(name, age, medical_record, sex, input_image_bytes, predicted_image_bytes, result))
|
165 |
conn.commit()
|
166 |
conn.close()
|
167 |
return "Result submitted to database."
|
|
|
170 |
def view_database():
|
171 |
conn = sqlite3.connect('results.db')
|
172 |
c = conn.cursor()
|
173 |
+
c.execute("SELECT name, age, medical_record, sex, input_image, predicted_image, result FROM results")
|
174 |
rows = c.fetchall()
|
175 |
conn.close()
|
176 |
|
177 |
# Convert to pandas DataFrame for better display in Gradio
|
178 |
+
df = pd.DataFrame(rows, columns=["Name", "Age", "Medical Record", "Sex", "Input Image", "Predicted Image", "Result"])
|
179 |
|
180 |
return df
|
181 |
|
|
|
203 |
init_db()
|
204 |
|
205 |
# Gradio Interface
|
206 |
+
def interface(name, age, medical_record, sex, input_image):
|
207 |
if input_image is None:
|
208 |
return "Please upload an image."
|
209 |
|
210 |
+
output_image, raw_result = predict_image(input_image, name, age, medical_record, sex)
|
211 |
+
submit_status = submit_result(name, age, medical_record, sex, input_image, output_image, raw_result)
|
212 |
|
213 |
return output_image, raw_result, submit_status
|
214 |
|
|
|
236 |
|
237 |
with gr.Column():
|
238 |
name = gr.Textbox(label="Name")
|
239 |
+
age = gr.Number(label="Age")
|
240 |
+
medical_record = gr.Number(label="Medical Record")
|
241 |
+
sex = gr.Radio(["Male", "Female"], label="Sex")
|
242 |
input_image = gr.Image(type="pil", label="Upload an Image", image_mode="RGB")
|
243 |
|
244 |
with gr.Column():
|
|
|
249 |
raw_result = gr.Textbox(label="Raw Result", lines=5)
|
250 |
submit_status = gr.Textbox(label="Submission Status")
|
251 |
|
252 |
+
submit_btn.click(fn=interface, inputs=[name, age, medical_record, sex, input_image], outputs=[output_image, raw_result, submit_status])
|
253 |
|
254 |
with gr.Column():
|
255 |
view_db_btn = gr.Button("View Database")
|