-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
99 lines (70 loc) · 3.25 KB
/
app.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Importing required libraries
import streamlit as st
import cv2
from PIL import Image
import numpy as np
import os
# Loading pre-trained parameters for the cascade classifier
try:
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_smile.xml')
except Exception:
st.write("Error loading cascade classifiers")
def detect(image):
'''
Function to detect faces/eyes and smiles in the image passed to this function
'''
image = np.array(image.convert('RGB'))
faces = face_cascade.detectMultiScale(image=image, scaleFactor=1.3, minNeighbors=5)
# The face_cascade classifier returns coordinates of the area in which the face might be located in the image
# We will be looking for eyes and smile within this area instead of looking for them in the entire image
# Draw rectangle around faces
for (x, y, w, h) in faces:
cv2.rectangle(img=image, pt1=(x, y), pt2=(x + w, y + h), color=(255, 0, 0), thickness=2)
roi = image[y:y+h, x:x+w]
# Detecting eyes in the face(s) detected
eyes = eye_cascade.detectMultiScale(roi)
# Detecting smiles in the face(s) detected
smile = smile_cascade.detectMultiScale(roi, minNeighbors = 25)
# Drawing rectangle around eyes
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi, (ex, ey), (ex+ew, ey+eh), (0,255,0), 2)
# Drawing rectangle around smile
for (sx,sy,sw,sh) in smile:
cv2.rectangle(roi, (sx, sy), (sx+sw, sy+sh), (0,0,255), 2)
# Returning the image with bounding boxes drawn on it (in case of detected objects), and faces array
return image, faces
def about():
st.write(
'''
**Haar Cascade** is an object detection algorithm.
It can be used to detect objects in images or videos.
The algorithm has four stages:
1. Haar Feature Selection
2. Creating Integral Images
3. Adaboost Training
4. Cascading Classifiers
Read more :point_right: https://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html
https://sites.google.com/site/5kk73gpu2012/assignment/viola-jones-face-detection#TOC-Image-Pyramid
''')
def main():
st.title("Face Detection App :sunglasses: ")
st.write("**Using the Haar cascade Classifiers**")
activities = ["Home", "About"]
choice = st.sidebar.selectbox("Select Pages", activities)
if choice == "Home":
st.write("Go to the About section from the sidebar to learn more about it.")
# Upload file to the app
image_file = st.file_uploader("Upload image", type=['jpeg', 'png', 'jpg', 'webp'])
if image_file is not None:
image = Image.open(image_file)
if st.button("Process"):
# result_img is the image with rectangle drawn on it (in case there are faces detected)
result_img, result_faces = detect(image=image)
st.image(result_img, use_column_width = True)
st.success("Found {} faces\n".format(len(result_faces)))
elif choice == "About":
about()
if __name__ == "__main__":
main()