alaahilal commited on
Commit
1547048
·
verified ·
1 Parent(s): 3c55b81

updated the file

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Convert uploaded image to NumPy array
16
+ image = np.array(input_image)
17
+
18
+ # Convert the image to RGB format (for compatibility with GrabCut)
19
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
20
+
21
+ # Initialize the mask for GrabCut
22
+ mask = np.zeros(image.shape[:2], np.uint8)
23
+
24
+ # Define the rectangle for the GrabCut algorithm
25
+ height, width = image.shape[:2]
26
+ rect = (10, 10, width - 20, height - 20) # Slightly smaller than the full image
27
+
28
+ # Allocate memory for the GrabCut algorithm
29
+ bgd_model = np.zeros((1, 65), np.float64)
30
+ fgd_model = np.zeros((1, 65), np.float64)
31
+
32
+ # Apply the GrabCut algorithm
33
+ cv2.grabCut(image_rgb, mask, rect, bgd_model, fgd_model, 5, cv2.GC_INIT_WITH_RECT)
34
+
35
+ # Convert the mask to binary (foreground is white, background is black)
36
+ binary_mask = np.where((mask == 2) | (mask == 0), 0, 255).astype('uint8')
37
+
38
+ # Resize the binary mask to the desired shape (960x720)
39
+ resized_mask = cv2.resize(binary_mask, (720, 960), interpolation=cv2.INTER_AREA)
40
+
41
+ # Further resize the mask to target size (224x224)
42
+ target_size = (224, 224)
43
+ final_resized_mask = cv2.resize(resized_mask, target_size, interpolation=cv2.INTER_AREA)
44
+
45
+ final_resized_mask = np.expand_dims(final_resized_mask, axis=-1)
46
+
47
+ return final_resized_mask
48
+
49
+ # Streamlit UI
50
+ st.title("Body Measurement Predictor")
51
+ st.write("Upload an image to predict body measurements.")
52
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
53
+
54
+ # Check if the model is already loaded in the session state
55
+ if 'loaded_model' not in st.session_state:
56
+ with st.spinner("Model is getting loaded. Please wait..."):
57
+ try:
58
+ # Load the pre-trained model only once
59
+ st.session_state.loaded_model = keras.saving.load_model("hf://datasciencesage/bodym_measurement_model")
60
+ st.success("Model loaded successfully!")
61
+ except Exception as e:
62
+ st.error(f"Error loading model: {e}")
63
+
64
+ # Show the image upload box immediately
65
+
66
+ if uploaded_image is not None:
67
+ # Display the uploaded image
68
+ image = Image.open(uploaded_image)
69
+ st.image(image, caption="Uploaded Image", use_column_width=True)
70
+
71
+ # Preprocess the image and make predictions using the pre-loaded model
72
+ with st.spinner("DOING IMAGE PREPROCESSING.....PLEASE WAIT..."):
73
+ resized_image = resize_for_inference(image)
74
+ single_image_expanded = np.expand_dims(resized_image, axis=0)
75
+
76
+ # Make predictions using the loaded model
77
+ with st.spinner("INFERENCE IS BEING DONE.....PLEASE WAIT..."):
78
+ single_image_expanded = np.expand_dims(resized_image, axis=0)
79
+
80
+ # Make predictions using the loaded model
81
+ predicted_values = st.session_state.loaded_model.predict(single_image_expanded)[0]
82
+ columns = ['ankle', 'arm-length', 'bicep', 'calf', 'chest',
83
+ 'forearm', 'height', 'hip', 'leg-length', 'shoulder-breadth',
84
+ 'shoulder-to-crotch', 'thigh', 'waist', 'wrist']
85
+
86
+ # Display the results
87
+
88
+
89
+ st.write("Predicted Body Measurements:")
90
+ for body_type, measurement in zip(columns, predicted_values):
91
+ st.write(f"{body_type}: {measurement:.2f} cm")