Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
b05b0e9
1
Parent(s):
882c701
Update app.py
Browse files
app.py
CHANGED
@@ -41,20 +41,17 @@ def predict_image(input_image, name, age, medical_record, sex):
|
|
41 |
raw_predictions = []
|
42 |
|
43 |
if results[0].boxes:
|
44 |
-
# Sort the results by confidence and take the highest confidence one
|
45 |
highest_confidence_result = max(results[0].boxes, key=lambda x: x.conf.item())
|
46 |
-
|
47 |
-
# Determine the label based on the class index
|
48 |
class_index = highest_confidence_result.cls.item()
|
49 |
if class_index == 0:
|
50 |
label = "Immature"
|
51 |
-
color = (0, 255, 255)
|
52 |
elif class_index == 1:
|
53 |
label = "Mature"
|
54 |
-
color = (255, 0, 0)
|
55 |
else:
|
56 |
label = "Normal"
|
57 |
-
color = (0, 255, 0)
|
58 |
|
59 |
confidence = highest_confidence_result.conf.item()
|
60 |
xmin, ymin, xmax, ymax = map(int, highest_confidence_result.xyxy[0])
|
@@ -74,10 +71,6 @@ def predict_image(input_image, name, age, medical_record, sex):
|
|
74 |
font_scale = 1.0
|
75 |
thickness = 2
|
76 |
|
77 |
-
# Calculate label background size
|
78 |
-
(text_width, text_height), baseline = cv2.getTextSize(f'{label} {confidence:.2f}', cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness)
|
79 |
-
cv2.rectangle(image_with_boxes, (xmin, ymin - text_height - baseline), (xmin + text_width, ymin), (0, 0, 0), cv2.FILLED)
|
80 |
-
|
81 |
# Put the label text with black background
|
82 |
cv2.putText(image_with_boxes, f'{label} {confidence:.2f}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), thickness)
|
83 |
|
@@ -102,8 +95,7 @@ def predict_image(input_image, name, age, medical_record, sex):
|
|
102 |
def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
103 |
draw = ImageDraw.Draw(image)
|
104 |
|
105 |
-
|
106 |
-
font_size = 24 # Example font size
|
107 |
try:
|
108 |
font = ImageFont.truetype("font.ttf", size=font_size)
|
109 |
except IOError:
|
@@ -111,17 +103,12 @@ def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
|
111 |
print("Error: cannot open resource, using default font.")
|
112 |
|
113 |
text = f"Name: {name}, Age: {age}, Medical Record: {medical_record}, Sex: {sex}, Result: {label}"
|
114 |
-
|
115 |
-
# Calculate text bounding box
|
116 |
-
text_bbox = draw.textbbox((0, 0), text, font=font)
|
117 |
-
text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
|
118 |
-
text_x = 20
|
119 |
-
text_y = 40
|
120 |
padding = 10
|
121 |
|
122 |
# Draw a filled rectangle for the background
|
123 |
draw.rectangle(
|
124 |
-
[text_x - padding, text_y - padding, text_x +
|
125 |
fill="black"
|
126 |
)
|
127 |
|
@@ -145,20 +132,16 @@ def save_patient_info_to_html(name, age, medical_record, sex, result):
|
|
145 |
</html>
|
146 |
"""
|
147 |
|
148 |
-
# Save HTML content to file
|
149 |
html_file_path = os.path.join(tempfile.gettempdir(), 'patient_info.html')
|
150 |
with open(html_file_path, 'w') as f:
|
151 |
f.write(html_content)
|
152 |
|
153 |
return html_file_path
|
154 |
|
155 |
-
# Function to download the folders
|
156 |
def download_folder(folder_path):
|
157 |
zip_path = os.path.join(tempfile.gettempdir(), f"{Path(folder_path).name}.zip")
|
158 |
-
|
159 |
-
# Zip the folder
|
160 |
shutil.make_archive(zip_path.replace('.zip', ''), 'zip', folder_path)
|
161 |
-
|
162 |
return zip_path
|
163 |
|
164 |
# Gradio Interface
|
@@ -187,16 +170,18 @@ with gr.Blocks() as demo:
|
|
187 |
download_uploaded_btn = gr.Button("Download Uploaded Images")
|
188 |
download_predicted_btn = gr.Button("Download Predicted Images")
|
189 |
|
190 |
-
# Add file download output components for the uploaded and predicted images
|
191 |
patient_info_file = gr.File(label="Patient Information HTML File")
|
192 |
uploaded_folder_file = gr.File(label="Uploaded Images Zip File")
|
193 |
predicted_folder_file = gr.File(label="Predicted Images Zip File")
|
194 |
|
195 |
-
#
|
|
|
|
|
|
|
196 |
submit_btn.click(fn=predict_image, inputs=[name, age, medical_record, sex, input_image], outputs=[output_image, raw_result])
|
197 |
download_html_btn.click(fn=save_patient_info_to_html, inputs=[name, age, medical_record, sex, raw_result], outputs=patient_info_file)
|
198 |
-
download_uploaded_btn.click(fn=download_folder, inputs=[
|
199 |
-
download_predicted_btn.click(fn=download_folder, inputs=[
|
200 |
|
201 |
# Launch Gradio app
|
202 |
demo.launch()
|
|
|
41 |
raw_predictions = []
|
42 |
|
43 |
if results[0].boxes:
|
|
|
44 |
highest_confidence_result = max(results[0].boxes, key=lambda x: x.conf.item())
|
|
|
|
|
45 |
class_index = highest_confidence_result.cls.item()
|
46 |
if class_index == 0:
|
47 |
label = "Immature"
|
48 |
+
color = (0, 255, 255)
|
49 |
elif class_index == 1:
|
50 |
label = "Mature"
|
51 |
+
color = (255, 0, 0)
|
52 |
else:
|
53 |
label = "Normal"
|
54 |
+
color = (0, 255, 0)
|
55 |
|
56 |
confidence = highest_confidence_result.conf.item()
|
57 |
xmin, ymin, xmax, ymax = map(int, highest_confidence_result.xyxy[0])
|
|
|
71 |
font_scale = 1.0
|
72 |
thickness = 2
|
73 |
|
|
|
|
|
|
|
|
|
74 |
# Put the label text with black background
|
75 |
cv2.putText(image_with_boxes, f'{label} {confidence:.2f}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), thickness)
|
76 |
|
|
|
95 |
def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
96 |
draw = ImageDraw.Draw(image)
|
97 |
|
98 |
+
font_size = 24
|
|
|
99 |
try:
|
100 |
font = ImageFont.truetype("font.ttf", size=font_size)
|
101 |
except IOError:
|
|
|
103 |
print("Error: cannot open resource, using default font.")
|
104 |
|
105 |
text = f"Name: {name}, Age: {age}, Medical Record: {medical_record}, Sex: {sex}, Result: {label}"
|
106 |
+
text_x, text_y = 20, 40
|
|
|
|
|
|
|
|
|
|
|
107 |
padding = 10
|
108 |
|
109 |
# Draw a filled rectangle for the background
|
110 |
draw.rectangle(
|
111 |
+
[text_x - padding, text_y - padding, text_x + 500, text_y + 30 + padding],
|
112 |
fill="black"
|
113 |
)
|
114 |
|
|
|
132 |
</html>
|
133 |
"""
|
134 |
|
|
|
135 |
html_file_path = os.path.join(tempfile.gettempdir(), 'patient_info.html')
|
136 |
with open(html_file_path, 'w') as f:
|
137 |
f.write(html_content)
|
138 |
|
139 |
return html_file_path
|
140 |
|
141 |
+
# Function to download the folders
|
142 |
def download_folder(folder_path):
|
143 |
zip_path = os.path.join(tempfile.gettempdir(), f"{Path(folder_path).name}.zip")
|
|
|
|
|
144 |
shutil.make_archive(zip_path.replace('.zip', ''), 'zip', folder_path)
|
|
|
145 |
return zip_path
|
146 |
|
147 |
# Gradio Interface
|
|
|
170 |
download_uploaded_btn = gr.Button("Download Uploaded Images")
|
171 |
download_predicted_btn = gr.Button("Download Predicted Images")
|
172 |
|
|
|
173 |
patient_info_file = gr.File(label="Patient Information HTML File")
|
174 |
uploaded_folder_file = gr.File(label="Uploaded Images Zip File")
|
175 |
predicted_folder_file = gr.File(label="Predicted Images Zip File")
|
176 |
|
177 |
+
# Use gr.State to hold folder paths
|
178 |
+
uploaded_folder_state = gr.State(str(uploaded_folder))
|
179 |
+
predicted_folder_state = gr.State(str(predicted_folder))
|
180 |
+
|
181 |
submit_btn.click(fn=predict_image, inputs=[name, age, medical_record, sex, input_image], outputs=[output_image, raw_result])
|
182 |
download_html_btn.click(fn=save_patient_info_to_html, inputs=[name, age, medical_record, sex, raw_result], outputs=patient_info_file)
|
183 |
+
download_uploaded_btn.click(fn=download_folder, inputs=[uploaded_folder_state], outputs=uploaded_folder_file)
|
184 |
+
download_predicted_btn.click(fn=download_folder, inputs=[predicted_folder_state], outputs=predicted_folder_file)
|
185 |
|
186 |
# Launch Gradio app
|
187 |
demo.launch()
|