ayoubsa commited on
Commit
9327d3f
·
verified ·
1 Parent(s): 4e01a45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -160
app.py CHANGED
@@ -1,163 +1,3 @@
1
- """import gradio as gr
2
- import torch
3
- from PIL import Image
4
- from pathlib import Path
5
- import numpy as np
6
-
7
- # Load YOLO Model
8
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='https://huggingface.co/ayoubsa/yolo_model/blob/main/best.pt') # Replace 'model.pt' with your uploaded model's path
9
-
10
- # Function to make predictions
11
- def predict(image):
12
- # Convert input to PIL image if it's not
13
- if isinstance(image, np.ndarray):
14
- image = Image.fromarray(image)
15
-
16
- # Run inference
17
- results = model(image)
18
-
19
- # Extract predictions (bounding boxes, labels, confidence scores)
20
- predictions = results.pandas().xyxy[0] # Pandas dataframe
21
- annotated_image = np.array(results.render()[0]) # Annotated image as NumPy array
22
-
23
- return annotated_image, predictions[['name', 'confidence']].to_dict(orient="records")
24
-
25
- # Create Gradio Interface
26
- image_input = gr.inputs.Image(type="numpy", label="Input Image")
27
- output_image = gr.outputs.Image(type="numpy", label="Annotated Image")
28
- output_text = gr.outputs.JSON(label="Predictions (Labels & Confidence)")
29
-
30
- interface = gr.Interface(
31
- fn=predict,
32
- inputs=image_input,
33
- outputs=[output_image, output_text],
34
- title="YOLO Object Detection",
35
- description="Upload an image to detect objects using YOLO.",
36
- examples=["example1.jpg", "example2.jpg", "example3.jpg"] # Provide paths to example images
37
- )
38
-
39
- interface.launch()
40
-
41
-
42
- from datasets import load_dataset
43
- import gradio as gr
44
- import random
45
- import numpy as np
46
- from PIL import Image
47
- import torch
48
-
49
- # Load the YOLO model
50
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='https://huggingface.co/ayoubsa/yolo_model/blob/main/best.pt') # Replace with your uploaded model's path
51
-
52
- # Load your dataset from Hugging Face
53
- dataset = load_dataset("https://huggingface.co/datasets/ayoubsa/Sign_Road_Detection_Dataset/tree/main") # Replace with your dataset's repository name on Hugging Face
54
-
55
- # Function to get random examples from the dataset
56
- def get_random_examples(dataset, num_examples=3):
57
- images = dataset['test'][:]['image'] # Assuming the images are in the 'train' split and column 'image'
58
- random_examples = random.sample(images, num_examples)
59
- return random_examples
60
-
61
- # Define the prediction function
62
- def predict(image):
63
- results = model(image) # Perform object detection using YOLO
64
- results.render() # Render bounding boxes on the image
65
- output_image = Image.fromarray(results.imgs[0]) # Convert to PIL image
66
- return output_image
67
-
68
- # Get examples for Gradio app
69
- example_images = get_random_examples(dataset, num_examples=3)
70
-
71
- # Create the Gradio interface
72
- iface = gr.Interface(
73
- fn=predict,
74
- inputs=gr.inputs.Image(type="pil"), # PIL Image for compatibility with YOLO
75
- outputs=gr.outputs.Image(type="pil"),
76
- examples=example_images # Linking examples directly from Hugging Face dataset
77
- )
78
-
79
- # Launch the Gradio app
80
- iface.launch()
81
-
82
- import gradio as gr
83
- import torch
84
- from PIL import Image
85
- import zipfile
86
- import os
87
- import random
88
-
89
- # Define the paths for the model and dataset
90
- MODEL_PATH = 'https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt' # YOLO model file
91
- DATASET_PATH = 'test.zip' # The name of the uploaded test dataset zip file
92
-
93
- # Load the YOLO model
94
- model = torch.hub.load('ultralytics/yolov5', 'custom', path=MODEL_PATH)
95
-
96
- # Unzip the dataset
97
- if not os.path.exists("unzipped_test"):
98
- with zipfile.ZipFile(DATASET_PATH, 'r') as zip_ref:
99
- zip_ref.extractall("unzipped_test") # Extract images to this folder
100
-
101
- # Get all image paths from the unzipped folder
102
- image_folder = "unzipped_test"
103
- all_images = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if f.endswith(('.jpg', '.png', '.jpeg'))]
104
-
105
- # Function to get random examples
106
- def get_random_examples(num_examples=3):
107
- if len(all_images) >= num_examples:
108
- return random.sample(all_images, num_examples)
109
- else:
110
- return all_images # Return whatever is available if less than required
111
-
112
- # Define the prediction function
113
- def predict(image):
114
- results = model(image) # Perform object detection using YOLO
115
- results.render() # Render bounding boxes on the image
116
- output_image = Image.fromarray(results.imgs[0]) # Convert to PIL image
117
- return output_image
118
-
119
- # Get example images
120
- example_images = get_random_examples(num_examples=3)
121
-
122
- # Create the Gradio interface
123
- iface = gr.Interface(
124
- fn=predict,
125
- inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
126
- outputs=gr.outputs.Image(type="pil", label="Predicted Image with Bounding Boxes"),
127
- examples=example_images # Link the example images
128
- )
129
-
130
- # Launch the Gradio app
131
- iface.launch()
132
-
133
- import gradio as gr
134
- import torch
135
- from PIL import Image
136
- import numpy as np
137
- from ultralytics import YOLO
138
-
139
- # Load the YOLO model
140
- MODEL_URL= 'https://huggingface.co/ayoubsa/yolo_model/resolve/main/best.pt'
141
- model = YOLO(MODEL_URL)
142
-
143
- # Define the prediction function
144
- def predict(image):
145
- results = model(image) # Perform object detection using YOLO
146
- results.render() # Render bounding boxes on the image
147
- output_image = Image.fromarray(results.imgs[0]) # Convert to PIL image
148
- return output_image
149
-
150
- # Create the Gradio interface
151
- iface = gr.Interface(
152
- fn=predict,
153
- inputs=gr.Image(type="pil", label="Upload an Image"), # Upload input as PIL Image
154
- outputs=gr.Image(type="pil", label="Predicted Image with Bounding Boxes"), # Output image
155
- title="Object Detection App",
156
- description="Upload an image, and the YOLO model will detect objects in it."
157
- )
158
-
159
- # Launch the Gradio app
160
- iface.launch()"""
161
  from PIL import Image
162
  import numpy as np
163
  from ultralytics import YOLO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from PIL import Image
2
  import numpy as np
3
  from ultralytics import YOLO