eho69 commited on
Commit
63fdff1
·
verified ·
1 Parent(s): 7d9bb6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
app.py CHANGED
@@ -4,15 +4,25 @@ import numpy as np
4
  from PIL import Image
5
  import torch
6
  from transformers import DetrImageProcessor, DetrForObjectDetection
 
7
 
8
  # Load DETR model for object detection
9
- processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
10
- model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
 
 
 
 
 
 
11
 
12
  def detect_objects(image):
13
  """
14
  Detect objects in an image using DETR model
15
  """
 
 
 
16
  # Convert PIL image to numpy array
17
  if isinstance(image, Image.Image):
18
  image = np.array(image)
@@ -25,7 +35,9 @@ def detect_objects(image):
25
 
26
  # Prepare image for the model
27
  inputs = processor(images=image, return_tensors="pt")
28
- outputs = model(**inputs)
 
 
29
 
30
  # Post-process outputs
31
  target_sizes = torch.tensor([image.shape[:2]])
@@ -83,6 +95,9 @@ def process_image(image, mode):
83
  """
84
  Process image based on selected mode
85
  """
 
 
 
86
  if mode == "Object Detection":
87
  return detect_objects(image)
88
  else: # Edge Detection
@@ -90,7 +105,7 @@ def process_image(image, mode):
90
  return edges, "Edge detection completed"
91
 
92
  # Create Gradio interface
93
- with gr.Blocks(title="Object Scanner") as demo:
94
  gr.Markdown("# 🔍 Object Scanner with OpenCV & Hugging Face")
95
  gr.Markdown("Upload an image to detect objects or scan edges using computer vision")
96
 
@@ -109,16 +124,17 @@ with gr.Blocks(title="Object Scanner") as demo:
109
  output_text = gr.Markdown(label="Detection Results")
110
 
111
  # Examples
112
- gr.Examples(
113
- examples=[
114
- ["examples/sample1.jpg", "Object Detection"],
115
- ["examples/sample2.jpg", "Edge Detection"],
116
- ],
117
- inputs=[input_image, mode],
118
- outputs=[output_image, output_text],
119
- fn=process_image,
120
- cache_examples=False,
121
- )
 
122
 
123
  scan_btn.click(
124
  fn=process_image,
@@ -127,4 +143,4 @@ with gr.Blocks(title="Object Scanner") as demo:
127
  )
128
 
129
  if __name__ == "__main__":
130
- demo.launch()
 
4
  from PIL import Image
5
  import torch
6
  from transformers import DetrImageProcessor, DetrForObjectDetection
7
+ import os
8
 
9
  # Load DETR model for object detection
10
+ # We use try-except to handle potential model loading issues in constrained environments
11
+ try:
12
+ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
13
+ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
14
+ except Exception as e:
15
+ print(f"Error loading model: {e}")
16
+ processor = None
17
+ model = None
18
 
19
  def detect_objects(image):
20
  """
21
  Detect objects in an image using DETR model
22
  """
23
+ if model is None or processor is None:
24
+ return image, "Model failed to load. Please check logs."
25
+
26
  # Convert PIL image to numpy array
27
  if isinstance(image, Image.Image):
28
  image = np.array(image)
 
35
 
36
  # Prepare image for the model
37
  inputs = processor(images=image, return_tensors="pt")
38
+
39
+ with torch.no_grad():
40
+ outputs = model(**inputs)
41
 
42
  # Post-process outputs
43
  target_sizes = torch.tensor([image.shape[:2]])
 
95
  """
96
  Process image based on selected mode
97
  """
98
+ if image is None:
99
+ return None, "Please upload an image."
100
+
101
  if mode == "Object Detection":
102
  return detect_objects(image)
103
  else: # Edge Detection
 
105
  return edges, "Edge detection completed"
106
 
107
  # Create Gradio interface
108
+ with gr.Blocks(title="Object Scanner", theme=gr.themes.Soft()) as demo:
109
  gr.Markdown("# 🔍 Object Scanner with OpenCV & Hugging Face")
110
  gr.Markdown("Upload an image to detect objects or scan edges using computer vision")
111
 
 
124
  output_text = gr.Markdown(label="Detection Results")
125
 
126
  # Examples
127
+ if os.path.exists("examples"):
128
+ gr.Examples(
129
+ examples=[
130
+ ["examples/sample1.jpg", "Object Detection"],
131
+ ["examples/sample2.jpg", "Edge Detection"],
132
+ ],
133
+ inputs=[input_image, mode],
134
+ outputs=[output_image, output_text],
135
+ fn=process_image,
136
+ cache_examples=False,
137
+ )
138
 
139
  scan_btn.click(
140
  fn=process_image,
 
143
  )
144
 
145
  if __name__ == "__main__":
146
+ demo.launch()