Spaces:
Paused
Paused
Update app.py
Browse files117 unmodified lines118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275117 unmodified linesimport base64from io import BytesIOfrom PIL import Imageimport requestsimport torchimport numpy as npimport cv2import jsonimport osimport gradio as grfrom detectron2.engine import DefaultPredictorfrom detectron2.config import get_cfgfrom detectron2 import model_zoo# === Save to DB function ===def save_to_database(measurements, image, user_height_cm, user_id):if not user_id:return "❌ user_id missing from URL."if measurements is None or image is None:return "⚠️ No data to save."buffered = BytesIO()pil_image = Image.fromarray(image)pil_image.save(buffered, format="JPEG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")payload = {"imageBase64": img_str,"heightCm": user_height_cm,"waistCircumferenceCm": measurements.get("Waist Circumference (cm)"),"hipCircumferenceCm": measurements.get("Hip Circumference (cm)"),"shoulderWidthCm": measurements.get("Shoulder Width (cm)"),"torsoLengthCm": measurements.get("Torso Length (Neck to Pelvis, cm)"),"fullArmLengthCm": measurements.get("Arm Length (Shoulder to Wrist, cm)"),"neckToKneeLengthCm": measurements.get("Neck to Knee Length (cm)"),}try:response = requests.post(f"https://7da2-2409-4042-6e81-1806-de6-b8e5-836c-6b95.ngrok-free.app/upload/{user_id}",json=payload)if response.status_code == 201:return "✅ Measurements and image saved to database!"else:return f"❌ Failed: {response.status_code} - {response.text}"except Exception as e:return f"⚠️ Error during save: {str(e)}"# Setup Detectron2 Keypoint R-CNN modeloutput_dir = "key/"os.makedirs(output_dir, exist_ok=True)output_file = os.path.join(output_dir, "keypoints.json")cfg = get_cfg()cfg.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_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)outputs = predictor(image_np)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_WRIST = 9L_HIP, R_HIP = 11, 12L_KNEE, R_KNEE = 13, 14L_ANKLE, R_ANKLE = 15, 16skeleton = [(L_SHOULDER, R_SHOULDER), (L_SHOULDER, L_HIP), (R_SHOULDER, R_HIP), (L_HIP, R_HIP)]# Draw keypoints and skeleton for visualizationfor x, y in keypoints:cv2.circle(image_np, (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_np, (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, np.array(cv2.cvtColor(image_np, 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")user_id_input = gr.Textbox(label="🆔 User ID", placeholder="Enter your user ID")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")save_status = gr.Textbox(label="��� Save Status")def process_and_save(image, height, user_id):measurements, img_with_keypoints = process_image(image, height)if isinstance(measurements, str): # Means an error messagereturn measurements, None, ""save_msg = save_to_database(measurements, np.array(img_with_keypoints), height, user_id)return measurements, img_with_keypoints, save_msgsubmit_btn.click(fn=process_and_save,inputs=[image_input, height_input, user_id_input],outputs=[measurement_output, image_input, save_status])demo.launch()117 unmodified lines118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300117 unmodified linesimport gradio as grimport jsonimport base64import requestsfrom io import BytesIOimport torchimport numpy as npimport cv2from PIL import Imagefrom detectron2.engine import DefaultPredictorfrom detectron2.config import get_cfgfrom detectron2 import model_zoo# === Set up Detectron2 model ===cfg = get_cfg()cfg.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)# === Utility ===def get_distance(p1, p2):return np.linalg.norm(np.array(p1) - np.array(p2))# === Keypoint and Measurement Logic ===def process_image(image, user_height_cm):image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)outputs = predictor(image_cv)instances = outputs["instances"]keypoints = instances.pred_keypoints.cpu().numpy().tolist() if instances.has("pred_keypoints") else Noneif not keypoints:return "No keypoints detected.", None, Nonekeypoints = np.array(keypoints[0])[:, :2]# Draw keypoints and skeletonskeleton = [(5, 6), (5, 11), (6, 12), (11, 12)]for x, y in keypoints:cv2.circle(image_cv, (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_cv, (x1, y1), (x2, y2), (255, 0, 0), 2)# Body part indices from COCO keypointsNOSE, 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, 16# Calculate pixel height based on nose to midpoint of ankles (rough estimate)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.87 # Adjust factor to estimate full height if partially visiblepixels_per_cm = estimated_full_pixel_height / user_height_cm# Measurements in 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_point = ((keypoints[L_SHOULDER] + keypoints[R_SHOULDER]) / 2).tolist()torso_length_cm = get_distance(neck_point, pelvis) / pixels_per_cmarm_length_cm = get_distance(keypoints[L_SHOULDER], keypoints[L_WRIST]) / pixels_per_cm# Additional measurements# Neck circumference: approximate neck width * pineck_left = keypoints[L_SHOULDER]neck_right = keypoints[R_SHOULDER]neck_width_cm = get_distance(neck_left, neck_right) / pixels_per_cmneck_circumference_cm = neck_width_cm * np.pi# Calf circumference: approximate calf width at ankle * piankle_left = keypoints[L_ANKLE]ankle_right = keypoints[R_ANKLE]calf_width_cm = get_distance(ankle_left, ankle_right) / pixels_per_cmcalf_circumference_cm = calf_width_cm * np.pi# Waist and Hip circumferenceswaist_circumference_cm = waist_width_cm * np.pihip_circumference_cm = waist_circumference_cm / 0.75 # Approximate relationmeasurements = {"Waist Circumference (cm)": round(waist_circumference_cm, 2),"Hip Circumference (cm)": round(hip_circumference_cm, 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),"Neck Circumference (cm)": round(neck_circumference_cm, 2),"Calf Circumference (cm)": round(calf_circumference_cm, 2),}return measurements, cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB), keypoints.tolist()def save_to_database(measurements, image, user_height_cm, user_id):if not user_id:return "❌ user_id missing from URL."if measurements is None or image is None:return "⚠️ No data to save."buffered = BytesIO()pil_image = Image.fromarray(image)pil_image.save(buffered, format="JPEG")img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")payload = {"imageBase64": img_str,"heightCm": user_height_cm,"waistCircumferenceCm": measurements.get("Waist Circumference (cm)"),"hipCircumferenceCm": measurements.get("Hip Circumference (cm)"),"shoulderWidthCm": measurements.get("Shoulder Width (cm)"),"torsoLengthCm": measurements.get("Torso Length (Neck to Pelvis, cm)"),"fullArmLengthCm": measurements.get("Full Arm Length (Shoulder to Wrist, cm)"),"neckCircumferenceCm": measurements.get("Neck Circumference (cm)"),"calfCircumferenceCm": measurements.get("Calf Circumference (cm)"),}try:response = requests.post(f"https://7da2-2409-4042-6e81-1806-de6-b8e5-836c-6b95.ngrok-free.app/upload/{user_id}",json=payload)if response.status_code == 201:return "✅ Measurements and image saved to database!"else:return f"❌ Failed: {response.status_code} - {response.text}"except Exception as e:return f"⚠️ Error during save: {str(e)}"# === Gradio App ===with gr.Blocks() as demo:gr.Markdown("# 📏 AI-Powered Body Measurement Tool")user_id_state = gr.State()@demo.load(inputs=None, outputs=[user_id_state])def load_user_id(request: gr.Request):return request.query_params.get("user_id", "")with gr.Row():with gr.Column():image_input = gr.Image(label="Upload Your Full Body Image", type="pil")height_input = gr.Number(label="Your Real Height (in cm)")process_button = gr.Button("📐 Extract Measurements")with gr.Column():output_image = gr.Image(label="Detected Keypoints")measurement_output = gr.JSON(label="Body Measurements")with gr.Row():save_button = gr.Button("💾 Save to Backend")save_status = gr.Textbox(label="Status", interactive=False)# Store results for saveprocessed_img = gr.State()processed_data = gr.State()process_button.click(fn=process_image,inputs=[image_input, height_input],outputs=[measurement_output, output_image, processed_data]).then(fn=lambda img: img,inputs=[output_image],outputs=processed_img)save_button.click(fn=save_to_database,inputs=[measurement_output, processed_img, height_input, user_id_state],outputs=save_status)if __name__ == "__main__":demo.launch()
app.py+127-102