ritz26 commited on
Commit
ed0aacc
·
verified ·
1 Parent(s): 74c525a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -65
app.py CHANGED
@@ -6,89 +6,134 @@ 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
  try:
17
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
18
- except PermissionError:
19
- print(f"Permission denied for creating {UPLOAD_FOLDER}. Please check permissions.")
20
-
21
- try:
22
  os.makedirs(OUTPUT_FOLDER, exist_ok=True)
23
- except PermissionError:
24
- print(f"Permission denied for creating {OUTPUT_FOLDER}. Please check permissions.")
25
-
 
 
26
 
27
  # Load model once at startup
28
- model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50")
29
- processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50")
 
 
 
 
30
 
31
  # Pose function
32
  def get_shoulder_coordinates(image_path):
33
- mp_pose = mp.solutions.pose
34
- pose = mp_pose.Pose()
35
- image = cv2.imread(image_path)
36
- if image is None:
37
- return None
38
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
39
- results = pose.process(image_rgb)
40
- if results.pose_landmarks:
41
- height, width, _ = image.shape
42
- landmarks = results.pose_landmarks.landmark
43
- left_shoulder = (int(landmarks[11].x * width), int(landmarks[11].y * height))
44
- right_shoulder = (int(landmarks[12].x * width), int(landmarks[12].y * height))
45
- print(left_shoulder)
46
- print(right_shoulder)
47
- return left_shoulder, right_shoulder
48
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  return None
50
 
51
  @app.route('/', methods=['GET', 'POST'])
52
  def index():
53
  if request.method == 'POST':
54
- person_file = request.files['person_image']
55
- tshirt_file = request.files['tshirt_image']
56
-
57
- person_path = os.path.join(UPLOAD_FOLDER, 'person.jpg')
58
- tshirt_path = os.path.join(UPLOAD_FOLDER, 'tshirt.png')
59
-
60
- person_file.save(person_path)
61
- tshirt_file.save(tshirt_path)
62
-
63
- # Run your model
64
- coordinates = get_shoulder_coordinates(person_path)
65
- if coordinates is None:
66
- return "No pose detected."
67
-
68
- img = load_image(person_path)
69
- new_tshirt = load_image(tshirt_path)
70
-
71
- left_shoulder, right_shoulder = coordinates
72
- input_points = [[[left_shoulder[0], left_shoulder[1]], [right_shoulder[0], right_shoulder[1]]]]
73
-
74
- inputs = processor(img, input_points=input_points, return_tensors="pt")
75
- outputs = model(**inputs)
76
-
77
- masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(),
78
- inputs["original_sizes"].cpu(),
79
- inputs["reshaped_input_sizes"].cpu())
80
-
81
- mask_tensor = masks[0][0][2].to(dtype=torch.uint8)
82
- mask = transforms.ToPILImage()(mask_tensor * 255)
83
-
84
- new_tshirt = new_tshirt.resize(img.size, Image.LANCZOS)
85
- img_with_new_tshirt = Image.composite(new_tshirt, img, mask)
86
-
87
- result_path = os.path.join(OUTPUT_FOLDER, 'result.jpg')
88
- img_with_new_tshirt.save(result_path)
89
-
90
- return render_template('index.html', result_img='outputs/result.jpg')
91
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  return render_template('index.html')
93
 
94
  if __name__ == '__main__':
 
6
  import mediapipe as mp
7
  from transformers import SamModel, SamProcessor
8
  from diffusers.utils import load_image
9
+ from torchvision import transforms
10
+ import tempfile
11
 
12
  app = Flask(__name__)
13
 
14
+ # Use temporary directories for uploads and outputs
15
+ UPLOAD_FOLDER = '/tmp/uploads'
16
+ OUTPUT_FOLDER = '/tmp/outputs'
17
 
18
  # Ensure folders exist
19
  try:
20
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
 
 
 
 
21
  os.makedirs(OUTPUT_FOLDER, exist_ok=True)
22
+ # Also create static directories for serving files
23
+ os.makedirs('static/uploads', exist_ok=True)
24
+ os.makedirs('static/outputs', exist_ok=True)
25
+ except PermissionError as e:
26
+ print(f"Permission denied for creating directories: {e}")
27
 
28
  # Load model once at startup
