ritz26 commited on
Commit
10b8ce5
·
verified ·
1 Parent(s): f342634

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -86
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.makedirs(UPLOAD_FOLDER, exist_ok=True)
17
- os.makedirs(OUTPUT_FOLDER, exist_ok=True)
18
-
19
- # Load model once at startup
20
- model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50")
21
- processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50")
22
-
23
- # Pose function
24
- def get_shoulder_coordinates(image_path):
25
- mp_pose = mp.solutions.pose
26
- pose = mp_pose.Pose()
27
- image = cv2.imread(image_path)
28
- if image is None:
29
- return None
30
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
31
- results = pose.process(image_rgb)
32
- if results.pose_landmarks:
33
- height, width, _ = image.shape
34
- landmarks = results.pose_landmarks.landmark
35
- left_shoulder = (int(landmarks[11].x * width), int(landmarks[11].y * height))
36
- right_shoulder = (int(landmarks[12].x * width), int(landmarks[12].y * height))
37
- print(left_shoulder)
38
- print(right_shoulder)
39
- return left_shoulder, right_shoulder
40
- else:
41
- return None
42
-
43
- @app.route('/', methods=['GET', 'POST'])
44
- def index():
45
- if request.method == 'POST':
46
- person_file = request.files['person_image']
47
- tshirt_file = request.files['tshirt_image']
48
-
49
- person_path = os.path.join(UPLOAD_FOLDER, 'person.jpg')
50
- tshirt_path = os.path.join(UPLOAD_FOLDER, 'tshirt.png')
51
-
52
- person_file.save(person_path)
53
- tshirt_file.save(tshirt_path)
54
-
55
- # Run your model
56
- coordinates = get_shoulder_coordinates(person_path)
57
- if coordinates is None:
58
- return "No pose detected."
59
-
60
- img = load_image(person_path)
61
- new_tshirt = load_image(tshirt_path)
62
-
63
- left_shoulder, right_shoulder = coordinates
64
- input_points = [[[left_shoulder[0], left_shoulder[1]], [right_shoulder[0], right_shoulder[1]]]]
65
-
66
- inputs = processor(img, input_points=input_points, return_tensors="pt")
67
- outputs = model(**inputs)
68
-
69
- masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(),
70
- inputs["original_sizes"].cpu(),
71
- inputs["reshaped_input_sizes"].cpu())
72
-
73
- mask_tensor = masks[0][0][2].to(dtype=torch.uint8)
74
- mask = transforms.ToPILImage()(mask_tensor * 255)
75
-
76
- new_tshirt = new_tshirt.resize(img.size, Image.LANCZOS)
77
- img_with_new_tshirt = Image.composite(new_tshirt, img, mask)
78
-
79
- result_path = os.path.join(OUTPUT_FOLDER, 'result.jpg')
80
- img_with_new_tshirt.save(result_path)
81
-
82
- return render_template('index.html', result_img='outputs/result.jpg')
83
-
84
- return render_template('index.html')
85
-
86
- if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
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)