Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
|
| 6 |
+
def predict(img):
|
| 7 |
+
path = img.split("\\")[-1].split(".")[0]
|
| 8 |
+
print("path", path)
|
| 9 |
+
|
| 10 |
+
if os.path.exists(r".\runs\segment\predict"):
|
| 11 |
+
shutil.rmtree(r".\runs\segment\predict")
|
| 12 |
+
model = YOLO("model.pt")
|
| 13 |
+
|
| 14 |
+
results = model.predict(source=img,save=True, show_labels=False, show_conf=False)
|
| 15 |
+
count = 0
|
| 16 |
+
for i in results:
|
| 17 |
+
count = len(i.boxes)
|
| 18 |
+
cur_path = os.getcwd()
|
| 19 |
+
result_img = os.path.join(cur_path,r"runs\segment\predict",f"{path}.jpg")
|
| 20 |
+
print("result img",result_img)
|
| 21 |
+
return str(count), result_img
|
| 22 |
+
|
| 23 |
+
with gr.Blocks(title="Pill Counter") as demo:
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
img = gr.Image(type="filepath",format="jpg", height=500, width=700)
|
| 28 |
+
|
| 29 |
+
button = gr.Button()
|
| 30 |
+
with gr.Column():
|
| 31 |
+
data_output = gr.Textbox()
|
| 32 |
+
img_output = gr.Image(type="filepath")
|
| 33 |
+
button.click(fn=predict, inputs=img, outputs=[data_output ,img_output])
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch(allowed_paths=["runs"])
|