Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
from flask import Flask, render_template, request, redirect, url_for
|
2 |
-
from
|
3 |
-
from PIL import Image, ImageDraw
|
4 |
-
import torch
|
5 |
import os
|
6 |
import uuid
|
7 |
import logging
|
@@ -17,12 +15,6 @@ UPLOAD_FOLDER = 'static/uploads'
|
|
17 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
18 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
19 |
|
20 |
-
# Load DETR model and processor
|
21 |
-
logger.info("Loading DETR model and processor...")
|
22 |
-
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
23 |
-
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
24 |
-
logger.info("Model and processor loaded successfully.")
|
25 |
-
|
26 |
@app.route('/')
|
27 |
def index():
|
28 |
return render_template('index.html')
|
@@ -44,33 +36,14 @@ def upload_file():
|
|
44 |
file.save(filepath)
|
45 |
logger.info(f"File saved: {filename}")
|
46 |
|
47 |
-
#
|
48 |
image = Image.open(filepath).convert("RGB")
|
49 |
-
image = image.resize((800, 600)) # Resize for
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# Post-process outputs
|
54 |
-
target_sizes = torch.tensor([image.size[::-1]])
|
55 |
-
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
56 |
-
|
57 |
-
# Draw bounding boxes
|
58 |
-
draw = ImageDraw.Draw(image)
|
59 |
-
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
60 |
-
box = [round(i, 2) for i in box.tolist()]
|
61 |
-
label_str = model.config.id2label[label.item()]
|
62 |
-
draw.rectangle(box, outline="red", width=3)
|
63 |
-
draw.text((box[0], box[1]), f"{label_str}: {score:.2f}", fill="red")
|
64 |
-
|
65 |
-
# Save output image
|
66 |
-
output_filename = f"output_{filename}"
|
67 |
-
output_filepath = os.path.join(app.config['UPLOAD_FOLDER'], output_filename)
|
68 |
-
image.save(output_filepath)
|
69 |
-
logger.info(f"Processed image saved: {output_filename}")
|
70 |
-
|
71 |
return render_template('results.html',
|
72 |
-
|
73 |
-
processed_image=url_for('static', filename=f'uploads/{output_filename}'))
|
74 |
|
75 |
except Exception as e:
|
76 |
logger.error(f"Error processing file: {str(e)}")
|
|
|
1 |
from flask import Flask, render_template, request, redirect, url_for
|
2 |
+
from PIL import Image
|
|
|
|
|
3 |
import os
|
4 |
import uuid
|
5 |
import logging
|
|
|
15 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
16 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
@app.route('/')
|
19 |
def index():
|
20 |
return render_template('index.html')
|
|
|
36 |
file.save(filepath)
|
37 |
logger.info(f"File saved: {filename}")
|
38 |
|
39 |
+
# Verify and resize image
|
40 |
image = Image.open(filepath).convert("RGB")
|
41 |
+
image = image.resize((800, 600)) # Resize for consistent display
|
42 |
+
image.save(filepath) # Overwrite with resized image
|
43 |
+
logger.info(f"Image resized and saved: {filename}")
|
44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
return render_template('results.html',
|
46 |
+
image=url_for('static', filename=f'uploads/{filename}'))
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
logger.error(f"Error processing file: {str(e)}")
|