Spaces:
Paused
Paused
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351424 unmodified lines5395405415425435445457 unmodified lines55355455555655755855956056156256356456556656726 unmodified lines594595596597598599600601602603604605606607608609610611612613614# import torch# import numpy as np# import cv2# import json# import os# import gradio as gr# from detectron2.detectron2.engine import DefaultPredictor# from detectron2.detectron2.config import get_cfg# from detectron2 import model_zoo# import torch_utils# import dnnlib# # Create output directory if it doesn't exist# output_dir = "key/"# os.makedirs(output_dir, exist_ok=True)# output_file = os.path.join(output_dir, "keypoints.json")# # Load pre-trained Keypoint R-CNN 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"# # Load the predictor# predictor = DefaultPredictor(cfg)# def process_image(image, user_height_cm):# # Convert Gradio image input to OpenCV format# image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)# # Run keypoint detection# outputs = predictor(image)# # Extract keypoints# instances = outputs["instances"]# keypoints = instances.pred_keypoints.cpu().numpy().tolist() if instances.has("pred_keypoints") else None# if not keypoints:# return "No keypoints detected.", None# # Save keypoints to JSON# with 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 indices# NOSE, L_SHOULDER, R_SHOULDER = 0, 5, 6# L_ELBOW, R_ELBOW = 7, 8# L_WRIST, R_WRIST = 9, 10# L_HIP, R_HIP = 11, 12# L_ANKLE, R_ANKLE = 15, 16# # Define Keypoint Pairs for Drawing Lines (COCO Format)# skeleton = [(5, 6), (5, 11), (6, 12), (11, 12)]# # Draw Keypoints# for x, y in keypoints:# cv2.circle(image, (int(x), int(y)), 5, (0, 255, 0), -1)# # Draw Skeleton# 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)# # Function to calculate Euclidean distance# def 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 ankle# pixels_per_cm = estimated_full_pixel_height / user_height_cm# # Waist and shoulder measurements# shoulder_width_px = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER])# waist_width_px = get_distance(keypoints[L_HIP], keypoints[R_HIP])# # Convert to cm# shoulder_width_cm = shoulder_width_px / pixels_per_cm# waist_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) Γ 2# waist_circumference = np.pi * waist_width_cm# hip_circumference = waist_circumference / 0.75 # Assuming hip is slightly bigger than waist# # Improved body measurement calculation# def 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 Interface# demo = 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()# from PIL import Image # Importing Image from PIL (Pillow)# import json# import base64# import requests# from io import BytesIO# import torch# import numpy as np# import cv2# import json# import os# import gradio as gr# from detectron2.detectron2.engine import DefaultPredictor# from detectron2.detectron2.config import get_cfg# from detectron2 import model_zoo# import torch_utils# import dnnlib# # Create output directory if it doesn't exist# output_dir = "key/"# os.makedirs(output_dir, exist_ok=True)# output_file = os.path.join(output_dir, "keypoints.json")# # Load pre-trained Keypoint R-CNN 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"# # Load the predictor# predictor = DefaultPredictor(cfg)# def process_image(image, user_height_cm):# # Convert Gradio image input to OpenCV format# image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)# # Run keypoint detection# outputs = predictor(image)# # Extract keypoints# instances = outputs["instances"]# keypoints = instances.pred_keypoints.cpu().numpy().tolist() if instances.has("pred_keypoints") else None# if not keypoints:# return "No keypoints detected.", None# # Save keypoints to JSON# with 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 indices# NOSE, L_SHOULDER, R_SHOULDER = 0, 5, 6# L_ELBOW, R_ELBOW = 7, 8# L_WRIST, R_WRIST = 9, 10# L_HIP, R_HIP = 11, 12# L_ANKLE, R_ANKLE = 15, 16# # Define Keypoint Pairs for Drawing Lines (COCO Format)# skeleton = [(5, 6), (5, 11), (6, 12), (11, 12)]# # Draw Keypoints# for x, y in keypoints:# cv2.circle(image, (int(x), int(y)), 5, (0, 255, 0), -1)# # Draw Skeleton# 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)# # Function to calculate Euclidean distance# def 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 ankle# pixels_per_cm = estimated_full_pixel_height / user_height_cm# # Waist and shoulder measurements# shoulder_width_px = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER])# waist_width_px = get_distance(keypoints[L_HIP], keypoints[R_HIP])# # Convert to cm# shoulder_width_cm = shoulder_width_px / pixels_per_cm# waist_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) Γ 2# waist_circumference = np.pi * waist_width_cm# hip_circumference = waist_circumference / 0.75 # Assuming hip is slightly bigger than waist# # Improved body measurement calculation# def 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)# # Save to database function# def save_to_database(measurements, image, user_height_cm):# if measurements is None or image is None:# return "No data to save."# # Convert image to base64# buffered = BytesIO()# pil_image = Image.fromarray(image) # Convert to PIL Image from NumPy array# pil_image.save(buffered, format="JPEG")# img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")# # Send POST request to save measurements and image to the database# response = requests.post(# "https://9dbc-210-212-162-140.ngrok-free.app/upload", # Replace with your actual URL# json={# "imageBase64": img_str,# "heightCm": user_height_cm,# "waistCircumferenceCm": measurements["Waist Circumference (cm)"],# "shoulderwidth": measurements["Shoulder Width (cm)"],# "hipcircumference": measurements["Hip Circumference (cm)"],# "torsolength": measurements["Torso Length (Neck to Pelvis, cm)"],# "fullarmlength": measurements["Full Arm Length (Shoulder to Wrist, cm)"]# }# )# return f"Status: {response.status_code}, Message: {response.text}"# # Example usage (integrating with your Gradio app)# import gradio as gr# # Gradio interface setup# def process_and_save(image, user_height_cm):# measurements, processed_image = process_image(image, user_height_cm)# if measurements:# save_message = save_to_database(measurements, processed_image, user_height_cm)# return processed_image, measurements, save_message# else:# return None, None, "Error in processing image."# # Gradio interface setup# iface = gr.Interface(# fn=process_and_save,# inputs=[gr.Image(type="numpy"), gr.Number(label="User Height (cm)")],# outputs=[gr.Image(type="numpy"), gr.JSON(), gr.Textbox()],# live=True# )# iface.launch(share=True)# import gradio as gr# import json# import base64# import requests# from io import BytesIO# import torch# import numpy as np# import cv2# import os# from PIL import Image# from detectron2.engine import DefaultPredictor# from detectron2.config import get_cfg# from 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 None# if not keypoints:# return "No keypoints detected.", None, None# keypoints = np.array(keypoints[0])[:, :2]# # Draw keypoints and skeleton# skeleton = [(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# NOSE, L_SHOULDER, R_SHOULDER = 0, 5, 6# L_WRIST, L_HIP, R_HIP, L_ANKLE, R_ANKLE = 9, 11, 12, 15, 16# 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# pixels_per_cm = estimated_full_pixel_height / user_height_cm# shoulder_width_cm = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER]) / pixels_per_cm# waist_width_cm = get_distance(keypoints[L_HIP], keypoints[R_HIP]) / pixels_per_cm# pelvis = ((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_cm# arm_length_cm = get_distance(keypoints[L_SHOULDER], keypoints[L_WRIST]) / pixels_per_cm# waist_circumference = np.pi * waist_width_cm# hip_circumference = waist_circumference / 0.75# measurements = {# "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),# }# return measurements, cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB), keypoints.tolist()# # === Save to DB ===# # 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,# # "measurements": measurements# # }# # try:# # response = requests.post(# # f"https://9d68-210-212-162-140.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)}"# 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)"),# "hipcircumference": measurements.get("Hip Circumference (cm)"),# "shoulderwidth": measurements.get("Shoulder Width (cm)"),# "torsolength": measurements.get("Torso Length (Neck to Pelvis, cm)"),# "fullarmlength": measurements.get("Full Arm Length (Shoulder to Wrist, 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 save# processed_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()import torchimport numpy as npimport cv224 unmodified lineskeypoints = 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)7 unmodified linesL_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))26 unmodified lines"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])demo.launch()12324 unmodified lines282930313233347 unmodified lines42434445464726 unmodified lines7475767778798081828384858687888990919293949596979899import torchimport numpy as npimport cv224 unmodified lineskeypoints = instances.pred_keypoints.cpu().numpy().tolist() if instances.has("pred_keypoints") else Noneif not keypoints:return "No keypoints detected."with open(output_file, "w") as f:json.dump({"keypoints": keypoints}, f, indent=4)7 unmodified linesL_KNEE, R_KNEE = 13, 14L_ANKLE, R_ANKLE = 15, 16def get_distance(p1, p2):return np.linalg.norm(np.array(p1) - np.array(p2))26 unmodified lines"Neck to Knee Length (cm)": round(neck_to_knee_cm, 2)}return measurements# Gradio Interfacewith gr.Blocks() as demo:gr.Markdown("## π§ AI-Powered Body Measurement Estimator")gr.Markdown("Upload a clear **full-body image** and enter your **height (in cm)** to estimate key body dimensions using computer vision.")with gr.Row():with gr.Column():image_input = gr.Image(type="pil", label="πΈ Upload Image")height_input = gr.Number(label="π Your Height (cm)", value=170)submit_btn = gr.Button("π Estimate Measurements")with gr.Column():measurement_output = gr.JSON(label="π Estimated Measurements")submit_btn.click(fn=process_image,inputs=[image_input, height_input],outputs=[measurement_output])demo.launch()
app.py+12-527