Spaces:
Running
Running
SalehAhmad
commited on
Commit
•
e469eba
1
Parent(s):
47b93ca
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
5 |
+
|
6 |
+
import tensorflow as tf
|
7 |
+
from tf_bodypix.api import download_model, load_model, BodyPixModelPaths
|
8 |
+
from tf_bodypix.draw import draw_poses # utility function using OpenCV
|
9 |
+
from tensorflow.keras import preprocessing
|
10 |
+
import cv2
|
11 |
+
import json
|
12 |
+
import numpy as np
|
13 |
+
from calculations import measure_body_sizes
|
14 |
+
|
15 |
+
# Load BodyPix model
|
16 |
+
bodypix_model = load_model(download_model(BodyPixModelPaths.MOBILENET_FLOAT_50_STRIDE_16))
|
17 |
+
|
18 |
+
def process_images(front_img, side_img, height):
|
19 |
+
# Convert images to image arrays
|
20 |
+
front_image_array = preprocessing.image.img_to_array(front_img)
|
21 |
+
side_image_array = preprocessing.image.img_to_array(side_img)
|
22 |
+
|
23 |
+
# BodyPix prediction
|
24 |
+
result = bodypix_model.predict_single(front_image_array)
|
25 |
+
mask = result.get_mask(threshold=0.75)
|
26 |
+
# colored_mask = result.get_colored_part_mask(mask)
|
27 |
+
|
28 |
+
poses = result.get_poses()
|
29 |
+
print(f'shape of poses: {np.shape(poses)}')
|
30 |
+
print(poses)
|
31 |
+
# image_with_poses = draw_poses(
|
32 |
+
# front_image_array.copy(), # create a copy to ensure we are not modifying the source image
|
33 |
+
# poses,
|
34 |
+
# keypoints_color=(255, 100, 100),
|
35 |
+
# skeleton_color=(100, 100, 255)
|
36 |
+
# )
|
37 |
+
|
38 |
+
# Measure body sizes using poses and real height
|
39 |
+
body_sizes = measure_body_sizes(poses, height)
|
40 |
+
print(f'Body sizes: {body_sizes}')
|
41 |
+
|
42 |
+
# Prepare the output images
|
43 |
+
# front_image_with_poses = preprocessing.image.array_to_img(image_with_poses)
|
44 |
+
|
45 |
+
# Convert measurements to DataFrame for display
|
46 |
+
measurements_df = pd.DataFrame(body_sizes)
|
47 |
+
|
48 |
+
return measurements_df
|
49 |
+
|
50 |
+
# Create the Gradio interface
|
51 |
+
interface = gr.Interface(
|
52 |
+
fn=process_images,
|
53 |
+
inputs=[
|
54 |
+
gr.Image(label="Upload Front Pose"),
|
55 |
+
gr.Image(label="Upload Side Pose"),
|
56 |
+
gr.Number(label="Enter Height (cm)")
|
57 |
+
],
|
58 |
+
outputs=[
|
59 |
+
gr.DataFrame(label="Body Measurements")
|
60 |
+
],
|
61 |
+
title="Body Sizing System Demo",
|
62 |
+
description="Upload two images: Front View and Side View, and input the height in cm."
|
63 |
+
)
|
64 |
+
|
65 |
+
# Launch the app
|
66 |
+
interface.launch(share=False)
|