SakshiRathi77 commited on
Commit
192a5c6
1 Parent(s): aceebcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -33
app.py CHANGED
@@ -141,37 +141,41 @@
141
  # gradio_app.launch(debug=True)
142
 
143
  # make sure you have the following dependencies
 
144
  import torch
145
- import numpy as np
146
- from models.common import DetectMultiBackend
147
- from utils.general import non_max_suppression, scale_boxes
148
- from utils.torch_utils import select_device, smart_inference_mode
149
- from utils.augmentations import letterbox
150
- import PIL.Image
151
- from huggingface_hub import hf_hub_download
152
- hf_hub_download("SakshiRathi77/void-space-detection", filename="weights/best.pt", local_dir="./")
153
-
154
-
155
- @smart_inference_mode()
156
- def predict(image_path, weights='best.pt', imgsz=640, conf_thres=0.1, iou_thres=0.45):
157
- # Initialize
158
- device = select_device('0')
159
- # model = DetectMultiBackend(weights='best.pt', device="0", fp16=False, data='data/coco.yaml')
160
- model = DetectMultiBackend(weights='best.pt', device="0", fp16=False)
161
- stride, names, pt = model.stride, model.names, model.pt
162
-
163
- # Load image
164
- image = np.array(PIL.Image.open(image_path))
165
- img = letterbox(img0, imgsz, stride=stride, auto=True)[0]
166
- img = img[:, :, ::-1].transpose(2, 0, 1)
167
- img = np.ascontiguousarray(img)
168
- img = torch.from_numpy(img).to(device).float()
169
- img /= 255.0
170
- if img.ndimension() == 3:
171
- img = img.unsqueeze(0)
172
-
173
- # Inference
174
- # pred = model(img, augment=False, visualize=False)
175
- pred = model(img)
176
- # Apply NMS
177
- pred = non_max_suppression(pred[0][0], conf_thres, iou_thres, classes=None, max_det=1000)
 
 
 
 
141
  # gradio_app.launch(debug=True)
142
 
143
  # make sure you have the following dependencies
144
+ import gradio as gr
145
  import torch
146
+ from torchvision import transforms
147
+ from PIL import Image
148
+
149
+ # Load the YOLOv9 model
150
+ model_path = "best.pt" # Replace with the path to your YOLOv9 model
151
+ model = torch.load(model_path)
152
+
153
+ # Define preprocessing transforms
154
+ preprocess = transforms.Compose([
155
+ transforms.Resize((640, 640)), # Resize image to model input size
156
+ transforms.ToTensor(), # Convert image to tensor
157
+ ])
158
+
159
+ # Define a function to perform inference
160
+ def detect_void(image):
161
+ # Preprocess the input image
162
+ image = Image.fromarray(image)
163
+ image = preprocess(image).unsqueeze(0) # Add batch dimension
164
+
165
+ # Perform inference
166
+ with torch.no_grad():
167
+ output = model(image)
168
+
169
+ # Post-process the output if needed
170
+ # For example, draw bounding boxes on the image
171
+
172
+ # Convert the image back to numpy array
173
+ # and return the result
174
+ return output.squeeze().numpy()
175
+
176
+ # Define Gradio interface components
177
+ input_image = gr.inputs.Image(shape=(640, 640), label="Input Image")
178
+ output_image = gr.outputs.Image(label="Output Image")
179
+
180
+ # Create Gradio interface
181
+ gr.Interface(fn=detect_void, inputs=input_image, outputs=output_image, title="Void Detection App").launch()