Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,96 @@
|
|
| 1 |
-
from flask import Flask, render_template, request
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import os
|
| 4 |
-
import torch
|
| 5 |
-
import cv2
|
| 6 |
-
import mediapipe as mp
|
| 7 |
-
from transformers import SamModel, SamProcessor
|
| 8 |
-
from diffusers.utils import load_image
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
|
| 12 |
-
UPLOAD_FOLDER = 'static/uploads'
|
| 13 |
-
OUTPUT_FOLDER = 'static/outputs'
|
| 14 |
-
|
| 15 |
-
# Ensure folders exist
|
| 16 |
-
os.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
app.run(debug=True, host='0.0.0.0', port=6000)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
import cv2
|
| 6 |
+
import mediapipe as mp
|
| 7 |
+
from transformers import SamModel, SamProcessor
|
| 8 |
+
from diffusers.utils import load_image
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
UPLOAD_FOLDER = 'static/uploads'
|
| 13 |
+
OUTPUT_FOLDER = 'static/outputs'
|
| 14 |
+
|
| 15 |
+
# Ensure folders exist
|
| 16 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
| 17 |
+
try:
|
| 18 |
+
os.makedirs(UPLOAD_FOLDER)
|
| 19 |
+
except FileExistsError:
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
if not os.path.exists(OUTPUT_FOLDER):
|
| 23 |
+
try:
|
| 24 |
+
os.makedirs(OUTPUT_FOLDER)
|
| 25 |
+
except FileExistsError:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
# Load model once at startup
|
| 29 |
+
model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50")
|
| 30 |
+
processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50")
|
| 31 |
+
|
| 32 |
+
# Pose function
|
| 33 |
+
def get_shoulder_coordinates(image_path):
|
| 34 |
+
mp_pose = mp.solutions.pose
|
| 35 |
+
pose = mp_pose.Pose()
|
| 36 |
+
image = cv2.imread(image_path)
|
| 37 |
+
if image is None:
|
| 38 |
+
return None
|
| 39 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 40 |
+
results = pose.process(image_rgb)
|
| 41 |
+
if results.pose_landmarks:
|
| 42 |
+
height, width, _ = image.shape
|
| 43 |
+
landmarks = results.pose_landmarks.landmark
|
| 44 |
+
left_shoulder = (int(landmarks[11].x * width), int(landmarks[11].y * height))
|
| 45 |
+
right_shoulder = (int(landmarks[12].x * width), int(landmarks[12].y * height))
|
| 46 |
+
print(left_shoulder)
|
| 47 |
+
print(right_shoulder)
|
| 48 |
+
return left_shoulder, right_shoulder
|
| 49 |
+
else:
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 53 |
+
def index():
|
| 54 |
+
if request.method == 'POST':
|
| 55 |
+
person_file = request.files['person_image']
|
| 56 |
+
tshirt_file = request.files['tshirt_image']
|
| 57 |
+
|
| 58 |
+
person_path = os.path.join(UPLOAD_FOLDER, 'person.jpg')
|
| 59 |
+
tshirt_path = os.path.join(UPLOAD_FOLDER, 'tshirt.png')
|
| 60 |
+
|
| 61 |
+
person_file.save(person_path)
|
| 62 |
+
tshirt_file.save(tshirt_path)
|
| 63 |
+
|
| 64 |
+
# Run your model
|
| 65 |
+
coordinates = get_shoulder_coordinates(person_path)
|
| 66 |
+
if coordinates is None:
|
| 67 |
+
return "No pose detected."
|
| 68 |
+
|
| 69 |
+
img = load_image(person_path)
|
| 70 |
+
new_tshirt = load_image(tshirt_path)
|
| 71 |
+
|
| 72 |
+
left_shoulder, right_shoulder = coordinates
|
| 73 |
+
input_points = [[[left_shoulder[0], left_shoulder[1]], [right_shoulder[0], right_shoulder[1]]]]
|
| 74 |
+
|
| 75 |
+
inputs = processor(img, input_points=input_points, return_tensors="pt")
|
| 76 |
+
outputs = model(**inputs)
|
| 77 |
+
|
| 78 |
+
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(),
|
| 79 |
+
inputs["original_sizes"].cpu(),
|
| 80 |
+
inputs["reshaped_input_sizes"].cpu())
|
| 81 |
+
|
| 82 |
+
mask_tensor = masks[0][0][2].to(dtype=torch.uint8)
|
| 83 |
+
mask = transforms.ToPILImage()(mask_tensor * 255)
|
| 84 |
+
|
| 85 |
+
new_tshirt = new_tshirt.resize(img.size, Image.LANCZOS)
|
| 86 |
+
img_with_new_tshirt = Image.composite(new_tshirt, img, mask)
|
| 87 |
+
|
| 88 |
+
result_path = os.path.join(OUTPUT_FOLDER, 'result.jpg')
|
| 89 |
+
img_with_new_tshirt.save(result_path)
|
| 90 |
+
|
| 91 |
+
return render_template('index.html', result_img='outputs/result.jpg')
|
| 92 |
+
|
| 93 |
+
return render_template('index.html')
|
| 94 |
+
|
| 95 |
+
if __name__ == '__main__':
|
| 96 |
app.run(debug=True, host='0.0.0.0', port=6000)
|