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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from anchor_utils import *
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from flask import Flask, request, send_file, make_response
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
+
from flask import Flask, request, jsonify, render_template
|
| 11 |
+
from rembg import remove
|
| 12 |
+
import subprocess
|
| 13 |
+
import shutil
|
| 14 |
+
|
| 15 |
+
# Set up paths
|
| 16 |
+
|
| 17 |
+
# image_dir = os.path.join(os.getcwd(), 'pifuhd', 'sample_images'
|
| 18 |
+
# output_dir = os.path.join(os.getcwd(), "pifuhd", "results")
|
| 19 |
+
ckpt_path = os.path.join(os.getcwd(), 'pifuhd', 'checkpoints', 'pifuhd.pt')
|
| 20 |
+
|
| 21 |
+
import io
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# os.chdir(os.path.join(os.getcwd(), 'lightweight-human-pose-estimation.pytorch'))
|
| 25 |
+
print('cwd', os.getcwd())
|
| 26 |
+
print(os.listdir(os.getcwd()))
|
| 27 |
+
|
| 28 |
+
import sys
|
| 29 |
+
sys.path.append(os.path.join(os.getcwd(), 'lightweight-human-pose-estimation.pytorch'))
|
| 30 |
+
# Import necessary modules from lightweight human pose estimation
|
| 31 |
+
from models.with_mobilenet import PoseEstimationWithMobileNet
|
| 32 |
+
from modules.keypoints import extract_keypoints, group_keypoints
|
| 33 |
+
from modules.load_state import load_state
|
| 34 |
+
from modules.pose import Pose
|
| 35 |
+
import demo
|
| 36 |
+
|
| 37 |
+
# Initialize Flask app
|
| 38 |
+
app = Flask(__name__)
|
| 39 |
+
|
| 40 |
+
UPLOAD_FOLDER = os.getenv('UPLOAD_FOLDER', '/tmp/sample_images')
|
| 41 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 42 |
+
|
| 43 |
+
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
| 44 |
+
os.makedirs(app.config['UPLOAD_FOLDER'])
|
| 45 |
+
|
| 46 |
+
image_dir = os.path.join(app.config['UPLOAD_FOLDER'])
|
| 47 |
+
print('img_dir', image_dir)
|
| 48 |
+
|
| 49 |
+
# Load pose estimation model
|
| 50 |
+
net = PoseEstimationWithMobileNet()
|
| 51 |
+
checkpoint = torch.load(os.path.join(os.getcwd(), "lightweight-human-pose-estimation.pytorch", "checkpoint_iter_370000.pth"), map_location='cpu')
|
| 52 |
+
load_state(net, checkpoint)
|
| 53 |
+
net = net.eval()
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Preprocess function to crop image
|
| 57 |
+
def get_rect(net, image_path, height_size=512):
|
| 58 |
+
stride = 8
|
| 59 |
+
upsample_ratio = 4
|
| 60 |
+
num_keypoints = Pose.num_kpts
|
| 61 |
+
img = cv2.imread(image_path, cv2.IMREAD_COLOR)
|
| 62 |
+
orig_img = img.copy()
|
| 63 |
+
heatmaps, pafs, scale, pad = demo.infer_fast(net, img, height_size, stride, upsample_ratio, cpu=True)
|
| 64 |
+
|
| 65 |
+
total_keypoints_num = 0
|
| 66 |
+
all_keypoints_by_type = []
|
| 67 |
+
for kpt_idx in range(num_keypoints):
|
| 68 |
+
total_keypoints_num += extract_keypoints(heatmaps[:, :, kpt_idx], all_keypoints_by_type, total_keypoints_num)
|
| 69 |
+
|
| 70 |
+
pose_entries, all_keypoints = group_keypoints(all_keypoints_by_type, pafs)
|
| 71 |
+
for kpt_id in range(all_keypoints.shape[0]):
|
| 72 |
+
all_keypoints[kpt_id, 0] = (all_keypoints[kpt_id, 0] * stride / upsample_ratio - pad[1]) / scale
|
| 73 |
+
all_keypoints[kpt_id, 1] = (all_keypoints[kpt_id, 1] * stride / upsample_ratio - pad[0]) / scale
|
| 74 |
+
|
| 75 |
+
rects = []
|
| 76 |
+
for n in range(len(pose_entries)):
|
| 77 |
+
if len(pose_entries[n]) == 0:
|
| 78 |
+
continue
|
| 79 |
+
pose_keypoints = np.ones((num_keypoints, 2), dtype=np.int32) * -1
|
| 80 |
+
valid_keypoints = []
|
| 81 |
+
for kpt_id in range(num_keypoints):
|
| 82 |
+
if pose_entries[n][kpt_id] != -1.0:
|
| 83 |
+
pose_keypoints[kpt_id, 0] = int(all_keypoints[int(pose_entries[n][kpt_id]), 0])
|
| 84 |
+
pose_keypoints[kpt_id, 1] = int(all_keypoints[int(pose_entries[n][kpt_id]), 1])
|
| 85 |
+
valid_keypoints.append([pose_keypoints[kpt_id, 0], pose_keypoints[kpt_id, 1]])
|
| 86 |
+
valid_keypoints = np.array(valid_keypoints)
|
| 87 |
+
|
| 88 |
+
if pose_entries[n][10] != -1.0 or pose_entries[n][13] != -1.0:
|
| 89 |
+
pmin = valid_keypoints.min(0)
|
| 90 |
+
pmax = valid_keypoints.max(0)
|
| 91 |
+
center = (0.5 * (pmax[:2] + pmin[:2])).astype(np.int32)
|
| 92 |
+
radius = int(0.65 * max(pmax[0] - pmin[0], pmax[1] - pmin[1]))
|
| 93 |
+
elif pose_entries[n][10] == -1.0 and pose_entries[n][13] == -1.0 and pose_entries[n][8] != -1.0 and pose_entries[n][11] != -1.0:
|
| 94 |
+
center = (0.5 * (pose_keypoints[8] + pose_keypoints[11])).astype(np.int32)
|
| 95 |
+
radius = int(1.45 * np.sqrt(((center[None, :] - valid_keypoints) ** 2).sum(1)).max(0))
|
| 96 |
+
center[1] += int(0.05 * radius)
|
| 97 |
+
else:
|
| 98 |
+
center = np.array([img.shape[1] // 2, img.shape[0] // 2])
|
| 99 |
+
radius = max(img.shape[1] // 2, img.shape[0] // 2)
|
| 100 |
+
|
| 101 |
+
x1 = center[0] - radius
|
| 102 |
+
y1 = center[1] - radius
|
| 103 |
+
|
| 104 |
+
rects.append([x1, y1, 2 * radius, 2 * radius])
|
| 105 |
+
|
| 106 |
+
return np.array(rects)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
from pifuhd.apps.simple_test import run_pifuhd
|
| 111 |
+
|
| 112 |
+
def process_image_pifuhd(image):
|
| 113 |
+
# Define paths
|
| 114 |
+
# image_path = os.path.join(os.getcwd(), image_dir, "uploaded_image.png")
|
| 115 |
+
image_path = os.path.join(app.config['UPLOAD_FOLDER'], "uploaded_image.png")
|
| 116 |
+
|
| 117 |
+
# obj_path = os.path.join(output_dir, "output.obj")
|
| 118 |
+
|
| 119 |
+
# Save the uploaded image
|
| 120 |
+
image.save(image_path)
|
| 121 |
+
|
| 122 |
+
# Preprocess the image to get rect
|
| 123 |
+
rects = get_rect(net, image_path)
|
| 124 |
+
rect_path = image_path.replace('.png', '_rect.txt')
|
| 125 |
+
np.savetxt(rect_path, rects, fmt='%d')
|
| 126 |
+
|
| 127 |
+
# Run PIFuHD and get the buffer
|
| 128 |
+
obj_buffer = run_pifuhd(
|
| 129 |
+
input_path=image_dir,
|
| 130 |
+
ckpt_path=ckpt_path,
|
| 131 |
+
resolution=256,
|
| 132 |
+
use_rect=True
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# # # Save the first buffer as an OBJ file
|
| 136 |
+
# with open(obj_path, 'wb') as f:
|
| 137 |
+
# f.write(obj_buffer[0].getvalue())
|
| 138 |
+
|
| 139 |
+
return obj_buffer[0]
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
@app.route('/')
|
| 143 |
+
def pifuhd_page():
|
| 144 |
+
return render_template('pifuhd.html')
|
| 145 |
+
|
| 146 |
+
@app.route('/generate-mesh', methods=['POST'])
|
| 147 |
+
def generate_mesh():
|
| 148 |
+
if 'file' not in request.files:
|
| 149 |
+
return "No file part", 400
|
| 150 |
+
|
| 151 |
+
file = request.files['file']
|
| 152 |
+
if file.filename == '':
|
| 153 |
+
return "No selected file", 400
|
| 154 |
+
|
| 155 |
+
if file:
|
| 156 |
+
# Convert the file to an image and process it
|
| 157 |
+
image = Image.open(file.stream)
|
| 158 |
+
obj_buffer = process_image_pifuhd(image)
|
| 159 |
+
|
| 160 |
+
# Create a BytesIO object to send the file
|
| 161 |
+
obj_bytes_io = io.BytesIO(obj_buffer.getvalue())
|
| 162 |
+
obj_bytes_io.seek(0) # Ensure the buffer is at the start
|
| 163 |
+
|
| 164 |
+
# Return the OBJ file in memory
|
| 165 |
+
return send_file(
|
| 166 |
+
obj_bytes_io,
|
| 167 |
+
as_attachment=True,
|
| 168 |
+
download_name='output.obj',
|
| 169 |
+
mimetype='application/octet-stream'
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
# Helper function to process images
|
| 178 |
+
def process_image(filename):
|
| 179 |
+
# input_path = f'{os.getcwd()}/sample_images/{filename}'
|
| 180 |
+
input_path = filename
|
| 181 |
+
output_path = f'{os.getcwd()}/AnchorUDF/pics/my_test/RENDER/1/0_0_00.jpg'
|
| 182 |
+
|
| 183 |
+
input_image = Image.open(input_path)
|
| 184 |
+
|
| 185 |
+
# Determine new size
|
| 186 |
+
if input_image.size[0] <= 600 or input_image.size[1] <= 600:
|
| 187 |
+
new_size = (500, 500)
|
| 188 |
+
elif input_image.size[0] >= 900 or input_image.size[1] >= 900:
|
| 189 |
+
new_size = (1500, 1500)
|
| 190 |
+
elif input_image.size[0] <= 900 or input_image.size[1] <= 900 and input_image.size[0] >= 700 or input_image.size[1] >= 700:
|
| 191 |
+
new_size = (1000, 1000)
|
| 192 |
+
else:
|
| 193 |
+
new_size = input_image.size
|
| 194 |
+
|
| 195 |
+
resized_image = input_image.resize(new_size, Image.LANCZOS)
|
| 196 |
+
|
| 197 |
+
output_image = remove(resized_image)
|
| 198 |
+
rgb_output_image = output_image.convert('RGB')
|
| 199 |
+
|
| 200 |
+
# Save the processed image
|
| 201 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 202 |
+
rgb_output_image.save(output_path, quality=200, optimize=True)
|
| 203 |
+
|
| 204 |
+
return output_path
|
| 205 |
+
|
| 206 |
+
# Helper function to generate mask
|
| 207 |
+
def generate_mask(image_path):
|
| 208 |
+
image = cv2.imread(image_path)
|
| 209 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 210 |
+
_, binary = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
|
| 211 |
+
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 212 |
+
mask = np.zeros_like(gray)
|
| 213 |
+
cv2.drawContours(mask, contours, -1, 255, thickness=cv2.FILLED)
|
| 214 |
+
|
| 215 |
+
mask_path = f"{os.getcwd()}/AnchorUDF/pics/my_test/MASK/1/0_0_00.png"
|
| 216 |
+
cv2.imwrite(mask_path, mask)
|
| 217 |
+
|
| 218 |
+
return mask_path
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# Helper function for mesh processing and returning cleaned .ply file
|
| 222 |
+
def process_mesh(scale_factor=1.0, nb_neighbors=40, std_ratio=1.0, voxel_size=0.005):
|
| 223 |
+
off_path = None
|
| 224 |
+
al_obj = glob.glob(f'{os.getcwd()}/AnchorUDF/out/out/*.off')
|
| 225 |
+
# print('all_obg', al_obj)
|
| 226 |
+
for i in al_obj:
|
| 227 |
+
if os.path.getsize(i) > 30000:
|
| 228 |
+
print('found off file', i)
|
| 229 |
+
off_path = i
|
| 230 |
+
break
|
| 231 |
+
|
| 232 |
+
if not off_path:
|
| 233 |
+
raise FileNotFoundError("No suitable OFF file found.")
|
| 234 |
+
|
| 235 |
+
filename = 'output'
|
| 236 |
+
saved_ply_path = f"{off_path}_{filename}.ply"
|
| 237 |
+
|
| 238 |
+
convert_off_to_ply(off_path, saved_ply_path, scale_factor)
|
| 239 |
+
|
| 240 |
+
cleaned_ply_path, cleaned_pcd_path = remove_outliers_and_convert_to_point_cloud(saved_ply_path, nb_neighbors, std_ratio, voxel_size)
|
| 241 |
+
|
| 242 |
+
return cleaned_ply_path, cleaned_pcd_path
|
| 243 |
+
|
| 244 |
+
# Helper function for garment inference
|
| 245 |
+
def run_garment_inference():
|
| 246 |
+
# os.makedirs("out", exist_ok=True)
|
| 247 |
+
print('garm working dir', os.getcwd())
|
| 248 |
+
shutil.rmtree(os.path.join(os.getcwd(), "out"), ignore_errors=True)
|
| 249 |
+
|
| 250 |
+
command = [
|
| 251 |
+
"python", "-m", "apps.eval", "--results_path", "out", "--name", "out",
|
| 252 |
+
"--dataroot", f'{os.getcwd()}/AnchorUDF/pics/my_test', "--test_folder_path", "1",
|
| 253 |
+
"--load_netG_checkpoint_path", os.path.join(os.getcwd(), 'netG_epoch_59'),
|
| 254 |
+
"--anchor", "--num_steps", "2", "--filter_val", "0.0075",
|
| 255 |
+
"--max_dist", "0.05", "--num_gen_points", "850000", "--gamma", "1",
|
| 256 |
+
"--aug_bri", "1.5", "--aug_con", "0.5", "--aug_sat", "0.5", "--aug_hue", "0.5"
|
| 257 |
+
]
|
| 258 |
+
|
| 259 |
+
subprocess.run(command, cwd=os.path.join(os.getcwd(), "AnchorUDF"))
|
| 260 |
+
print('garment generated!!!')
|
| 261 |
+
|
| 262 |
+
@app.route('/process', methods=['POST'])
|
| 263 |
+
def process():
|
| 264 |
+
data = request.json
|
| 265 |
+
filename = data.get('filename')
|
| 266 |
+
|
| 267 |
+
if not filename:
|
| 268 |
+
return jsonify({"error": "Filename is required"}), 400
|
| 269 |
+
|
| 270 |
+
processed_image_path = process_image(filename)
|
| 271 |
+
mask_path = generate_mask(processed_image_path)
|
| 272 |
+
run_garment_inference()
|
| 273 |
+
|
| 274 |
+
try:
|
| 275 |
+
print('generating ply...')
|
| 276 |
+
cleaned_ply_path = process_mesh(scale_factor=0.5, nb_neighbors=40, std_ratio=1.0, voxel_size=0.003)
|
| 277 |
+
print('ply generated')
|
| 278 |
+
except FileNotFoundError as e:
|
| 279 |
+
import traceback
|
| 280 |
+
traceback.print_exc()
|
| 281 |
+
return jsonify({"error": str(e)}), 500
|
| 282 |
+
|
| 283 |
+
return send_file(cleaned_ply_path, as_attachment=True)
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
# Run the Flask app
|
| 287 |
+
if __name__ == "__main__":
|
| 288 |
+
app.run(host='0.0.0.0', port=6100)
|