Spaces:
Running
Running
updated image link
Browse files
app.py
CHANGED
|
@@ -7,7 +7,26 @@ import json
|
|
| 7 |
from collections import Counter
|
| 8 |
import base64
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
model_paths = [Path(f"model/fold_{i}_best.pt") for i in range(5)]
|
| 12 |
models = [YOLO(str(path)) for path in model_paths]
|
| 13 |
|
|
@@ -27,7 +46,6 @@ def get_label_info(name):
|
|
| 27 |
print(f"Label '{name}' not found. Available labels are: {list(labels_info.keys())}")
|
| 28 |
return ["Unknown", "Unknown"]
|
| 29 |
|
| 30 |
-
|
| 31 |
def classify_image(img, conf_threshold=0.25, iou_threshold=0.45):
|
| 32 |
predicted_classes = []
|
| 33 |
results_for_plot = None
|
|
@@ -57,19 +75,9 @@ def classify_image(img, conf_threshold=0.25, iou_threshold=0.45):
|
|
| 57 |
|
| 58 |
return Image.fromarray(annotated_img), waste_details
|
| 59 |
|
| 60 |
-
# ------------------------------------------------------------
|
| 61 |
-
#
|
| 62 |
-
# ------------------------------------------------------------
|
| 63 |
-
image_path = Path("hcw_classification.png")
|
| 64 |
-
if image_path.exists():
|
| 65 |
-
with open(image_path, "rb") as f:
|
| 66 |
-
img_b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 67 |
-
image_data_uri = f"data:image/png;base64,{img_b64}"
|
| 68 |
-
else:
|
| 69 |
-
# Fallback: a tiny 1x1 transparent pixel (in case the file is missing)
|
| 70 |
-
image_data_uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
|
| 71 |
-
# ------------------------------------------------------------
|
| 72 |
-
|
| 73 |
article_html = (
|
| 74 |
"<div style='display: flex; flex-wrap: wrap; gap: 30px; align-items: stretch;'>"
|
| 75 |
" <div style='flex: 2; min-width: 300px;'>"
|
|
@@ -107,12 +115,13 @@ article_html = (
|
|
| 107 |
" </div>"
|
| 108 |
" <div style='flex: 1; min-width: 250px; display: flex; flex-direction: column; align-items: center;'>"
|
| 109 |
" <figure style='margin:0; text-align:center;'>"
|
| 110 |
-
|
| 111 |
-
"
|
| 112 |
-
"
|
| 113 |
-
"
|
| 114 |
-
"
|
| 115 |
-
"
|
|
|
|
| 116 |
" </figure>"
|
| 117 |
" </div>"
|
| 118 |
"</div>"
|
|
@@ -135,17 +144,12 @@ article_html = (
|
|
| 135 |
"<a href='http://dwaste.live' target='_blank'>dwaste.live</a></p>"
|
| 136 |
)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
gr.Image(label="Result"),
|
| 145 |
-
gr.Dataframe(headers=["Name", "Type", "Color Code"], label="Details")
|
| 146 |
-
],
|
| 147 |
-
title="Health Care Waste Classification",
|
| 148 |
-
description=(
|
| 149 |
"DWaste uses lightweight AI to classify and sort health care waste (HCW) into various categories "
|
| 150 |
"according to the <strong>National Health Care Waste Management Standards and Operating Procedures of Nepal</strong> "
|
| 151 |
"color coding system to streamline waste management processes for healthcare facilities. "
|
|
@@ -153,8 +157,15 @@ iface = gr.Interface(
|
|
| 153 |
'<p><strong>Related Research:</strong> See our supporting study on <a href="https://www.researchgate.net/publication/399424815_Health_care_waste_classification_using_deep_learning_aligned_with_Nepal\'s_bin_color_guidelines" target="_blank">Kathmandu University Journal of Science, Engineering and Technology</a>.</p>'
|
| 154 |
"<p><strong>Disclaimer:</strong> This tool is for informational purposes only. Predictions made by the AI model "
|
| 155 |
"may not always be accurate. Please use the results cautiously and verify if necessary.</p>"
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
iface.launch()
|
|
|
|
| 7 |
from collections import Counter
|
| 8 |
import base64
|
| 9 |
|
| 10 |
+
# -----------------------------------------------------------------
|
| 11 |
+
# DEBUG: Check if the image file exists and encode it as base64
|
| 12 |
+
# -----------------------------------------------------------------
|
| 13 |
+
image_path = Path("hcw_classification.png")
|
| 14 |
+
print(f"DEBUG: Looking for image at {image_path.absolute()}")
|
| 15 |
|
| 16 |
+
if image_path.exists():
|
| 17 |
+
print(f"DEBUG: Image file found, size = {image_path.stat().st_size} bytes")
|
| 18 |
+
with open(image_path, "rb") as f:
|
| 19 |
+
img_b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 20 |
+
image_data_uri = f"data:image/png;base64,{img_b64}"
|
| 21 |
+
print(f"DEBUG: Base64 length = {len(image_data_uri)} characters")
|
| 22 |
+
else:
|
| 23 |
+
print("DEBUG: WARNING – image file NOT found, using fallback transparent pixel")
|
| 24 |
+
# fallback 1x1 transparent pixel
|
| 25 |
+
image_data_uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
|
| 26 |
+
|
| 27 |
+
# -----------------------------------------------------------------
|
| 28 |
+
# Load models and labels
|
| 29 |
+
# -----------------------------------------------------------------
|
| 30 |
model_paths = [Path(f"model/fold_{i}_best.pt") for i in range(5)]
|
| 31 |
models = [YOLO(str(path)) for path in model_paths]
|
| 32 |
|
|
|
|
| 46 |
print(f"Label '{name}' not found. Available labels are: {list(labels_info.keys())}")
|
| 47 |
return ["Unknown", "Unknown"]
|
| 48 |
|
|
|
|
| 49 |
def classify_image(img, conf_threshold=0.25, iou_threshold=0.45):
|
| 50 |
predicted_classes = []
|
| 51 |
results_for_plot = None
|
|
|
|
| 75 |
|
| 76 |
return Image.fromarray(annotated_img), waste_details
|
| 77 |
|
| 78 |
+
# -----------------------------------------------------------------
|
| 79 |
+
# Build the article HTML – using + concatenation to avoid f‑string issues
|
| 80 |
+
# -----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
article_html = (
|
| 82 |
"<div style='display: flex; flex-wrap: wrap; gap: 30px; align-items: stretch;'>"
|
| 83 |
" <div style='flex: 2; min-width: 300px;'>"
|
|
|
|
| 115 |
" </div>"
|
| 116 |
" <div style='flex: 1; min-width: 250px; display: flex; flex-direction: column; align-items: center;'>"
|
| 117 |
" <figure style='margin:0; text-align:center;'>"
|
| 118 |
+
# Insert the image using simple string concatenation – no f‑string
|
| 119 |
+
+ " <img src='" + image_data_uri + "' alt='HCW Classification Table Visualization' "
|
| 120 |
+
+ " style='height: 500px; width: auto; max-width: 100%; object-fit: contain; "
|
| 121 |
+
+ " border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);' />"
|
| 122 |
+
+ " <figcaption style='margin-top: 8px; font-style: italic; color: #555;'>"
|
| 123 |
+
+ " Color codes for waste segregation (based on Nepal's HCW standards)"
|
| 124 |
+
+ " </figcaption>"
|
| 125 |
" </figure>"
|
| 126 |
" </div>"
|
| 127 |
"</div>"
|
|
|
|
| 144 |
"<a href='http://dwaste.live' target='_blank'>dwaste.live</a></p>"
|
| 145 |
)
|
| 146 |
|
| 147 |
+
# -----------------------------------------------------------------
|
| 148 |
+
# Gradio interface using Blocks for reliable HTML insertion
|
| 149 |
+
# -----------------------------------------------------------------
|
| 150 |
+
with gr.Blocks() as iface:
|
| 151 |
+
gr.Markdown("# Health Care Waste Classification")
|
| 152 |
+
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
"DWaste uses lightweight AI to classify and sort health care waste (HCW) into various categories "
|
| 154 |
"according to the <strong>National Health Care Waste Management Standards and Operating Procedures of Nepal</strong> "
|
| 155 |
"color coding system to streamline waste management processes for healthcare facilities. "
|
|
|
|
| 157 |
'<p><strong>Related Research:</strong> See our supporting study on <a href="https://www.researchgate.net/publication/399424815_Health_care_waste_classification_using_deep_learning_aligned_with_Nepal\'s_bin_color_guidelines" target="_blank">Kathmandu University Journal of Science, Engineering and Technology</a>.</p>'
|
| 158 |
"<p><strong>Disclaimer:</strong> This tool is for informational purposes only. Predictions made by the AI model "
|
| 159 |
"may not always be accurate. Please use the results cautiously and verify if necessary.</p>"
|
| 160 |
+
)
|
| 161 |
+
with gr.Row():
|
| 162 |
+
with gr.Column():
|
| 163 |
+
input_img = gr.Image(type="pil", label="Upload Image")
|
| 164 |
+
with gr.Column():
|
| 165 |
+
output_img = gr.Image(label="Result")
|
| 166 |
+
output_df = gr.Dataframe(headers=["Name", "Type", "Color Code"], label="Details")
|
| 167 |
+
btn = gr.Button("Classify")
|
| 168 |
+
btn.click(fn=classify_image, inputs=[input_img], outputs=[output_img, output_df])
|
| 169 |
+
gr.HTML(article_html) # The article + image appears below the interactive area
|
| 170 |
|
| 171 |
iface.launch()
|