muhammadhamza-stack commited on
Commit
f23f0f3
·
1 Parent(s): 3351b09

refine the gradio app

Browse files
Files changed (3) hide show
  1. app.py +168 -38
  2. blood_smear_1.jpg +3 -0
  3. blood_smear_2.jpg +3 -0
app.py CHANGED
@@ -2,65 +2,195 @@ 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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from ultralytics import YOLO
3
  from ultralytics.engine.results import Boxes
4
  from ultralytics.utils.plotting import Annotator
 
5
  import gradio as gr
6
+ import os
7
 
8
+ # --- Model Loading ---
9
+ try:
10
+ cell_detector = YOLO("./weights/yolo_uninfected_cells.pt")
11
+ yolo_detector = YOLO("./weights/yolo_infected_cells.pt")
12
+ redetr_detector = YOLO("./weights/redetr_infected_cells.pt")
13
+ except Exception as e:
14
+ print(f"Warning: Model loading failed. Ensure weights files are in ./weights/ directory. Error: {e}")
15
+ # Define placeholder models if real models fail to load (for UI development)
16
+ class DummyYOLO:
17
+ def predict(self, image, conf=0.5):
18
+ # Return dummy results structure
19
+ class DummyBoxes:
20
+ xyxy = []
21
+ class DummyResult:
22
+ boxes = DummyBoxes()
23
+ return [DummyResult()]
24
+ cell_detector = DummyYOLO()
25
+ yolo_detector = DummyYOLO()
26
+ redetr_detector = DummyYOLO()
27
 
28
  models = {"Yolo V11": yolo_detector, "Real Time Detection Transformer": redetr_detector}
 
29
 
30
+ # --- Documentation Strings ---
31
+
32
+ USAGE_GUIDELINES = """
33
+ ## 1. Quick Start Guide: Cell Detection and Counting
34
+ This application uses two specialized Artificial Intelligence models to analyze a blood smear image, simultaneously detecting both healthy and potentially infected (unhealthy) cells.
35
+
36
+ 1. **Upload**: Upload a clear blood smear image (JPG or PNG) using the 'Input Image' box.
37
+ 2. **Select Model**: Choose between the two detection models: `Yolo V11` (often fast and accurate for common objects) or `Real Time Detection Transformer`.
38
+ 3. **Adjust Confidence**: Use the slider to set the **Confidence Threshold**. (A higher value means the model must be more certain of a detection.)
39
+ 4. **Run**: Click the **"Submit"** button.
40
+ 5. **Review**: The output image will show bounding boxes around detected cells (colors based on model configuration), and the counts will be displayed below.
41
+
42
+ ### Key Requirement:
43
+ * The system uses **two independent models**: one strictly for **Healthy Cells**, and one (the selected model) for **Infected Cells**.
44
+ """
45
+
46
+ INPUT_EXPLANATION = """
47
+ ## 2. Expected Inputs
48
+
49
+ | Parameter | Purpose | Range/Options | Guidance for Non-Tech Users |
50
+ | :--- | :--- | :--- | :--- |
51
+ | **Input Image** | The microscopic blood smear image to be analyzed. | JPG, PNG format. | Ensure the image is clear and focused. |
52
+ | **Model Selection** | Chooses the AI architecture used for detecting **Infected Cells**. | Yolo V11, Real Time Detection Transformer | Start with the default (`Yolo V11`) unless specific performance is required. |
53
+ | **Confidence Threshold** | The minimum probability required for a detection box to be shown. | 0.01 to 1.00 | Setting this too low (e.g., 0.1) may show many false positives. Setting it too high (e.g., 0.9) may miss real cells. Start around 0.5. |
54
+ """
55
+
56
+ OUTPUT_EXPLANATION = """
57
+ ## 3. Expected Outputs
58
+
59
+ | Output Field | Description | Interpretation |
60
+ | :--- | :--- | :--- |
61
+ | **Output Image** | The input image with colored bounding boxes drawn around every detected cell. | Visually confirms the location and classification of each cell. |
62
+ | **Healthy Cells Count** | The total number of cells detected by the dedicated *uninfected* cell model. | Provides a baseline count of normal cells. |
63
+ | **Infected Cells Count** | The total number of cells detected by the *selected* model (Yolo V11 or RT DETR). | This represents the count of potentially cancerous/abnormal cells. |
64
+
65
+ """
66
+
67
+ # --- Example Data Setup ---
68
+ # Assuming you have example images named 'blood_smear_1.jpg' and 'blood_smear_2.jpg'
69
+ # in an 'examples' folder or the root directory.
70
+ SAMPLE_EXAMPLES = [
71
+ ["blood_smear_1.jpg", "Yolo V11", 0.5],
72
+ ["blood_smear_2.jpg", "Real Time Detection Transformer", 0.45],
73
+ ]
74
+
75
+ # ----------------- Core Inference Function -----------------
76
 
77
  def inference(image, model, conf) -> Tuple[str, str, str]:
78
+ # Ensure all inputs are valid before proceeding
79
+ if image is None:
80
+ gr.Error("Please upload an image.")
81
+ return None, "0", "0"
82
+ if model not in models:
83
+ gr.Error(f"Selected model '{model}' is not available.")
84
+ return None, "0", "0"
85
+
86
  bboxes = []
87
  labels = []
88
+
89
+ # Use lists to store counts that will be incremented
90
+ healthy_cell_count_list = [0]
91
+ unhealthy_cell_count_list = [0]
92
+
93
+ # 1. Healthy Cell Detection (Fixed model and fixed confidence 0.4)
94
  cells_results = cell_detector.predict(image, conf=0.4)
 
 
 
 
