kylecsnow commited on
Commit
508aef3
·
verified ·
1 Parent(s): 469a301

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import gradio as gr
3
+
4
+
5
+ model = YOLO("./runs/detect/train18/weights/best.pt")
6
+
7
+
8
+ def yolo_predict(image):
9
+ """Run YOLOv8 inference and return annotated image with results"""
10
+ results = model(image)
11
+ print(results)
12
+ annotated_image = results[0].plot()
13
+
14
+ # Get prediction details
15
+ boxes = results[0].boxes
16
+ prediction_details = []
17
+
18
+ for box in boxes:
19
+ class_id = int(box.cls[0].item())
20
+ class_name = model.names[class_id]
21
+ confidence = round(box.conf[0].item(), 2)
22
+ coords = box.xyxy[0].tolist() # [x1, y1, x2, y2]
23
+
24
+ prediction_details.append({
25
+ "class": class_name,
26
+ "confidence": confidence,
27
+ "bbox": coords
28
+ })
29
+
30
+ return annotated_image
31
+ # return annotated_image, prediction_details
32
+
33
+
34
+ with gr.Blocks() as demo:
35
+ gr.Markdown("# YOLOv8 Object Detection")
36
+ gr.Markdown(
37
+ """
38
+ This application uses a YOLOv8m model fine-tuned specifically to detect red blood
39
+ cells, white blood cells, and platelets in images of blood cells. This version
40
+ was trained using the `keremberke/blood-cell-object-detection` dataset on huggingface.com.
41
+ """
42
+ )
43
+
44
+ gr.Interface(
45
+ fn=yolo_predict,
46
+ inputs=gr.Image(label="Input Image",type="pil"),
47
+ outputs=[
48
+ gr.Image(label="Detected Objects"),
49
+ # gr.JSON(label="Detection Details")
50
+ ],
51
+ # title="YOLOv8 Object Detection",
52
+ # # description="Upload an image to detect objects using YOLOv8",
53
+ description='Select an example image below (none of which were included in model training or validation), or upload your own image. Then, click "Submit" to see the model in action.',
54
+ examples=[
55
+ "bloodcell-examples/image_0.jpg",
56
+ "bloodcell-examples/image_1.jpg",
57
+ "bloodcell-examples/image_2.jpg",
58
+ "bloodcell-examples/image_3.jpg",
59
+ "bloodcell-examples/image_4.jpg",
60
+ ],
61
+ )
62
+
63
+
64
+ demo.launch(
65
+ show_error=True,
66
+ height=900,
67
+ width="80%",
68
+ # width="100%",
69
+ share=True,
70
+ )