kmnis commited on
Commit
dd7ee52
·
1 Parent(s): fcbc69f

Updated app

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -26,28 +26,29 @@ warnings.filterwarnings("ignore")
26
 
27
  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
28
 
29
- # If required, create a face detection pipeline using MTCNN:
30
- mtcnn = MTCNN(
31
- image_size=160, margin=40, min_face_size=20,
32
- thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
33
- device=device
34
- )
35
-
36
- mtcnn2 = MTCNN(
37
- image_size=160, margin=40, min_face_size=20,
38
- thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False,
39
- device=device
40
- )
41
-
42
- # Create an inception resnet (in eval mode):
43
- resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
44
-
45
- # Define the transformation to preprocess the images
46
- preprocess = transforms.Compose([
47
- transforms.Resize((160, 160)),
48
- transforms.ToTensor(),
49
- transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
50
- ])
 
51
 
52
  def extract_features(img):
53
  img = img.convert('RGB')
@@ -66,7 +67,10 @@ def extract_features(img):
66
  with open("models/svr.p", "rb") as f:
67
  lr = pickle.load(f)
68
 
69
- img_file_buffer = st.camera_input("Take a picture")
 
 
 
70
 
71
  if img_file_buffer is not None:
72
  # To read image file buffer as a PIL Image:
@@ -76,5 +80,5 @@ if img_file_buffer is not None:
76
  st.image(detected_image, caption="Detected Face")
77
 
78
  embeddings = extract_features(img)
79
- bmi = round(lr.predict([embeddings])[0], 2) - 4 # Because the model is consistently over predicting the BMI values
80
  st.write(f"Your BMI is {bmi}")
 
26
 
27
  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
28
 
29
+ with st.spinner('Loading the models...'):
30
+ # If required, create a face detection pipeline using MTCNN:
31
+ mtcnn = MTCNN(
32
+ image_size=160, margin=40, min_face_size=20,
33
+ thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
34
+ device=device
35
+ )
36
+
37
+ mtcnn2 = MTCNN(
38
+ image_size=160, margin=40, min_face_size=20,
39
+ thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False,
40
+ device=device
41
+ )
42
+
43
+ # Create an inception resnet (in eval mode):
44
+ resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
45
+
46
+ # Define the transformation to preprocess the images
47
+ preprocess = transforms.Compose([
48
+ transforms.Resize((160, 160)),
49
+ transforms.ToTensor(),
50
+ transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
51
+ ])
52
 
53
  def extract_features(img):
54
  img = img.convert('RGB')
 
67
  with open("models/svr.p", "rb") as f:
68
  lr = pickle.load(f)
69
 
70
+ st.markdown("<center><h1>Know Your BMI</h1></center>", unsafe_allow_html=True)
71
+ st.caption("<center>Click a photo and the underlying Machine Learning model will predict your BMI</center>", unsafe_allow_html=True)
72
+
73
+ img_file_buffer = st.camera_input("Click a photo and the underlying Machine Learning model will predict your BMI", label_visibility="hidden")
74
 
75
  if img_file_buffer is not None:
76
  # To read image file buffer as a PIL Image:
 
80
  st.image(detected_image, caption="Detected Face")
81
 
82
  embeddings = extract_features(img)
83
+ bmi = round(lr.predict([embeddings])[0], 2) - 4
84
  st.write(f"Your BMI is {bmi}")