-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterFaceApp.py
More file actions
134 lines (95 loc) · 5.56 KB
/
FilterFaceApp.py
File metadata and controls
134 lines (95 loc) · 5.56 KB
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
import cv2
import streamlit as st
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
import numpy as np
import av
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyes_detector = cv2.CascadeClassifier('haarcascade_eye.xml')
class VideoDeteccionCaraOjo:
def __init__(self):
self.threshold1 = 100
def recv(self, frame):
img = frame.to_ndarray(format="bgr24")
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6 , minSize=(self.threshold1,self.threshold1), maxSize=(self.threshold1+100,self.threshold1+100))
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
gray_cara = cv2.cvtColor(img[y:y+h,x:x+w], cv2.COLOR_BGR2GRAY)
max_face = int(max(w, h)/4) # Para la deteccion de los ojos se mantiene la proporcion entre la cara y los ojos aprox el 25%
eyes = eyes_detector.detectMultiScale(gray_cara, scaleFactor=1.1, minNeighbors=6 , minSize=(max_face,max_face), maxSize=(max_face+10,max_face+10))
if ( len(eyes) == 2 ):# Solo cuando se detecte dos ojos se dibujara.
for (x_e, y_e, w_e, h_e) in eyes:
cv2.rectangle(img[y:y+h,x:x+w], (x_e,y_e), (x_e+w_e,y_e+h_e), (0,255,0), 2)
return av.VideoFrame.from_ndarray(img, format="bgr24")
class VideoBlurring:
def __init__(self):
self.threshold1 = 100
self.kernel = 5
def recv(self, frame):
img = frame.to_ndarray(format="bgr24")
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6 , minSize=(self.threshold1,self.threshold1), maxSize=(self.threshold1+100,self.threshold1+100))
for (x, y, w, h) in faces:
kernel = np.ones((self.kernel,self.kernel),np.float32)/(self.kernel*self.kernel)
img[y:y+h,x:x+w] = cv2.filter2D(img[y:y+h,x:x+w],-1,kernel)
return av.VideoFrame.from_ndarray(img, format="bgr24")
class VideoModificacionFace:
def __init__(self):
self.imagen = None
self.threshold1 = 100
def recv(self, frame):
img = frame.to_ndarray(format="bgr24")
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6 , minSize=(self.threshold1,self.threshold1), maxSize=(self.threshold1+100,self.threshold1+100))
for (x, y, w, h) in faces:
b = cv2.resize( self.imagen , dsize=(h,w), interpolation=cv2.INTER_CUBIC)
img[y:y+h,x:x+w] = b
return av.VideoFrame.from_ndarray(img, format="bgr24")
def main():
with open('style.css') as f:
st.sidebar.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
st.title('Crea tu propio filtro personalizado !!!')
st.write('FilterFaceApp es un WebApp con el objetivo de practicar tus conocimientos de procesamiento de imagenes creando tus propios filtros sobre **rostros** e incorporarlo a la WebApp.')
st.sidebar.header('Filtros personalizados')
option = st.sidebar.selectbox('Elige una opcion ...',('Deteccion de caras y ojos', 'Blurring', 'Reemplazar cara por imagen'))
if ( option == 'Deteccion de caras y ojos'):
ctx = webrtc_streamer(
key="example",
video_processor_factory=VideoDeteccionCaraOjo,
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
}
)
if ctx.video_processor:
ctx.video_processor.threshold1 = st.slider("Minimo tamaño de la cara", 100, 200, 150)
st.write('*Minimo tamaño de la cara* : Indica la longitud en pixels minima de la cara detectada , a menor valor se podra detectar rostros mas lejos de la camara')
if ( option == 'Blurring'):
ctx = webrtc_streamer(
key="example",
video_processor_factory=VideoBlurring,
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
}
)
if ctx.video_processor:
ctx.video_processor.threshold1 = st.slider("Minimo tamaño de la cara", 100, 200, 150)
ctx.video_processor.kernel = st.slider("Grado de Blurring", 5, 50, 20)
st.write('*Minimo tamaño de la cara* : Indica la longitud en pixels minima de la cara detectada , a menor valor se podra detectar rostros mas lejos de la camara')
if ( option == 'Reemplazar cara por imagen'):
file_uploader = st.sidebar.file_uploader("Suba una imagen en formato jpg ...",type="jpg")
if file_uploader is not None:
file_bytes = np.asarray(bytearray(file_uploader.read()), dtype=np.uint8)
opencv_image = cv2.imdecode(file_bytes, 1)
ctx = webrtc_streamer(
key="example",
video_processor_factory=VideoModificacionFace,
rtc_configuration={ # Add this line
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
}
)
if ctx.video_processor:
ctx.video_processor.threshold1 = st.slider("Minimo tamaño de la cara", 100, 200, 150)
ctx.video_processor.imagen = opencv_image
st.write('*Minimo tamaño de la cara* : Indica la longitud en pixels minima de la cara detectada , a menor valor se podra detectar rostros mas lejos de la camara')
if __name__ == '__main__':
main()