95
  for cell_result in cells_results:
96
  boxes: Boxes = cell_result.boxes
97
  healthy_cells_bboxes = boxes.xyxy.tolist()
98
+ healthy_cell_count_list[0] += len(healthy_cells_bboxes)
99
  bboxes.extend(healthy_cells_bboxes)
100
+ # Note: YOLO classes start at 0. Here we use custom labels 'healthy'
101
+ labels.extend(["healthy"] * len(healthy_cells_bboxes))
102
+
103
+ # 2. Infected Cell Detection (Selected model and user-defined confidence)
104
+ selected_model_results = models[model].predict(image, conf=conf)
105
 
106
  for res in selected_model_results:
107
  boxes: Boxes = res.boxes
108
  unhealthy_cells_bboxes = boxes.xyxy.tolist()
109
+ unhealthy_cell_count_list[0] += len(unhealthy_cells_bboxes)
110
  bboxes.extend(unhealthy_cells_bboxes)
111
+ # Note: Use 'unhealthy' label for the selected model's output
112
+ labels.extend(["unhealthy"] * len(unhealthy_cells_bboxes))
113
+
114
+ # 3. Annotation
115
+ annotator = Annotator(image, font_size=30, line_width=4, pil=True) # Increased font/width for visibility
116
 
117
+ # Define colors based on label
118
+ color_map = {"healthy": (0, 255, 0), "unhealthy": (255, 0, 0)} # Green for healthy, Red for unhealthy
119
 
120
  for box, label in zip(bboxes, labels):
121
+ # Annotator expects a list of 4 float coords and an optional label string
122
+ annotator.box_label(box, label, color=color_map.get(label, (255, 255, 255)))
123
 
124
  img = annotator.result()
125
+
126
+ # Return results as strings for the Textbox components
127
+ return (img, str(healthy_cell_count_list[0]), str(unhealthy_cell_count_list[0]))
128
+
129
+ # ----------------- Gradio Interface (Blocks) -----------------
130
+
131
+ with gr.Blocks(title="Blood Cell Detection") as ifer:
132
+ gr.Markdown("<h1 style='text-align: center;'> Blood Cell Cancer Detection and Counting </h1>")
133
+ gr.Markdown("Uses specialized object detection models to count healthy and infected cells in blood smear images.")
134
+
135
+ # 1. Documentation
136
+ with gr.Accordion(" Tips & Guidelines ", open=False):
137
+ gr.Markdown(USAGE_GUIDELINES)
138
+ gr.Markdown("---")
139
+ gr.Markdown(INPUT_EXPLANATION)
140
+ gr.Markdown("---")
141
+ gr.Markdown(OUTPUT_EXPLANATION)
142
+
143
+ # 2. Interface Inputs
144
+ with gr.Row():
145
+ with gr.Column():
146
+ gr.Markdown("## Step 1: Upload Image ")
147
+ image_input = gr.Image(label="Input Image", type="numpy")
148
+ with gr.Column():
149
+ gr.Markdown("## Step 2: Set Parameters")
150
+ model_selection = gr.Dropdown(
151
+ label="Select Detection Model (for Infected Cells)",
152
+ choices=["Yolo V11", "Real Time Detection Transformer"],
153
+ multiselect=False,
154
+ value="Yolo V11"
155
+ )
156
+ conf_slider = gr.Slider(
157
+ minimum=0.01,
158
+ maximum=1,
159
+ value=0.5,
160
+ step=0.01,
161
+ label="Confidence Threshold (Min. certainty required)"
162
+ )
163
+
164
+ gr.Markdown("## Step 3: Click Analyze Image")
165
+ with gr.Row():
166
+ submit_button = gr.Button("Analyze Image", variant="primary")
167
+
168
+ # 3. Interface Outputs
169
+ gr.Markdown("## Results")
170
+ output_image = gr.Image(label="Output Image (Detected Cells)", type="numpy")
171
+
172
+ with gr.Row():
173
+ healthy_count = gr.Textbox(label="Healthy Cells Count")
174
+ unhealthy_count = gr.Textbox(label="Infected Cells Count")
175
+
176
+ # 4. Examples
177
+ gr.Markdown("---")
178
+ gr.Markdown("## Example Inputs")
179
+ gr.Examples(
180
+ examples=SAMPLE_EXAMPLES,
181
+ inputs=[image_input, model_selection, conf_slider],
182
+ outputs=[output_image, healthy_count, unhealthy_count],
183
+ fn=inference,
184
+ cache_examples=False,
185
+ label="Click a row to load the image and parameters"
186
+ )
187
+
188
+ # Event Handler
189
+ submit_button.click(
190
+ fn=inference,
191
+ inputs=[image_input, model_selection, conf_slider],
192
+ outputs=[output_image, healthy_count, unhealthy_count]
193
+ )
194
+
195
+ if __name__ == "__main__":
196
+ ifer.launch(share=True)
blood_smear_1.jpg ADDED

Git LFS Details

  • SHA256: 24e9ba53812530a382452b9112cee51c74db638d1ebf7e54716594695ec53675
  • Pointer size: 131 Bytes
  • Size of remote file: 183 kB
blood_smear_2.jpg ADDED

Git LFS Details

  • SHA256: 6bf6397edb1986c771b096b0ab1b87c1d3d21ef6b12ff6a3dbc244dd0bb259e5
  • Pointer size: 131 Bytes
  • Size of remote file: 167 kB