File size: 2,140 Bytes
2cd5935
 
 
 
 
 
 
a0ba42d
2cd5935
05d0642
2cd5935
05d0642
54432a3
2cd5935
 
54432a3
2cd5935
 
 
 
54432a3
2cd5935
54432a3
2cd5935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54432a3
2cd5935
 
 
 
 
 
 
 
 
54432a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import Tuple
from ultralytics import YOLO
from ultralytics.engine.results import Boxes
from ultralytics.utils.plotting import Annotator

import gradio as gr

cell_detector = YOLO("./weights/yolo_uninfected_cells.pt")
yolo_detector = YOLO("./weights/yolo_infected_cells.pt")
redetr_detector = YOLO("./weights/redetr_infected_cells.pt")

models = {"Yolo V11": yolo_detector, "Real Time Detection Transformer": redetr_detector}
# classes = {"Yolo V11": [0], "Real Time Detection Transformer": [1]}


def inference(image, model, conf) -> Tuple[str, str, str]:
    bboxes = []
    labels = []
    healthy_cell_count = 0
    unhealthy_cell_count = 0
    cells_results = cell_detector.predict(image, conf=0.4)
    selected_model_results = models[model].predict(
        image, conf=conf
    )

    for cell_result in cells_results:
        boxes: Boxes = cell_result.boxes
        healthy_cells_bboxes = boxes.xyxy.tolist()
        healthy_cell_count += len(healthy_cells_bboxes)
        bboxes.extend(healthy_cells_bboxes)
        labels.extend(["healthy"] * healthy_cell_count)

    for res in selected_model_results:
        boxes: Boxes = res.boxes
        unhealthy_cells_bboxes = boxes.xyxy.tolist()
        unhealthy_cell_count += len(unhealthy_cells_bboxes)
        bboxes.extend(unhealthy_cells_bboxes)
        labels.extend(["unhealthy"] * unhealthy_cell_count)

    annotator = Annotator(image, font_size=5, line_width=1)

    for box, label in zip(bboxes, labels):
        annotator.box_label(box, label)

    img = annotator.result()
    return (img, healthy_cell_count, unhealthy_cell_count)


ifer = gr.Interface(
    fn=inference,
    inputs=[
        gr.Image(label="Input Image", type="numpy"),
        gr.Dropdown(
            choices=["Yolo V11", "Real Time Detection Transformer"], multiselect=False
        ),
        gr.Slider(minimum=0.01, maximum=1)
    ],
    outputs=[
        gr.Image(label="Output Image", type="numpy"),
        gr.Textbox(label="Healthy Cells Count"),
        gr.Textbox(label="Infected Cells Count"),
    ],
    title="Blood Cancer Cell Detection and Counting"
)

ifer.launch(share=True)