Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,15 +10,15 @@ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
|
| 10 |
# Prediction function
|
| 11 |
def predict_image(image: Image.Image):
|
| 12 |
try:
|
| 13 |
-
# Step 1: Preprocess the image with padding
|
| 14 |
inputs = processor(images=image, return_tensors="pt", padding=True)
|
| 15 |
|
| 16 |
-
# Step 2: Run inference
|
| 17 |
with torch.no_grad():
|
| 18 |
outputs = model(**inputs)
|
| 19 |
|
| 20 |
# Step 3: Post-process the outputs
|
| 21 |
-
target_sizes = torch.tensor([image.size[::-1]]) # image size
|
| 22 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.5)[0]
|
| 23 |
|
| 24 |
# Step 4: Draw bounding boxes on the image
|
|
|
|
| 10 |
# Prediction function
|
| 11 |
def predict_image(image: Image.Image):
|
| 12 |
try:
|
| 13 |
+
# Step 1: Preprocess the image with padding and ensure it is batched
|
| 14 |
inputs = processor(images=image, return_tensors="pt", padding=True)
|
| 15 |
|
| 16 |
+
# Step 2: Run inference (no gradients)
|
| 17 |
with torch.no_grad():
|
| 18 |
outputs = model(**inputs)
|
| 19 |
|
| 20 |
# Step 3: Post-process the outputs
|
| 21 |
+
target_sizes = torch.tensor([image.size[::-1]]) # image size reversed to match model format
|
| 22 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.5)[0]
|
| 23 |
|
| 24 |
# Step 4: Draw bounding boxes on the image
|