Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Tuple
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from ultralytics.engine.results import Boxes
|
4 |
+
from ultralytics.utils.plotting import Annotator
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
cell_detector = YOLO("./weights/yolo_uninfected_cells.pt")
|
9 |
+
yolo_detector = YOLO("./weights/yolo_infected_cells.pt")
|
10 |
+
redetr_detector = YOLO("./weights/redetr_infected_cells.pt")
|
11 |
+
|
12 |
+
models = {"Yolo V11": yolo_detector, "Real Time Detection Transformer": redetr_detector}
|
13 |
+
# classes = {"Yolo V11": [0], "Real Time Detection Transformer": [1]}
|
14 |
+
|
15 |
+
|
16 |
+
def inference(image, model, conf) -> Tuple[str, str, str]:
|
17 |
+
bboxes = []
|
18 |
+
labels = []
|
19 |
+
healthy_cell_count = 0
|
20 |
+
unhealthy_cell_count = 0
|
21 |
+
cells_results = cell_detector.predict(image, conf=0.4)
|
22 |
+
selected_model_results = models[model].predict(
|
23 |
+
image, conf=conf
|
24 |
+
)
|
25 |
+
|
26 |
+
for cell_result in cells_results:
|
27 |
+
boxes: Boxes = cell_result.boxes
|
28 |
+
healthy_cells_bboxes = boxes.xyxy.tolist()
|
29 |
+
healthy_cell_count += len(healthy_cells_bboxes)
|
30 |
+
bboxes.extend(healthy_cells_bboxes)
|
31 |
+
labels.extend(["healthy"] * healthy_cell_count)
|
32 |
+
|
33 |
+
for res in selected_model_results:
|
34 |
+
boxes: Boxes = res.boxes
|
35 |
+
unhealthy_cells_bboxes = boxes.xyxy.tolist()
|
36 |
+
unhealthy_cell_count += len(unhealthy_cells_bboxes)
|
37 |
+
bboxes.extend(unhealthy_cells_bboxes)
|
38 |
+
labels.extend(["unhealthy"] * unhealthy_cell_count)
|
39 |
+
|
40 |
+
annotator = Annotator(image, font_size=5, line_width=1)
|
41 |
+
|
42 |
+
for box, label in zip(bboxes, labels):
|
43 |
+
annotator.box_label(box, label)
|
44 |
+
|
45 |
+
img = annotator.result()
|
46 |
+
return (img, healthy_cell_count, unhealthy_cell_count)
|
47 |
+
|
48 |
+
|
49 |
+
ifer = gr.Interface(
|
50 |
+
fn=inference,
|
51 |
+
inputs=[
|
52 |
+
gr.Image(label="Input Image", type="numpy"),
|
53 |
+
gr.Dropdown(
|
54 |
+
choices=["Yolo V11", "Real Time Detection Transformer"], multiselect=False, value="Yolo V11"
|
55 |
+
),
|
56 |
+
gr.Slider(minimum=0.01, maximum=1)
|
57 |
+
],
|
58 |
+
outputs=[
|
59 |
+
gr.Image(label="Output Image", type="numpy"),
|
60 |
+
gr.Textbox(label="Healthy Cells Count"),
|
61 |
+
gr.Textbox(label="Infected Cells Count"),
|
62 |
+
],
|
63 |
+
title="Blood Cancer Cell Detection and Counting"
|
64 |
+
)
|
65 |
+
|
66 |
+
ifer.launch(share=True)
|