ibrahim313 commited on
Commit
0d5b33e
·
verified ·
1 Parent(s): f765816

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -23
app.py CHANGED
@@ -1,45 +1,63 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
4
  from collections import Counter
5
  from ultralytics import YOLO
6
  from huggingface_hub import hf_hub_download
7
 
8
- # Download model from Hugging Face repo
9
- MODEL_PATH = hf_hub_download(
10
- repo_id="ibrahim313/Bioengineering_Query_Tool_image_based",
11
- filename="best.pt"
12
- )
13
 
14
- # Load the YOLOv10 model
15
- model = YOLO(MODEL_PATH)
16
 
17
- def predict(image):
18
- # Convert the image from BGR to RGB
 
19
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
20
 
21
- # Perform prediction
22
  results = model.predict(source=image_rgb, imgsz=640, conf=0.25)
23
 
24
- # Get the annotated image
25
  annotated_img = results[0].plot()
26
 
27
  # Extract detection data
28
  detections = results[0].boxes.data if results[0].boxes is not None else []
29
- class_names = [model.names[int(cls)] for cls in detections[:, 5]] if len(detections) > 0 else []
30
- count = Counter(class_names)
31
-
32
- # Create a string representation of the detections
33
- detection_str = ', '.join([f"{name}: {count}" for name, count in count.items()]) if class_names else "No detections"
34
-
35
- return annotated_img, detection_str
 
 
 
 
 
 
 
 
 
 
 
36
 
 
37
  app = gr.Interface(
38
- predict,
39
  inputs=gr.Image(type="numpy", label="Upload an Image"),
40
- outputs=[gr.Image(type="numpy", label="Annotated Image"), gr.Textbox(label="Detection Counts")],
41
- title="Blood Cell Count",
42
- description="Upload an image and YOLOv10 will detect blood cells."
 
 
 
 
43
  )
44
 
45
- app.launch()
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ import pandas as pd
5
  from collections import Counter
6
  from ultralytics import YOLO
7
  from huggingface_hub import hf_hub_download
8
 
9
+ # # Download YOLOv10 model from Hugging Face
10
+ # MODEL_PATH = hf_hub_download(
11
+ # repo_id="ibrahim313/Bioengineering_Query_Tool_image_based",
12
+ # filename="best.pt"
13
+ # )
14
 
15
+ # Load the model
16
+ model = YOLO("best.pt")
17
 
18
+ def process_image(image):
19
+ """Detect cells in the image, extract attributes, and return results."""
20
+ # Convert image to RGB
21
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
22
 
23
+ # Perform detection
24
  results = model.predict(source=image_rgb, imgsz=640, conf=0.25)
25
 
26
+ # Get annotated image
27
  annotated_img = results[0].plot()
28
 
29
  # Extract detection data
30
  detections = results[0].boxes.data if results[0].boxes is not None else []
31
+ if len(detections) > 0:
32
+ class_names = [model.names[int(cls)] for cls in detections[:, 5]]
33
+ count = Counter(class_names)
34
+ detection_str = ', '.join([f"{name}: {count[name]}" for name in count])
35
+
36
+ # Extract cell attributes (position, size, etc.)
37
+ df = pd.DataFrame(detections.numpy(), columns=["x_min", "y_min", "x_max", "y_max", "confidence", "class"])
38
+ df["class_name"] = df["class"].apply(lambda x: model.names[int(x)])
39
+ df["width"] = df["x_max"] - df["x_min"]
40
+ df["height"] = df["y_max"] - df["y_min"]
41
+ df["area"] = df["width"] * df["height"]
42
+
43
+ summary = df.groupby("class_name")["area"].describe().reset_index()
44
+ else:
45
+ detection_str = "No detections"
46
+ summary = pd.DataFrame(columns=["class_name", "count", "mean", "std", "min", "25%", "50%", "75%", "max"])
47
+
48
+ return annotated_img, detection_str, summary
49
 
50
+ # Create Gradio interface
51
  app = gr.Interface(
52
+ fn=process_image,
53
  inputs=gr.Image(type="numpy", label="Upload an Image"),
54
+ outputs=[
55
+ gr.Image(type="numpy", label="Annotated Image"),
56
+ gr.Textbox(label="Detection Counts"),
57
+ gr.Dataframe(label="Cell Statistics")
58
+ ],
59
+ title="Bioengineering Image Analysis Tool",
60
+ description="Upload an image to detect and analyze bioengineering cells using YOLOv10."
61
  )
62
 
63
+ app.launch()