WizardForest commited on
Commit
e4d37b8
·
verified ·
1 Parent(s): d2aa50f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -1,33 +1,32 @@
1
- import gradio as gr
2
- from ultralytics import YOLO
3
-
4
- def predict(img):
5
- path = img.split("\\")[-1].split(".")[0]
6
- print("path", path)
7
-
8
- model = YOLO("model.pt")
9
-
10
- results = model.predict(source=img,save=False, show_labels=False, show_conf=False)
11
- count = 0
12
- for i in results:
13
- count = len(i.boxes)
14
- annot_img = i.plot(labels=False, masks=False)
15
-
16
- #result_img = os.path.join(cur_path,r"runs\segment\predict",f"{path}.jpg")
17
- #print("result img",result_img)
18
- return str(count), annot_img
19
-
20
- with gr.Blocks(title="Pill Counter") as demo:
21
-
22
- with gr.Row():
23
- with gr.Column():
24
- img = gr.Image(type="filepath",format="jpg", height=500, width=700)
25
-
26
- button = gr.Button()
27
- with gr.Column():
28
- data_output = gr.Textbox()
29
- img_output = gr.Image(type="numpy")
30
- button.click(fn=predict, inputs=img, outputs=[data_output,img_output])
31
-
32
- if __name__ == "__main__":
33
- demo.launch()
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+
4
+ def predict(img):
5
+ path = img.split("\\")[-1].split(".")[0]
6
+ print("path", path)
7
+
8
+ model = YOLO("model.pt")
9
+
10
+ results = model.predict(source=img, save=False, show_labels=False, show_conf=False)
11
+ count = 0
12
+ for i in results:
13
+ count = len(i.boxes)
14
+ annot_img = i.plot(labels=False, masks=False)
15
+ # Convert BGR to RGB if needed
16
+ if annot_img.shape[-1] == 3: # Check if image has 3 channels
17
+ annot_img = annot_img[..., ::-1] # Reverse the color channels
18
+
19
+ return str(count), annot_img
20
+
21
+ with gr.Blocks(title="Pill Counter") as demo:
22
+ with gr.Row():
23
+ with gr.Column():
24
+ img = gr.Image(type="filepath", format="jpg", height=500, width=700)
25
+ button = gr.Button()
26
+ with gr.Column():
27
+ data_output = gr.Textbox()
28
+ img_output = gr.Image(type="numpy")
29
+ button.click(fn=predict, inputs=img, outputs=[data_output, img_output])
30
+
31
+ if __name__ == "__main__":
32
+ demo.launch()