codevlogger2003's picture
Updated app.py
509e709
import numpy as np
import pandas as pd
from deepface import DeepFace
import streamlit as st
import cv2
import base64
import time
st.set_page_config(layout="wide")
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
def upload():
image=None
initial_image = st.camera_input('Take a picture')
original_image = initial_image
temp_path = None
if initial_image is not None:
bytes_data = initial_image.getvalue()
image = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
return image, original_image
def main(options):
if st.checkbox('Take a picture for prediction'):
image, original_image= upload()
if original_image is not None and original_image is not None and st.button('Prediction'): # Check if original_image is not None
st.warning('Wait for few seconds!!')
progress_bar = st.progress(0.0)
status_text = st.empty()
result = DeepFace.analyze(image,detector_backend=options,actions=['age','gender','emotion','race'])
for i in range(100):
progress_bar.progress((i + 1) / 100)
status_text.text(f"Processing {i+1}%")
time.sleep(0.01)
progress_bar.empty()
gray_frame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(gray_frame, 1.1, 3)
faces = sorted(faces, key=lambda f: -f[2] * f[3])
if len(faces) > 0:
x,y,w,h=faces[0]
cv2.rectangle(image, (x, y), (x+w, y+h), (4, 29, 255), 2, cv2.LINE_4)
user_selected_items = list(result[0].keys())
if 'age' in user_selected_items:
age_label='Age: '+str(result[0]['age'])
cv2.putText(image, age_label, (x ,y+h+30), cv2.FONT_ITALIC,1 ,(255,255,0), 2)
if 'dominant_gender' in user_selected_items:
gender_label='Gender: '+str(result[0]['dominant_gender'])
cv2.putText(image, gender_label, (x, y+h+70), cv2.FONT_ITALIC,1, (0,255,255), 2)
if 'dominant_emotion' in user_selected_items:
emotion_label='Emotion: '+str(result[0]['dominant_emotion']).title()
cv2.putText(image, emotion_label, (x, y+h+110), cv2.FONT_ITALIC,1 ,(255,0,255), 2)
if 'dominant_race' in user_selected_items:
emotion_label='Race: '+str(result[0]['dominant_race']).title()
cv2.putText(image, emotion_label, (x, y+h+150), cv2.FONT_ITALIC,1 ,(255,255,255), 2)
st.image(image, channels='BGR')
st.balloons()
if __name__ == '__main__':
def get_options():
actions = ['opencv','mtcnn','retinaface']
option2 = st.selectbox('Choose the following backend:', actions)
return option2
main(get_options())