Aumkeshchy2003 commited on
Commit
bccf53b
·
verified ·
1 Parent(s): 2492806

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -12,7 +12,7 @@ from torchvision.transforms import functional as F
12
  from PIL import Image
13
  import cv2
14
  import gradio as gr
15
- import numpy as np # Add this import
16
 
17
  from yolov5.models.yolo import Model
18
  from yolov5.utils.general import non_max_suppression
@@ -25,17 +25,17 @@ print("Model loaded successfully")
25
 
26
  def preprocess_image(image):
27
  try:
28
- image = image.convert("RGB") # Ensure image is in RGB mode
29
- image_tensor = F.to_tensor(image)
30
  print(f"Preprocessed image tensor: {image_tensor.shape}")
31
- return image_tensor.unsqueeze(0).to(device)
32
  except Exception as e:
33
  print(f"Error in preprocessing image: {e}")
34
  return None
35
 
36
  def draw_boxes(image, outputs, threshold=0.3):
37
  try:
38
- image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
39
  h, w, _ = image.shape
40
 
41
  for box in outputs:
@@ -64,7 +64,7 @@ def detect_objects(image):
64
  print("No objects detected.")
65
  return image
66
  print(f"Filtered outputs: {outputs}")
67
- result_image = draw_boxes(image, outputs)
68
  return result_image
69
  except Exception as e:
70
  print(f"Error in detecting objects: {e}")
@@ -72,9 +72,11 @@ def detect_objects(image):
72
 
73
  iface = gr.Interface(
74
  fn=detect_objects,
75
- inputs=gr.Image(type="pil"), # Removed source="webcam"
76
- outputs=gr.Image(type="pil"),
77
- live=True
 
78
  )
79
 
80
- iface.launch()
 
 
12
  from PIL import Image
13
  import cv2
14
  import gradio as gr
15
+ import numpy as np
16
 
17
  from yolov5.models.yolo import Model
18
  from yolov5.utils.general import non_max_suppression
 
25
 
26
  def preprocess_image(image):
27
  try:
28
+ image = Image.fromarray(image) # Convert numpy array to PIL Image
29
+ image_tensor = F.to_tensor(image).unsqueeze(0).to(device)
30
  print(f"Preprocessed image tensor: {image_tensor.shape}")
31
+ return image_tensor
32
  except Exception as e:
33
  print(f"Error in preprocessing image: {e}")
34
  return None
35
 
36
  def draw_boxes(image, outputs, threshold=0.3):
37
  try:
38
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
39
  h, w, _ = image.shape
40
 
41
  for box in outputs:
 
64
  print("No objects detected.")
65
  return image
66
  print(f"Filtered outputs: {outputs}")
67
+ result_image = draw_boxes(image, outputs.cpu().numpy())
68
  return result_image
69
  except Exception as e:
70
  print(f"Error in detecting objects: {e}")
 
72
 
73
  iface = gr.Interface(
74
  fn=detect_objects,
75
+ inputs=gr.Image(type="numpy"),
76
+ outputs=gr.Image(type="numpy"),
77
+ title="YOLOv5 Object Detection",
78
+ description="Upload an image to detect objects using the YOLOv5 model."
79
  )
80
 
81
+ if __name__ == "__main__":
82
+ iface.launch()