ayoubsa commited on
Commit
be1f96e
·
verified ·
1 Parent(s): f8b6069

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -5
app.py CHANGED
@@ -36,7 +36,7 @@ interface = gr.Interface(
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
@@ -47,14 +47,14 @@ from PIL import Image
47
  import torch
48
 
49
  # Load the YOLO model
50
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='model.pt') # Replace with your uploaded model's path
51
 
52
  # Load your dataset from Hugging Face
53
- dataset = load_dataset("username/your_dataset_name") # 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['train'][:]['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
 
@@ -77,5 +77,55 @@ iface = gr.Interface(
77
  )
78
 
79
  # Launch the Gradio app
80
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
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
 
 
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()