Spaces:
Runtime error
Runtime error
Commit
Β·
4aae61a
1
Parent(s):
d5a6eb2
initial
Browse files- README.md +1 -12
- Thalassemia.pt +3 -0
- app.py +217 -0
- bestluk.pt +3 -0
- blood_group/clot_classifier_model.h5 +3 -0
- clot_classifier_model.h5 +3 -0
- packages.txt +3 -0
- requirements.txt +6 -0
- sickle_cell_model_mobnetv2.h5 +3 -0
README.md
CHANGED
@@ -1,12 +1 @@
|
|
1 |
-
|
2 |
-
title: Blooddetection
|
3 |
-
emoji: π
|
4 |
-
colorFrom: pink
|
5 |
-
colorTo: blue
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.31.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# blood-group-disease-identification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thalassemia.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d0f7dbccb3d7e70b0855857c104aebe7651046fca6548979a0b55f1412cf1e0d
|
3 |
+
size 2965121
|
app.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.preprocessing import image as keras_image
|
6 |
+
from ultralytics import YOLO
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# === Load Models ===
|
10 |
+
clot_model = load_model("blood_group/clot_classifier_model.h5")
|
11 |
+
sickle_model = load_model("sickle_cell_model_mobnetv2.h5")
|
12 |
+
thalassemia_model = YOLO("Thalassemia.pt") # YOLOv8 classifier
|
13 |
+
import pathlib
|
14 |
+
temp = pathlib.PosixPath
|
15 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
16 |
+
|
17 |
+
leukemia_model = torch.hub.load("ultralytics/yolov5", "custom", path="bestluk.pt", force_reload=True)
|
18 |
+
|
19 |
+
# Restore PosixPath
|
20 |
+
pathlib.PosixPath = temp
|
21 |
+
|
22 |
+
# === Clot Detection ===
|
23 |
+
def is_clotted(img):
|
24 |
+
img = img.convert("RGB").resize((128, 128))
|
25 |
+
arr = keras_image.img_to_array(img)
|
26 |
+
arr = arr.astype("float32") / 255.0
|
27 |
+
arr = np.expand_dims(arr, axis=0)
|
28 |
+
pred = clot_model.predict(arr)[0][0]
|
29 |
+
return pred < 0.5 # True = clotted
|
30 |
+
|
31 |
+
def predict_clot(image):
|
32 |
+
try:
|
33 |
+
if image is None:
|
34 |
+
return "β No image uploaded."
|
35 |
+
label = "Clotted" if is_clotted(image) else "Non-Clotted"
|
36 |
+
return f"π©Έ Clot Detection: *{label}*"
|
37 |
+
except Exception as e:
|
38 |
+
return f"β Error: {str(e)}"
|
39 |
+
|
40 |
+
# === Sickle Cell Detection ===
|
41 |
+
def predict_sickle(image):
|
42 |
+
try:
|
43 |
+
if image is None:
|
44 |
+
return "β No image uploaded."
|
45 |
+
img = image.convert("RGB").resize((128, 128))
|
46 |
+
arr = keras_image.img_to_array(img)
|
47 |
+
arr = arr.astype("float32") / 255.0
|
48 |
+
arr = np.expand_dims(arr, axis=0)
|
49 |
+
pred = sickle_model.predict(arr)[0][0]
|
50 |
+
label = "Normal" if pred >= 0.5 else "Sickle Cell"
|
51 |
+
confidence = pred if pred >= 0.5 else 1 - pred
|
52 |
+
return f"𧬠Sickle Cell Detection: *{label}*\nConfidence: {confidence:.2f}"
|
53 |
+
except Exception as e:
|
54 |
+
return f"β Error: {str(e)}"
|
55 |
+
|
56 |
+
# === Blood Group Identification ===
|
57 |
+
def split_into_three_drops(image):
|
58 |
+
width, height = image.size
|
59 |
+
third = width // 3
|
60 |
+
return (
|
61 |
+
image.crop((0, 0, third, height)),
|
62 |
+
image.crop((third, 0, 2 * third, height)),
|
63 |
+
image.crop((2 * third, 0, width, height))
|
64 |
+
)
|
65 |
+
|
66 |
+
def determine_blood_group(a_clot, b_clot, d_clot):
|
67 |
+
if a_clot and b_clot:
|
68 |
+
base = "AB"
|
69 |
+
elif a_clot:
|
70 |
+
base = "A"
|
71 |
+
elif b_clot:
|
72 |
+
base = "B"
|
73 |
+
else:
|
74 |
+
base = "O"
|
75 |
+
rh = "+" if d_clot else "-"
|
76 |
+
return base + rh
|
77 |
+
|
78 |
+
def detect_blood_group(image):
|
79 |
+
try:
|
80 |
+
drop_a, drop_b, drop_d = split_into_three_drops(image)
|
81 |
+
a_clot = is_clotted(drop_a)
|
82 |
+
b_clot = is_clotted(drop_b)
|
83 |
+
d_clot = is_clotted(drop_d)
|
84 |
+
group = determine_blood_group(a_clot, b_clot, d_clot)
|
85 |
+
return (
|
86 |
+
drop_a, drop_b, drop_d,
|
87 |
+
f"π§ͺ Blood Group: *{group}*\n\nClotting:\n- Anti-A: {'Yes' if a_clot else 'No'}\n- Anti-B: {'Yes' if b_clot else 'No'}\n- Anti-D: {'Yes' if d_clot else 'No'}"
|
88 |
+
)
|
89 |
+
except Exception as e:
|
90 |
+
return None, None, None, f"β Error: {str(e)}"
|
91 |
+
|
92 |
+
# === Thalassemia Classification (YOLOv8) ===
|
93 |
+
def predict_thalassemia(image):
|
94 |
+
try:
|
95 |
+
results = thalassemia_model(image, save=False)[0]
|
96 |
+
top_class_idx = int(results.probs.top1)
|
97 |
+
top_class_label = thalassemia_model.names[top_class_idx]
|
98 |
+
confidence = results.probs.data[top_class_idx]
|
99 |
+
return f"𧬠Thalassemia Classification: *{top_class_label}*\nConfidence: {confidence:.2f}"
|
100 |
+
except Exception as e:
|
101 |
+
return f"β Error: {str(e)}"
|
102 |
+
|
103 |
+
# === Leukemia Detection (YOLOv5) ===
|
104 |
+
class_info = {
|
105 |
+
'Benign': "Stage: Benign cells. No immediate concern, monitor routinely.",
|
106 |
+
'Early': "Stage: Early leukemia detected. Please consult a hematologist.",
|
107 |
+
'Pre': "Stage: Pre-leukemic condition. Immediate medical consultation recommended.",
|
108 |
+
'Pro': "Stage: Pro-leukemic (progressed). Urgent specialist intervention required."
|
109 |
+
}
|
110 |
+
|
111 |
+
def predict_leukemia(image):
|
112 |
+
try:
|
113 |
+
image_path = "temp_leukemia.jpg"
|
114 |
+
image.save(image_path)
|
115 |
+
|
116 |
+
results = leukemia_model(image_path)
|
117 |
+
predictions = results.pandas().xyxy[0]
|
118 |
+
|
119 |
+
if predictions.empty:
|
120 |
+
return "𧬠No abnormal cells detected. Please check image quality."
|
121 |
+
|
122 |
+
labels = predictions['name'].value_counts().to_dict()
|
123 |
+
report = "𧬠*Leukemia Detection Summary*:\n"
|
124 |
+
for cls, count in labels.items():
|
125 |
+
report += f"\n- *{cls}*: {count} cell(s)\n β {class_info.get(cls, 'No info available.')}"
|
126 |
+
return report
|
127 |
+
|
128 |
+
except Exception as e:
|
129 |
+
return f"β Error: {str(e)}"
|
130 |
+
|
131 |
+
# === Gradio GUI ===
|
132 |
+
with gr.Blocks(css="body { background-color: white; color: #111; }") as demo:
|
133 |
+
gr.Markdown("## π©Έ Blood Group & Disease Detection System")
|
134 |
+
gr.Markdown("Select a task, upload an image, and click Predict.")
|
135 |
+
|
136 |
+
task_selector = gr.Dropdown(
|
137 |
+
choices=[
|
138 |
+
"Check Clotness",
|
139 |
+
"Blood Group Identification",
|
140 |
+
"Sickle Cell Detection",
|
141 |
+
"Thalassemia Detection",
|
142 |
+
"Leukemia Detection"
|
143 |
+
],
|
144 |
+
label="Select Task",
|
145 |
+
value=None,
|
146 |
+
interactive=True
|
147 |
+
)
|
148 |
+
|
149 |
+
# Clotness Section
|
150 |
+
with gr.Column(visible=False) as clot_section:
|
151 |
+
clot_image = gr.Image(type="pil", label="Upload Blood Drop Image")
|
152 |
+
clot_result = gr.Textbox(label="Prediction Result", lines=4)
|
153 |
+
with gr.Row():
|
154 |
+
clot_predict = gr.Button("π Predict")
|
155 |
+
clot_clear = gr.Button("π Clear")
|
156 |
+
clot_predict.click(fn=predict_clot, inputs=clot_image, outputs=clot_result)
|
157 |
+
clot_clear.click(fn=lambda: (None, ""), outputs=[clot_image, clot_result])
|
158 |
+
|
159 |
+
# Blood Group Section
|
160 |
+
with gr.Column(visible=False) as group_section:
|
161 |
+
group_image = gr.Image(type="pil", label="Upload 3-Drop Blood Image")
|
162 |
+
with gr.Row():
|
163 |
+
drop_a_img = gr.Image(label="Anti-A Drop", interactive=False)
|
164 |
+
drop_b_img = gr.Image(label="Anti-B Drop", interactive=False)
|
165 |
+
drop_d_img = gr.Image(label="Anti-D Drop", interactive=False)
|
166 |
+
group_result = gr.Textbox(label="Prediction Result", lines=6)
|
167 |
+
with gr.Row():
|
168 |
+
group_predict = gr.Button("π Predict Blood Group")
|
169 |
+
group_clear = gr.Button("π Clear")
|
170 |
+
group_predict.click(fn=detect_blood_group, inputs=group_image, outputs=[drop_a_img, drop_b_img, drop_d_img, group_result])
|
171 |
+
group_clear.click(fn=lambda: (None, None, None, None, ""), outputs=[group_image, drop_a_img, drop_b_img, drop_d_img, group_result])
|
172 |
+
|
173 |
+
# Sickle Cell Section
|
174 |
+
with gr.Column(visible=False) as sickle_section:
|
175 |
+
sickle_image = gr.Image(type="pil", label="Upload Blood Cell Image")
|
176 |
+
sickle_result = gr.Textbox(label="Prediction Result", lines=4)
|
177 |
+
with gr.Row():
|
178 |
+
sickle_predict = gr.Button("π Predict")
|
179 |
+
sickle_clear = gr.Button("π Clear")
|
180 |
+
sickle_predict.click(fn=predict_sickle, inputs=sickle_image, outputs=sickle_result)
|
181 |
+
sickle_clear.click(fn=lambda: (None, ""), outputs=[sickle_image, sickle_result])
|
182 |
+
|
183 |
+
# Thalassemia Section
|
184 |
+
with gr.Column(visible=False) as thal_section:
|
185 |
+
thal_image = gr.Image(type="pil", label="Upload Blood Image")
|
186 |
+
thal_result = gr.Textbox(label="Prediction Result", lines=4)
|
187 |
+
with gr.Row():
|
188 |
+
thal_predict = gr.Button("π Predict")
|
189 |
+
thal_clear = gr.Button("π Clear")
|
190 |
+
thal_predict.click(fn=predict_thalassemia, inputs=thal_image, outputs=thal_result)
|
191 |
+
thal_clear.click(fn=lambda: (None, ""), outputs=[thal_image, thal_result])
|
192 |
+
|
193 |
+
# Leukemia Section
|
194 |
+
with gr.Column(visible=False) as leukemia_section:
|
195 |
+
leukemia_image = gr.Image(type="pil", label="Upload Microscopic Blood Image")
|
196 |
+
leukemia_result = gr.Textbox(label="Prediction Result", lines=8)
|
197 |
+
with gr.Row():
|
198 |
+
leukemia_predict = gr.Button("π Predict")
|
199 |
+
leukemia_clear = gr.Button("π Clear")
|
200 |
+
leukemia_predict.click(fn=predict_leukemia, inputs=leukemia_image, outputs=leukemia_result)
|
201 |
+
leukemia_clear.click(fn=lambda: (None, ""), outputs=[leukemia_image, leukemia_result])
|
202 |
+
|
203 |
+
# Toggle UI Sections
|
204 |
+
def show_section(task):
|
205 |
+
return (
|
206 |
+
gr.update(visible=task == "Check Clotness"),
|
207 |
+
gr.update(visible=task == "Blood Group Identification"),
|
208 |
+
gr.update(visible=task == "Sickle Cell Detection"),
|
209 |
+
gr.update(visible=task == "Thalassemia Detection"),
|
210 |
+
gr.update(visible=task == "Leukemia Detection")
|
211 |
+
)
|
212 |
+
|
213 |
+
task_selector.change(fn=show_section, inputs=task_selector, outputs=[
|
214 |
+
clot_section, group_section, sickle_section, thal_section, leukemia_section
|
215 |
+
])
|
216 |
+
|
217 |
+
demo.launch(share=True)
|
bestluk.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5fceee94be2967a69b7a44373bf77a9bbfa9cc3fd6f81deb41f4546e597cb239
|
3 |
+
size 14458152
|
blood_group/clot_classifier_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a08b11f45c455d272d84d9264d3d862f042049cd6ba68bb2568c53119a721ef6
|
3 |
+
size 39704352
|
clot_classifier_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a08b11f45c455d272d84d9264d3d862f042049cd6ba68bb2568c53119a721ef6
|
3 |
+
size 39704352
|
packages.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
libgl1
|
2 |
+
ffmpeg
|
3 |
+
python3-distutils
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.35.0
|
2 |
+
numpy>=1.23,<1.26
|
3 |
+
Pillow==9.5.0
|
4 |
+
tensorflow==2.12.0
|
5 |
+
torch==2.0.1
|
6 |
+
ultralytics==8.0.174
|
sickle_cell_model_mobnetv2.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9153b7820cf59dfccdf9993581ff649a60a55c320fc5e5a12c51432e0cfb4d72
|
3 |
+
size 28056472
|