zohaib9642 commited on
Commit
80d1a24
·
1 Parent(s): 2da79c4

Yolo Model

Browse files
Files changed (3) hide show
  1. app.py +46 -0
  2. best.pt +3 -0
  3. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import tempfile
4
+ import os
5
+ import subprocess
6
+ import glob
7
+
8
+
9
+ def process_image(input_img):
10
+ # Create a temporary directory to store the input image
11
+ with tempfile.TemporaryDirectory() as temp_input_dir:
12
+ input_image_path = os.path.join(temp_input_dir, "input.jpg")
13
+ input_img.save(input_image_path)
14
+
15
+ # Create a temporary directory for the output image
16
+ with tempfile.TemporaryDirectory() as temp_output_dir:
17
+ # Command to run the YOLO model
18
+ command = f"yolo task=detect mode=predict model=best.pt conf=0.25 source={temp_input_dir} save=True"
19
+ subprocess.run(command, shell=True)
20
+
21
+
22
+ # Get the most recent 'predict' folder in 'runs/detect'
23
+ list_of_dirs = glob.glob('runs/detect/predict*')
24
+ latest_dir = max(list_of_dirs, key=os.path.getctime)
25
+
26
+ # Assuming YOLO saves the output with the same name in the latest 'predict' folder
27
+ output_image_name = os.path.basename(input_image_path)
28
+ output_image_path = os.path.join(latest_dir, output_image_name)
29
+
30
+ if os.path.exists(output_image_path):
31
+ output_img = Image.open(output_image_path)
32
+ return output_img
33
+ else:
34
+ return "No output image found."
35
+
36
+ # Define the Gradio interface
37
+ demo = gr.Interface(
38
+ fn=process_image,
39
+ inputs=gr.Image(type="pil"),
40
+ outputs=gr.Image(type="pil"),
41
+ title="Object Detection with YOLO",
42
+ description="Upload an image and the YOLO model will detect objects."
43
+ )
44
+
45
+ # Launch the app
46
+ demo.launch(share=True)
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:299df2cc15fc2b7a9db5a8fe8344658098ecbb0cdbac632c119ad655857d5551
3
+ size 22464291
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ numpy
2
+ pandas
3
+ ultralytics
4
+ albumentations
5
+ torchvision
6
+ torch