jin3213 commited on
Commit
24c865a
·
verified ·
1 Parent(s): ae85f3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -54
app.py CHANGED
@@ -1,54 +1,54 @@
1
- from ultralytics import YOLO
2
- import gradio as gr
3
- from PIL import Image
4
- import torch
5
-
6
- # Load model
7
- model = YOLO("C:\\Users\\warho\\Desktop\\bonsAI\\best.pt") # make sure best.pt is in the same folder
8
-
9
- # Prediction function
10
- def predict(inp):
11
- if inp is None:
12
- return None, {}
13
- results = model.predict(source=inp, conf=0.5, iou=0.5, imgsz=640)
14
- r = results[0]
15
- output_img = r.plot()[:, :, ::-1] # convert BGR to RGB for Gradio
16
-
17
- # Build confidence dictionary (like the example video)
18
- conf_dict = {}
19
- for box in r.boxes:
20
- cls_id = int(box.cls.item())
21
- cls_name = model.names[cls_id]
22
- conf = round(float(box.conf.item()) * 100, 2)
23
- conf_dict[cls_name] = conf
24
-
25
- return Image.fromarray(output_img), conf_dict
26
-
27
-
28
- # Gradio interface (similar style to video)
29
- demo = gr.Interface(
30
- fn=predict,
31
- inputs=gr.Image(type="pil", label="Upload Pill Image"),
32
- outputs=[
33
- gr.Image(type="pil", label="Detected Pills"),
34
- gr.Label(num_top_classes=3, label="Predictions")
35
- ],
36
- title="bonsAI Pill Detection",
37
- description=(
38
- "Upload an image of a pill. The YOLOv12 model detects and classifies 20 pill types "
39
- "commonly found in the Philippines. This study aims to automate pill recognition for "
40
- "pharmaceutical verification and healthcare support using computer vision and deep learning."
41
- ),
42
- article=(
43
- "### Study Summary\n"
44
- "The bonsAI project demonstrates the application of YOLOv12 in real-time pill classification "
45
- "and segmentation. By training on the Pharmaceutical Drugs and Vitamins Dataset (Version 2), "
46
- "the system accurately identifies tablets and capsules across 20 classes using bounding boxes "
47
- "and mask segmentation. The model achieved high mAP and F1-scores, confirming its potential "
48
- "for aiding pharmacists and healthcare providers in ensuring drug authenticity and preventing "
49
- "dispensing errors."
50
- )
51
- )
52
-
53
-
54
- demo.launch()
 
1
+ from ultralytics import YOLO
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load model
7
+ model = YOLO("best.pt") # make sure best.pt is in the same folder
8
+
9
+ # Prediction function
10
+ def predict(inp):
11
+ if inp is None:
12
+ return None, {}
13
+ results = model.predict(source=inp, conf=0.5, iou=0.5, imgsz=640)
14
+ r = results[0]
15
+ output_img = r.plot()[:, :, ::-1] # convert BGR to RGB for Gradio
16
+
17
+ # Build confidence dictionary (like the example video)
18
+ conf_dict = {}
19
+ for box in r.boxes:
20
+ cls_id = int(box.cls.item())
21
+ cls_name = model.names[cls_id]
22
+ conf = round(float(box.conf.item()) * 100, 2)
23
+ conf_dict[cls_name] = conf
24
+
25
+ return Image.fromarray(output_img), conf_dict
26
+
27
+
28
+ # Gradio interface (similar style to video)
29
+ demo = gr.Interface(
30
+ fn=predict,
31
+ inputs=gr.Image(type="pil", label="Upload Pill Image"),
32
+ outputs=[
33
+ gr.Image(type="pil", label="Detected Pills"),
34
+ gr.Label(num_top_classes=3, label="Predictions")
35
+ ],
36
+ title="bonsAI Pill Detection",
37
+ description=(
38
+ "Upload an image of a pill. The YOLOv12 model detects and classifies 20 pill types "
39
+ "commonly found in the Philippines. This study aims to automate pill recognition for "
40
+ "pharmaceutical verification and healthcare support using computer vision and deep learning."
41
+ ),
42
+ article=(
43
+ "### Study Summary\n"
44
+ "The bonsAI project demonstrates the application of YOLOv12 in real-time pill classification "
45
+ "and segmentation. By training on the Pharmaceutical Drugs and Vitamins Dataset (Version 2), "
46
+ "the system accurately identifies tablets and capsules across 20 classes using bounding boxes "
47
+ "and mask segmentation. The model achieved high mAP and F1-scores, confirming its potential "
48
+ "for aiding pharmacists and healthcare providers in ensuring drug authenticity and preventing "
49
+ "dispensing errors."
50
+ )
51
+ )
52
+
53
+
54
+ demo.launch()