Spaces:
Sleeping
Sleeping
File size: 2,123 Bytes
ab8530c 388cf06 ab8530c 388cf06 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import streamlit as st
import os
import numpy as np
import pandas as pd
from glob import glob
import pickle
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
import seaborn as sns
import torch
import torchvision.transforms as transforms
from PIL import Image
from facenet_pytorch import MTCNN, InceptionResnetV1
import warnings
warnings.filterwarnings("ignore")
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# If required, create a face detection pipeline using MTCNN:
mtcnn = MTCNN(
image_size=160, margin=40, min_face_size=20,
thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True,
device=device
)
mtcnn2 = MTCNN(
image_size=160, margin=40, min_face_size=20,
thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=False,
device=device
)
# Create an inception resnet (in eval mode):
resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)
# Define the transformation to preprocess the images
preprocess = transforms.Compose([
transforms.Resize((160, 160)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
def extract_features(img):
img = img.convert('RGB')
face = mtcnn(img)
if face is None:
face = preprocess(img)
img = torch.stack([face]).to(device)
with torch.no_grad():
features = resnet(img)
return features[0].cpu().numpy()
with open("models/lr.p", "rb") as f:
lr = pickle.load(f)
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as a PIL Image:
img = Image.open(img_file_buffer)
detected_image = Image.fromarray(mtcnn2(img).numpy().transpose(1, 2, 0).astype(np.uint8))
st.image(detected_image, caption="Detected Face")
embeddings = extract_features(img)
bmi = round(lr.predict([embeddings])[0], 2)
st.write(f"Your BMI is {bmi}")
|