naman1011 commited on
Commit
33811e0
·
1 Parent(s): f823189

Update model and draw boundary

Browse files
Files changed (1) hide show
  1. app.py +16 -101
app.py CHANGED
@@ -10,110 +10,25 @@ import PIL
10
  import io
11
  import html
12
  import time
13
-
14
 
15
  model_file_path = 'models/bmi.h5'
16
  model = tf.keras.models.load_model(model_file_path)
17
 
 
 
 
 
 
18
  # Preprocess the images for VGG16
19
  def preprocess_image(img_path):
20
  img = image.load_img(img_path, target_size = (224, 224))
21
  img = image.img_to_array(img)
22
  img = np.expand_dims(img, axis = 0)
23
- img = preprocess_input(img)
24
  return img
25
 
26
 
27
- # function to convert OpenCV Rectangle bounding box image into base64 byte string to be overlayed on video stream
28
- def bbox_to_bytes(bbox_array):
29
- """
30
- Params:
31
- bbox_array: Numpy array (pixels) containing rectangle to overlay on video stream.
32
- Returns:
33
- bytes: Base64 image byte string
34
- """
35
- # convert array into PIL image
36
- bbox_PIL = PIL.Image.fromarray(bbox_array, 'RGBA')
37
- iobuf = io.BytesIO()
38
- # format bbox into png for return
39
- bbox_PIL.save(iobuf, format='png')
40
- # format return string
41
- bbox_bytes = 'data:image/png;base64,{}'.format((str(b64encode(iobuf.getvalue()), 'utf-8')))
42
-
43
- return bbox_bytes
44
-
45
- # base_model = VGGFace(model='vgg16', include_top=False, input_shape=(224, 224, 3))
46
- # x = base_model.output
47
- # x = GlobalAveragePooling2D()(x)
48
- # model = Model(inputs=base_model.input, outputs=x)
49
-
50
- # # Function to preprocess the image
51
- # def preprocess_image(img):
52
- # img = cv2.resize(img, (224, 224))
53
- # img = image.img_to_array(img)
54
- # img = np.expand_dims(img, axis=0)
55
- # img = img[0] # Remove the extra dimension
56
- # return img
57
-
58
- # def extract_features(image_array):
59
- # # img = np.squeeze(image_array, axis=0)
60
- # img = np.expand_dims(image_array, axis=0)
61
- # img = tf.keras.applications.resnet50.preprocess_input(img)
62
- # features = model.predict(img,verbose=0)
63
- # return features.flatten()
64
-
65
- # Function to predict BMI
66
-
67
- def draw_boundary(img):
68
- # initialize the Haar Cascade face detection model
69
- face_cascade = cv2.CascadeClassifier(cv2.samples.findFile(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'))
70
-
71
- # initialze bounding box to empty
72
- bbox = ''
73
- count = 0
74
- while True:
75
-
76
- # create transparent overlay for bounding box
77
- bbox_array = np.zeros([480,640,4], dtype=np.uint8)
78
-
79
- # grayscale image for face detection
80
- gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
81
-
82
- # get face region coordinates
83
- faces = face_cascade.detectMultiScale(gray)
84
- # get face bounding box for overlay
85
- for (x, y, w, h) in faces:
86
- # Extract the face region from the frame
87
- face = img[y:y+h, x:x+w]
88
-
89
- # Preprocess the face image
90
- face = cv2.resize(face, (224, 224))
91
- face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
92
- face = preprocess_input(face)/255.
93
- face = np.expand_dims(face, axis=0)
94
-
95
- # Predict BMI using the pre-trained model
96
- bmi = model.predict(face)[0][0]
97
-
98
- # Draw the predicted BMI on the frame
99
- bbox_array = cv2.putText(bbox_array, f'BMI: {bmi:.2f}', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
100
-
101
- # Draw a rectangle around the face
102
- bbox_array = cv2.rectangle(bbox_array, (x, y), (x+w, y+h), (255, 0, 0), 2)
103
-
104
- bbox_array[:,:,3] = (bbox_array.max(axis = 2) > 0 ).astype(int) * 255
105
- # convert overlay of bbox into bytes
106
- bbox_bytes = bbox_to_bytes(bbox_array)
107
- # update bbox so next frame gets new overlay
108
- bbox = bbox_bytes
109
-
110
- return img
111
-
112
- def predict_bmi(img):
113
- pre_img = preprocess_image(img)
114
- pred = draw_boundary(pre_img)
115
- return pred
116
-
117
  def main():
118
  st.title("BMI Prediction from Camera Image")
119
  st.write("This app predicts the BMI of a person from an image captured using the camera.")
@@ -122,15 +37,15 @@ def main():
122
  img_file_buffer = st.camera_input("Take a picture")
123
 
124
  if img_file_buffer is not None:
125
- # Load the image data from the file buffer
126
- file_bytes = np.asarray(bytearray(img_file_buffer.getvalue()), dtype=np.uint8)
127
- img = cv2.imdecode(file_bytes, 1)
128
-
129
- # Preprocess the image and predict BMI
130
- bmi_label = predict_bmi(img)
131
-
132
- # Display the predicted BMI
133
- st.write("Predicted BMI:", str(bmi_label[0] - 5))
134
 
135
  if __name__ == '__main__':
136
  main()
 
10
  import io
11
  import html
12
  import time
13
+ from facenet_pytorch import MTCNN
14
 
15
  model_file_path = 'models/bmi.h5'
16
  model = tf.keras.models.load_model(model_file_path)
17
 
18
+ mtcnn2 = MTCNN(
19
+ image_size=160, margin=40, min_face_size=20,
20
+ thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False
21
+ )
22
+
23
  # Preprocess the images for VGG16
24
  def preprocess_image(img_path):
25
  img = image.load_img(img_path, target_size = (224, 224))
26
  img = image.img_to_array(img)
27
  img = np.expand_dims(img, axis = 0)
28
+ img = preprocess_input(img)/255.
29
  return img
30
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def main():
33
  st.title("BMI Prediction from Camera Image")
34
  st.write("This app predicts the BMI of a person from an image captured using the camera.")
 
37
  img_file_buffer = st.camera_input("Take a picture")
38
 
39
  if img_file_buffer is not None:
40
+ # To read image file buffer as a PIL Image:
41
+ img = Image.open(img_file_buffer)
42
+
43
+ detected_image = Image.fromarray(mtcnn2(img).numpy().transpose(1, 2, 0).astype(np.uint8))
44
+ st.image(detected_image, caption="Detected Face")
45
+
46
+ embeddings = preprocess_image(img_file_buffer)
47
+ bmi = round(model.predict(embeddings), 2) - 4
48
+ st.write(f"Your BMI is {bmi}")
49
 
50
  if __name__ == '__main__':
51
  main()