-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace_Detection.py
159 lines (128 loc) · 5.27 KB
/
Face_Detection.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import streamlit as st
import cv2
from cv2 import *
from PIL import Image, ImageEnhance
import numpy as np
import os
@st.cache
def load_img(img):
im = Image.open(img)
return im
face_cascade = cv2.CascadeClassifier(
'haarcascades/haarcascade_frontalface_alt.xml')
eyes_cascade = cv2.CascadeClassifier(
'haarcascades/haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier(
'haarcascades/haarcascade_smile.xml')
def detect_faces(our_image):
new_img = np.array(our_image.convert('RGB'))
img = cv2.cvtColor(new_img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect Face
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw Rectangle
for(x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
return img, faces
def detect_eyes(our_image):
new_img = np.array(our_image.convert('RGB'))
img = cv2.cvtColor(new_img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect Eyes
eyes = eyes_cascade.detectMultiScale(gray, 1.3, 5)
# Draw Rectangle
for(ex, ey, ew, eh) in eyes:
cv2.rectangle(img, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
return img
def detect_smiles(our_image):
new_img = np.array(our_image.convert('RGB'))
img = cv2.cvtColor(new_img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# r_gray = gray[y:y+h, x:x+w]
# Detect Smile
smiles = smile_cascade.detectMultiScale(gray, 1.1, 4)
# Draw Rectangle
for(x, y, w, h) in smiles:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 5)
return img
def cartonize_image(our_image):
new_img = np.array(our_image.convert('RGB'))
img = cv2.cvtColor(new_img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Edges
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 2)
# Color
color = cv2.bilateralFilter(img, 9, 300, 300)
# Cartoon
cartoon = cv2.bitwise_and(color, color, mask=edges)
return cartoon
def cannize_image(our_image):
new_img = np.array(our_image.convert('RGB'))
img = cv2.cvtColor(new_img, 1)
img = cv2.GaussianBlur(img, (11, 11), 0)
canny = cv2.Canny(img, 100, 150)
return canny
def main():
st.title("Face Detection App")
st.text("Built with Streamlit and Open CV")
activities = ["Detection", "About"]
choice = st.sidebar.selectbox("Select Activity", activities)
if choice == 'Detection':
st.subheader("Face Detection")
image_file = st.file_uploader(
"Upload Image", type=['jpg', 'png', 'jpeg'])
if image_file is not None:
our_image = Image.open(image_file)
st.text("Original Image")
st.image(our_image, width=500)
enhance_type = st.sidebar.radio(
"Enhance Type", ["Original", "Gray-Scale", "Contrast", "Brightness", "Blurring"])
if enhance_type == "Gray-Scale":
new_img = np.array(our_image.convert('RGB'))
img = cv2.cvtColor(new_img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
st.image(gray, width=500)
if enhance_type == "Contrast":
c_rate = st.sidebar.slider("Contrast", 0.5, 3.5)
enhancer = ImageEnhance.Contrast(our_image)
img_output = enhancer.enhance(c_rate)
st.image(img_output, width=500)
if enhance_type == "Brightness":
c_rate = st.sidebar.slider("Brightness", 0.5, 3.5)
enhancer = ImageEnhance.Brightness(our_image)
img_output = enhancer.enhance(c_rate)
st.image(img_output, width=500)
if enhance_type == "Blurring":
new_img = np.array(our_image.convert('RGB'))
blur_rate = st.sidebar.slider("Blur", 0.5, 3.5)
img = cv2.cvtColor(new_img, 1)
blur_img = cv2.GaussianBlur(img, (11, 11), blur_rate)
st.image(blur_img, width=500)
# Face Detection
task = ["Faces", "Smiles", "Eyes", "Cannize", "Cartonize"]
feature_choice = st.sidebar.selectbox("Find Features", task)
if st.button("Process"):
if feature_choice == 'Faces':
result_img, result_faces = detect_faces(our_image)
st.image(result_img, width=500)
st.success("Found {} faces".format(len(result_faces)))
elif feature_choice == 'Eyes':
result_img = detect_eyes(our_image)
st.image(result_img, width=500)
elif feature_choice == 'Smiles':
result_img = detect_smiles(our_image)
st.image(result_img, width=500)
elif feature_choice == 'Cannize':
result_img = cannize_image(our_image)
st.image(result_img, width=500)
elif feature_choice == 'Cartonize':
result_img = cartonize_image(our_image)
st.image(result_img, width=500)
elif choice == 'About':
st.subheader("About")
st.text("A Face Detection Application built with Streamlit and Open CV in Python. \nIt provides features of \n- Editing Your Image\n- Face Detection\n- Smile Detection\n- Eyes Detection\n- Cartoonizing your image")
st.subheader("\n\n\nBuilt by Mihir Pesswani")
if __name__ == "__main__":
main()