Spaces:
Paused
Paused
Update app.py
Browse files507 unmodified lines5085095105115125134 unmodified lines5185195205215225235243 unmodified lines528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633507 unmodified lines# if __name__ == "__main__":# demo.launch()import torchimport numpy as npimport cv24 unmodified linesfrom detectron2.config import get_cfgfrom detectron2 import model_zoo# Create output directory if it doesn't existoutput_dir = "key/"os.makedirs(output_dir, exist_ok=True)output_file = os.path.join(output_dir, "keypoints.json")3 unmodified linescfg.merge_from_file(model_zoo.get_config_file("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml"))cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml")cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"# Load the predictorpredictor = DefaultPredictor(cfg)def process_image(image, user_height_cm):# Convert Gradio image input to OpenCV formatimage = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)# Run keypoint detectionoutputs = predictor(image)# Extract keypointsinstances = outputs["instances"]keypoints = instances.pred_keypoints.cpu().numpy().tolist() if instances.has("pred_keypoints") else Noneif not keypoints:return "No keypoints detected.", None# Save keypoints to JSONwith open(output_file, "w") as f:json.dump({"keypoints": keypoints}, f, indent=4)keypoints = np.array(keypoints[0])[:, :2] # Extract (x, y) coordinates# COCO format indicesNOSE, L_SHOULDER, R_SHOULDER = 0, 5, 6L_ELBOW, R_ELBOW = 7, 8L_WRIST, R_WRIST = 9, 10L_HIP, R_HIP = 11, 12L_ANKLE, R_ANKLE = 15, 16# Define Keypoint Pairs for Drawing Lines (COCO Format)skeleton = [(5, 6), (5, 11), (6, 12), (11, 12)]# Draw Keypointsfor x, y in keypoints:cv2.circle(image, (int(x), int(y)), 5, (0, 255, 0), -1)# Draw Skeletonfor pt1, pt2 in skeleton:x1, y1 = map(int, keypoints[pt1])x2, y2 = map(int, keypoints[pt2])cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)# Function to calculate Euclidean distancedef get_distance(p1, p2):return np.linalg.norm(np.array(p1) - np.array(p2))# Calculate full height (consider head length)ankle_mid = ((keypoints[L_ANKLE] + keypoints[R_ANKLE]) / 2).tolist()pixel_height = get_distance(keypoints[NOSE], ankle_mid)# Estimated full body height (add approx head length)estimated_full_pixel_height = pixel_height / 0.87 # Since 87% = nose to anklepixels_per_cm = estimated_full_pixel_height / user_height_cm# Waist and shoulder measurementsshoulder_width_px = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER])waist_width_px = get_distance(keypoints[L_HIP], keypoints[R_HIP])# Convert to cmshoulder_width_cm = shoulder_width_px / pixels_per_cmwaist_width_cm = waist_width_px / pixels_per_cm# Torso Length (Neck to Pelvis)pelvis = ((keypoints[L_HIP] + keypoints[R_HIP]) / 2).tolist()neck = ((keypoints[L_SHOULDER] + keypoints[R_SHOULDER]) / 2).tolist()torso_length_px = get_distance(neck, pelvis)torso_length_cm = torso_length_px / pixels_per_cm# Arm Length (Shoulder to Wrist)arm_length_px = get_distance(keypoints[L_SHOULDER], keypoints[L_WRIST])arm_length_cm = arm_length_px / pixels_per_cm# Calculate waist and hip circumference (Ellipse approximation)# Waist circumference ≈ π × (waist_width / 2) × 2waist_circumference = np.pi * waist_width_cmhip_circumference = waist_circumference / 0.75 # Assuming hip is slightly bigger than waist# Improved body measurement calculationdef calculate_body_measurements(waist_circumference, hip_circumference, shoulder_width_cm, torso_length_cm, arm_length_cm):return {"Waist Circumference (cm)": round(waist_circumference, 2),"Hip Circumference (cm)": round(hip_circumference, 2),"Shoulder Width (cm)": round(shoulder_width_cm, 2),"Torso Length (Neck to Pelvis, cm)": round(torso_length_cm, 2),"Full Arm Length (Shoulder to Wrist, cm)": round(arm_length_cm, 2),}measurements = calculate_body_measurements(waist_circumference, hip_circumference, shoulder_width_cm, torso_length_cm, arm_length_cm)return measurements, cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# Gradio Interfacedemo = gr.Interface(fn=process_image,inputs=[gr.Image(type="pil"), gr.Number(label="User Height (cm)")],outputs=[gr.JSON(label="Measurements"), gr.Image(type="pil", label="Keypoint Overlay")],title="Keypoint Measurement Extractor",description="Upload an image, enter your height, and get body measurements based on keypoints.",)demo.launch()507 unmodified lines5085095105115125135144 unmodified lines5195205215225235245253 unmodified lines529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614507 unmodified lines# if __name__ == "__main__":# demo.launch()import torchimport numpy as npimport cv24 unmodified linesfrom detectron2.config import get_cfgfrom detectron2 import model_zoo# Output directoryoutput_dir = "key/"os.makedirs(output_dir, exist_ok=True)output_file = os.path.join(output_dir, "keypoints.json")3 unmodified linescfg.merge_from_file(model_zoo.get_config_file("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml"))cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml")cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"predictor = DefaultPredictor(cfg)def process_image(image, user_height_cm):image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)outputs = predictor(image)instances = outputs["instances"]keypoints = instances.pred_keypoints.cpu().numpy().tolist() if instances.has("pred_keypoints") else Noneif not keypoints:return "No keypoints detected.", Nonewith open(output_file, "w") as f:json.dump({"keypoints": keypoints}, f, indent=4)keypoints = np.array(keypoints[0])[:, :2]NOSE, L_SHOULDER, R_SHOULDER = 0, 5, 6L_ELBOW, R_ELBOW = 7, 8L_WRIST, R_WRIST = 9, 10L_HIP, R_HIP = 11, 12L_KNEE, R_KNEE = 13, 14L_ANKLE, R_ANKLE = 15, 16skeleton = [(5, 6), (5, 11), (6, 12), (11, 12)]for x, y in keypoints:cv2.circle(image, (int(x), int(y)), 5, (0, 255, 0), -1)for pt1, pt2 in skeleton:x1, y1 = map(int, keypoints[pt1])x2, y2 = map(int, keypoints[pt2])cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)def get_distance(p1, p2):return np.linalg.norm(np.array(p1) - np.array(p2))ankle_mid = ((keypoints[L_ANKLE] + keypoints[R_ANKLE]) / 2).tolist()pixel_height = get_distance(keypoints[NOSE], ankle_mid)estimated_full_pixel_height = pixel_height / 0.87pixels_per_cm = estimated_full_pixel_height / user_height_cmshoulder_width_cm = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER]) / pixels_per_cmwaist_width_cm = get_distance(keypoints[L_HIP], keypoints[R_HIP]) / pixels_per_cmpelvis = ((keypoints[L_HIP] + keypoints[R_HIP]) / 2).tolist()neck = ((keypoints[L_SHOULDER] + keypoints[R_SHOULDER]) / 2).tolist()torso_length_cm = get_distance(neck, pelvis) / pixels_per_cmarm_length_cm = get_distance(keypoints[L_SHOULDER], keypoints[L_WRIST]) / pixels_per_cmknee_mid = ((keypoints[L_KNEE] + keypoints[R_KNEE]) / 2).tolist()neck_to_knee_cm = get_distance(neck, knee_mid) / pixels_per_cmwaist_circumference = np.pi * waist_width_cmhip_circumference = waist_circumference / 0.75measurements = {"Shoulder Width (cm)": round(shoulder_width_cm, 2),"Waist Circumference (cm)": round(waist_circumference, 2),"Hip Circumference (cm)": round(hip_circumference, 2),"Torso Length (Neck to Pelvis, cm)": round(torso_length_cm, 2),"Arm Length (Shoulder to Wrist, cm)": round(arm_length_cm, 2),"Neck to Knee Length (cm)": round(neck_to_knee_cm, 2)}return measurements, cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# Gradio Interfacewith gr.Blocks() as demo:gr.Markdown("## 🧍 Keypoint-Based Body Measurement Tool")gr.Markdown("Upload a **full-body image** and enter your **height (in cm)** to estimate body measurements using AI-powered keypoint detection.")with gr.Row():with gr.Column():image_input = gr.Image(type="pil", label="📸 Upload Image")submit_btn = gr.Button("🔍 Generate Measurements")with gr.Column():height_input = gr.Number(label="📏 Your Height (cm)", value=170)measurement_output = gr.JSON(label="📐 Estimated Measurements")submit_btn.click(fn=process_image, inputs=[image_input, height_input], outputs=[measurement_output, image_output])demo.launch()
app.py+34-53