Spaces:
Runtime error
Runtime error
datasciencesage
commited on
Commit
·
d907232
1
Parent(s):
9039581
Added all the files
Browse files- app.py +70 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
# Setting environment variables
|
3 |
+
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
4 |
+
os.environ["KERAS_BACKEND"] = "jax"
|
5 |
+
import streamlit as st
|
6 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image
|
9 |
+
import keras
|
10 |
+
import warnings
|
11 |
+
warnings.filterwarnings("ignore")
|
12 |
+
|
13 |
+
|
14 |
+
def resize_for_inference(input_image):
|
15 |
+
image = np.array(input_image)
|
16 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
17 |
+
mask = np.zeros(image.shape[:2], np.uint8)
|
18 |
+
|
19 |
+
height, width = image.shape[:2]
|
20 |
+
rect = (10, 10, width - 20, height - 20)
|
21 |
+
bgd_model = np.zeros((1, 65), np.float64)
|
22 |
+
fgd_model = np.zeros((1, 65), np.float64)
|
23 |
+
|
24 |
+
cv2.grabCut(image_rgb, mask, rect, bgd_model, fgd_model, 5, cv2.GC_INIT_WITH_RECT)
|
25 |
+
|
26 |
+
binary_mask = np.where((mask == 2) | (mask == 0), 0, 255).astype('uint8')
|
27 |
+
|
28 |
+
resized_mask = cv2.resize(binary_mask, (720, 960), interpolation=cv2.INTER_AREA)
|
29 |
+
|
30 |
+
target_size = (224, 224)
|
31 |
+
final_resized_mask = cv2.resize(resized_mask, target_size, interpolation=cv2.INTER_AREA)
|
32 |
+
|
33 |
+
final_resized_mask = np.expand_dims(final_resized_mask, axis=-1)
|
34 |
+
|
35 |
+
return final_resized_mask
|
36 |
+
|
37 |
+
st.title("Body Measurement Predictor")
|
38 |
+
st.write("Upload an image to predict body measurements.")
|
39 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
40 |
+
|
41 |
+
if 'loaded_model' not in st.session_state:
|
42 |
+
with st.spinner("Model is getting loaded. Please wait..."):
|
43 |
+
try:
|
44 |
+
st.session_state.loaded_model = keras.saving.load_model("hf://datasciencesage/bodym_measurement_model")
|
45 |
+
st.success("Model loaded successfully!")
|
46 |
+
except Exception as e:
|
47 |
+
st.error(f"Error loading model: {e}")
|
48 |
+
|
49 |
+
|
50 |
+
if uploaded_image is not None:
|
51 |
+
image = Image.open(uploaded_image)
|
52 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
53 |
+
|
54 |
+
with st.spinner("DOING IMAGE PREPROCESSING.....PLEASE WAIT..."):
|
55 |
+
resized_image = resize_for_inference(image)
|
56 |
+
single_image_expanded = np.expand_dims(resized_image, axis=0)
|
57 |
+
|
58 |
+
with st.spinner("INFERENCE IS BEING DONE.....PLEASE WAIT..."):
|
59 |
+
single_image_expanded = np.expand_dims(resized_image, axis=0)
|
60 |
+
|
61 |
+
predicted_values = st.session_state.loaded_model.predict(single_image_expanded)[0]
|
62 |
+
columns = ['ankle', 'arm-length', 'bicep', 'calf', 'chest',
|
63 |
+
'forearm', 'height', 'hip', 'leg-length', 'shoulder-breadth',
|
64 |
+
'shoulder-to-crotch', 'thigh', 'waist', 'wrist']
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
st.write("Predicted Body Measurements:")
|
69 |
+
for body_type, measurement in zip(columns, predicted_values):
|
70 |
+
st.write(f"{body_type}: {measurement:.2f} cm")
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
numpy
|
3 |
+
Pillow
|
4 |
+
matplotlib
|
5 |
+
keras==3.7.0
|
6 |
+
tensorflow==2.18.0
|
7 |
+
jax
|
8 |
+
huggingface_hub==0.20.3
|
9 |
+
streamlit
|