29
+ try:
30
+ model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50")
31
+ processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50")
32
+ print("Models loaded successfully")
33
+ except Exception as e:
34
+ print(f"Error loading models: {e}")
35
 
36
  # Pose function
37
  def get_shoulder_coordinates(image_path):
38
+ try:
39
+ mp_pose = mp.solutions.pose
40
+ pose = mp_pose.Pose(
41
+ static_image_mode=True,
42
+ model_complexity=2,
43
+ enable_segmentation=False,
44
+ min_detection_confidence=0.5
45
+ )
46
+
47
+ image = cv2.imread(image_path)
48
+ if image is None:
49
+ print(f"Could not load image from {image_path}")
50
+ return None
51
+
52
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
53
+ results = pose.process(image_rgb)
54
+
55
+ if results.pose_landmarks:
56
+ height, width, _ = image.shape
57
+ landmarks = results.pose_landmarks.landmark
58
+
59
+ left_shoulder = (
60
+ int(landmarks[11].x * width),
61
+ int(landmarks[11].y * height)
62
+ )
63
+ right_shoulder = (
64
+ int(landmarks[12].x * width),
65
+ int(landmarks[12].y * height)
66
+ )
67
+
68
+ print(f"Left shoulder: {left_shoulder}")
69
+ print(f"Right shoulder: {right_shoulder}")
70
+ return left_shoulder, right_shoulder
71
+ else:
72
+ print("No pose landmarks detected")
73
+ return None
74
+
75
+ except Exception as e:
76
+ print(f"Error in pose detection: {e}")
77
  return None
78
 
79
  @app.route('/', methods=['GET', 'POST'])
80
  def index():
81
  if request.method == 'POST':
82
+ try:
83
+ person_file = request.files.get('person_image')
84
+ tshirt_file = request.files.get('tshirt_image')
85
+
86
+ if not person_file or not tshirt_file:
87
+ return "Please upload both person and t-shirt images."
88
+
89
+ # Save files to temporary directory
90
+ person_path = os.path.join(UPLOAD_FOLDER, 'person.jpg')
91
+ tshirt_path = os.path.join(UPLOAD_FOLDER, 'tshirt.png')
92
+
93
+ person_file.save(person_path)
94
+ tshirt_file.save(tshirt_path)
95
+
96
+ # Run your model
97
+ coordinates = get_shoulder_coordinates(person_path)
98
+ if coordinates is None:
99
+ return "No pose detected. Please try with a different image where the person's shoulders are clearly visible."
100
+
101
+ img = load_image(person_path)
102
+ new_tshirt = load_image(tshirt_path)
103
+
104
+ left_shoulder, right_shoulder = coordinates
105
+ input_points = [[[left_shoulder[0], left_shoulder[1]], [right_shoulder[0], right_shoulder[1]]]]
106
+
107
+ inputs = processor(img, input_points=input_points, return_tensors="pt")
108
+
109
+ with torch.no_grad():
110
+ outputs = model(**inputs)
111
+
112
+ masks = processor.image_processor.post_process_masks(
113
+ outputs.pred_masks.cpu(),
114
+ inputs["original_sizes"].cpu(),
115
+ inputs["reshaped_input_sizes"].cpu()
116
+ )
117
+
118
+ mask_tensor = masks[0][0][2].to(dtype=torch.uint8)
119
+ mask = transforms.ToPILImage()(mask_tensor * 255)
120
+
121
+ new_tshirt = new_tshirt.resize(img.size, Image.LANCZOS)
122
+ img_with_new_tshirt = Image.composite(new_tshirt, img, mask)
123
+
124
+ # Save result to both temp and static directories
125
+ result_path_temp = os.path.join(OUTPUT_FOLDER, 'result.jpg')
126
+ result_path_static = os.path.join('static/outputs', 'result.jpg')
127
+
128
+ img_with_new_tshirt.save(result_path_temp)
129
+ img_with_new_tshirt.save(result_path_static)
130
+
131
+ return render_template('index.html', result_img='outputs/result.jpg')
132
+
133
+ except Exception as e:
134
+ print(f"Error processing request: {e}")
135
+ return f"Error processing images: {str(e)}"
136
+
137
  return render_template('index.html')
138
 
139
  if __name__ == '__main__':