Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import basic libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import cv2
|
| 5 |
+
from deepface import DeepFace as dfc
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title='Face-detection-analysis', page_icon=None, layout='centered', initial_sidebar_state='auto')
|
| 10 |
+
|
| 11 |
+
# function to load image
|
| 12 |
+
try:
|
| 13 |
+
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
| 14 |
+
except Exception:
|
| 15 |
+
st.write("Error loading cascade classifiers")
|
| 16 |
+
|
| 17 |
+
@st.cache
|
| 18 |
+
def face_detect(img):
|
| 19 |
+
img = np.array(img.convert("RGB"))
|
| 20 |
+
face = face_cascade.detectMultiScale(image=img)
|
| 21 |
+
|
| 22 |
+
# draw rectangle around face
|
| 23 |
+
for (x, y, w, h) in face:
|
| 24 |
+
cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(255, 0, 0), thickness=2)
|
| 25 |
+
roi = img[y:y + h, x:x + w]
|
| 26 |
+
return img, face
|
| 27 |
+
|
| 28 |
+
# analyze image
|
| 29 |
+
def analyze_image(img):
|
| 30 |
+
prediction = dfc.analyze(img_path=img)
|
| 31 |
+
return prediction
|
| 32 |
+
|
| 33 |
+
#function for webcam
|
| 34 |
+
def detect_web(image):
|
| 35 |
+
|
| 36 |
+
faces = face_cascade.detectMultiScale(
|
| 37 |
+
image=image, scaleFactor=1.3, minNeighbors=5)
|
| 38 |
+
|
| 39 |
+
for (x, y, w, h) in faces:
|
| 40 |
+
cv2.rectangle(img=image, pt1=(x, y), pt2=(
|
| 41 |
+
x + w, y + h), color=(255, 0, 0), thickness=2)
|
| 42 |
+
return image, faces
|
| 43 |
+
|
| 44 |
+
def main():
|
| 45 |
+
# Face Analysis Application #
|
| 46 |
+
st.markdown("<h1 style='text-align: center;'>Face Detection and Analysis </h1>", unsafe_allow_html=True)
|
| 47 |
+
activiteis = ["Home", "Analyze Face", "About"]
|
| 48 |
+
choice = st.sidebar.selectbox("Select Activity", activiteis)
|
| 49 |
+
st.sidebar.markdown(
|
| 50 |
+
""" Developed by [Vivek] (https://github.com/7Vivek)""")
|
| 51 |
+
st.sidebar.markdown(
|
| 52 |
+
""" Checkout complete project [here] (https://github.com/7Vivek/Face-detection-analysis)""")
|
| 53 |
+
# C0C0C0
|
| 54 |
+
if choice == "Home":
|
| 55 |
+
html_temp_home1 = """<div style="background-color:#1E2839;padding:10px">
|
| 56 |
+
<h4 style="color:white;text-align:center;">
|
| 57 |
+
Face detection and Face feature analysis application using OpenCV, DeepFace and Streamlit.</h4>
|
| 58 |
+
</div>
|
| 59 |
+
</br>"""
|
| 60 |
+
st.image('https://cdn.dribbble.com/users/1373613/screenshots/5510801/media/b82469d51c432c2ff65c0158334cfabf.gif',use_column_width=True)
|
| 61 |
+
st.markdown(html_temp_home1, unsafe_allow_html=True)
|
| 62 |
+
st.write("""
|
| 63 |
+
Application Functionalities.
|
| 64 |
+
|
| 65 |
+
1. Face feature analysis such as emotion, gender and age.""")
|
| 66 |
+
elif choice == "Analyze Face":
|
| 67 |
+
st.subheader("Analyze facial features such as emotion, age and gender.")
|
| 68 |
+
image_file = st.file_uploader("Upload image you want to analyze", type=['jpg', 'png', 'jpeg'])
|
| 69 |
+
|
| 70 |
+
if image_file is not None:
|
| 71 |
+
#read image using PIL
|
| 72 |
+
image_loaded = Image.open(image_file)
|
| 73 |
+
#detect faces in image
|
| 74 |
+
result_img, result_face = face_detect(image_loaded)
|
| 75 |
+
st.image(result_img, use_column_width=True)
|
| 76 |
+
st.success("found {} face\n".format(len(result_face)))
|
| 77 |
+
|
| 78 |
+
if st.button("Analyze image"):
|
| 79 |
+
# convert image to array
|
| 80 |
+
new_image = np.array(image_loaded.convert('RGB'))
|
| 81 |
+
img = cv2.cvtColor(new_image, 1)
|
| 82 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 83 |
+
#analyze features of face
|
| 84 |
+
result = analyze_image(img)
|
| 85 |
+
# st.write(result)
|
| 86 |
+
st.write("Analysis summary")
|
| 87 |
+
st.write("Face emotion is ", result["dominant_emotion"], "in image.")
|
| 88 |
+
st.write("Gender recognized as", result["gender"], "in image.")
|
| 89 |
+
st.write("Age is", result["age"], "years.")
|
| 90 |
+
else:
|
| 91 |
+
pass
|
| 92 |
+
#st.write("Click on Analyze image ")
|
| 93 |
+
|
| 94 |
+
elif choice == "About":
|
| 95 |
+
st.subheader("About this app")
|
| 96 |
+
html_temp_about1= """<div style="background-color:#283347;padding:10px">
|
| 97 |
+
<h4 style="color:white;text-align:center;">
|
| 98 |
+
Face detection and Face feature analysis application using OpenCV, DeepFace and Streamlit.</h4>
|
| 99 |
+
</div>
|
| 100 |
+
</br>"""
|
| 101 |
+
st.markdown(html_temp_about1, unsafe_allow_html=True)
|
| 102 |
+
|
| 103 |
+
html_temp4 = """
|
| 104 |
+
<div style="background-color:#434E61;padding:10px">
|
| 105 |
+
<h4 style="color:white;text-align:center;">This Application is developed by Vivek Limbad using Streamlit Framework, Opencv and DeepFace library for demonstration purpose. If you have any suggestion or want to comment just write a mail at viveklimbad1010@gmail.com. </h4>
|
| 106 |
+
<h4 style="color:white;text-align:center;">Thanks for Visiting </h4>
|
| 107 |
+
</div>
|
| 108 |
+
<br></br>
|
| 109 |
+
<br></br>"""
|
| 110 |
+
|
| 111 |
+
st.markdown(html_temp4, unsafe_allow_html=True)
|
| 112 |
+
|
| 113 |
+
else:
|
| 114 |
+
pass
|
| 115 |
+
|
| 116 |
+
if __name__ == '__main__':
|
| 117 |
+
main()
|