├── BoundingBox.py ├── README.md ├── __pycache__ ├── BoundingBox.cpython-37.pyc ├── common.cpython-37.pyc ├── config.cpython-37.pyc ├── f_detector.cpython-37.pyc ├── f_utils.cpython-37.pyc ├── tst_scene_render.cpython-37.pyc └── video.cpython-37.pyc ├── config.py ├── data_test ├── friends.jpg ├── friends10.jpg ├── friends2.jpg ├── friends3.jpg ├── friends4.jpg ├── friends5.jpg ├── friends6.jpg ├── friends7.jpg ├── friends8.jpg ├── friends9.jpg ├── juan.jpg ├── profile_juan.jpg ├── test1.jpg ├── test2.jpg ├── test3.jpg ├── test4.jpg ├── test5.jpg └── test6.jpg ├── f_detector.py ├── haarcascades ├── haarcascade_frontalface_alt.xml └── haarcascade_profileface.xml ├── perfil_detecttion.py ├── results ├── perfil_detection.gif └── perfil_detection.png └── test.py /BoundingBox.py: -------------------------------------------------------------------------------- 1 | ''' 2 | esta funcion pinta una caja sobre las coordenadas dadas 3 | Input: 4 | - img: imagen (array) 5 | - box: [[y0,x1,y1,x0],[y0,x1,y1,x0],...,[y0,x1,y1,x0]], es una lista de listas con todas las cajas que se quieren pintar 6 | - match_name: [[name],[name],...,[name]], lista de listas con cada uno de los nombres que se le quiere colocar a la caja, 7 | no es un parametro obligatorio 8 | ''' 9 | 10 | import cv2 11 | import numpy as np 12 | 13 | def bounding_box(img,box,match_name=[]): 14 | for i in np.arange(len(box)): 15 | x0,y0,x1,y1 = box[i] 16 | img = cv2.rectangle(img, 17 | (x0,y0), 18 | (x1,y1), 19 | (0,255,0),3); 20 | if not match_name: 21 | continue 22 | else: 23 | cv2.putText(img, match_name[i], (x0, y0-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2) 24 | return img 25 | 26 | #---------------------------------- ejemplo de consimo --------------------------------- 27 | ''' 28 | box = [[353, 123, 710, 480]] 29 | im = cv2.imread("juan.jpg") 30 | im_box = bounding_box(im,box) 31 | 32 | cv2.imshow('ejemplo',im_box) 33 | cv2.waitKey(0) 34 | ''' 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # profile_detection 2 | 3 | frontal face and profile detection implementation using OpenCV haar cascades files 4 | 5 | ![alt text](https://github.com/mevo12318/profile_detection/blob/master/results/perfil_detection.gif) 6 | 7 | ![alt text](https://github.com/mevo12318/profile_detection/blob/master/results/perfil_detection.png) -------------------------------------------------------------------------------- /__pycache__/BoundingBox.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/BoundingBox.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/common.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/common.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/config.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/config.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/f_detector.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/f_detector.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/f_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/f_utils.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/tst_scene_render.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/tst_scene_render.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/video.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/__pycache__/video.cpython-37.pyc -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | detect_frontal_face = 'haarcascades/haarcascade_frontalface_alt.xml' 2 | detect_perfil_face = 'haarcascades/haarcascade_profileface.xml' -------------------------------------------------------------------------------- /data_test/friends.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends.jpg -------------------------------------------------------------------------------- /data_test/friends10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends10.jpg -------------------------------------------------------------------------------- /data_test/friends2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends2.jpg -------------------------------------------------------------------------------- /data_test/friends3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends3.jpg -------------------------------------------------------------------------------- /data_test/friends4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends4.jpg -------------------------------------------------------------------------------- /data_test/friends5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends5.jpg -------------------------------------------------------------------------------- /data_test/friends6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends6.jpg -------------------------------------------------------------------------------- /data_test/friends7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends7.jpg -------------------------------------------------------------------------------- /data_test/friends8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends8.jpg -------------------------------------------------------------------------------- /data_test/friends9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/friends9.jpg -------------------------------------------------------------------------------- /data_test/juan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/juan.jpg -------------------------------------------------------------------------------- /data_test/profile_juan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/profile_juan.jpg -------------------------------------------------------------------------------- /data_test/test1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/test1.jpg -------------------------------------------------------------------------------- /data_test/test2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/test2.jpg -------------------------------------------------------------------------------- /data_test/test3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/test3.jpg -------------------------------------------------------------------------------- /data_test/test4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/test4.jpg -------------------------------------------------------------------------------- /data_test/test5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/test5.jpg -------------------------------------------------------------------------------- /data_test/test6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/data_test/test6.jpg -------------------------------------------------------------------------------- /f_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import config as cfg 4 | 5 | def detect(img, cascade): 6 | rects,_,confidence = cascade.detectMultiScale3(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), 7 | flags=cv2.CASCADE_SCALE_IMAGE, outputRejectLevels = True) 8 | #rects = cascade.detectMultiScale(img,minNeighbors=10, scaleFactor=1.05) 9 | if len(rects) == 0: 10 | return (),() 11 | rects[:,2:] += rects[:,:2] 12 | return rects,confidence 13 | 14 | 15 | def convert_rightbox(img,box_right): 16 | res = np.array([]) 17 | _,x_max = img.shape 18 | for box_ in box_right: 19 | box = np.copy(box_) 20 | box[0] = x_max-box_[2] 21 | box[2] = x_max-box_[0] 22 | if res.size == 0: 23 | res = np.expand_dims(box,axis=0) 24 | else: 25 | res = np.vstack((res,box)) 26 | return res 27 | 28 | 29 | class detect_face_orientation(): 30 | def __init__(self): 31 | # crear el detector de rostros frontal 32 | self.detect_frontal_face = cv2.CascadeClassifier(cfg.detect_frontal_face) 33 | # crear el detector de perfil rostros 34 | self.detect_perfil_face = cv2.CascadeClassifier(cfg.detect_perfil_face) 35 | def face_orientation(self,gray): 36 | # frontal_face 37 | box_frontal,w_frontal = detect(gray,self.detect_frontal_face) 38 | if len(box_frontal)==0: 39 | box_frontal = [] 40 | name_frontal = [] 41 | else: 42 | name_frontal = len(box_frontal)*["frontal"] 43 | # left_face 44 | box_left, w_left = detect(gray,self.detect_perfil_face) 45 | if len(box_left)==0: 46 | box_left = [] 47 | name_left = [] 48 | else: 49 | name_left = len(box_left)*["left"] 50 | # right_face 51 | gray_flipped = cv2.flip(gray, 1) 52 | box_right, w_right = detect(gray_flipped,self.detect_perfil_face) 53 | if len(box_right)==0: 54 | box_right = [] 55 | name_right = [] 56 | else: 57 | box_right = convert_rightbox(gray,box_right) 58 | name_right = len(box_right)*["right"] 59 | 60 | boxes = list(box_frontal)+list(box_left)+list(box_right) 61 | names = list(name_frontal)+list(name_left)+list(name_right) 62 | return boxes, names 63 | 64 | -------------------------------------------------------------------------------- /perfil_detecttion.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import time 3 | import imutils 4 | import BoundingBox 5 | import f_detector 6 | 7 | # instanciar detector 8 | detector = f_detector.detect_face_orientation() 9 | 10 | # visualizar 11 | cv2.namedWindow("preview") 12 | cam = cv2.VideoCapture(0) 13 | while True: 14 | # read the frame from the camera and send it to the server 15 | star_time = time.time() 16 | ret, frame = cam.read() 17 | frame = cv2.flip(frame, 1) 18 | frame = imutils.resize(frame,width=720) 19 | #-------------------------- Insertar preproceso ------------------------------------- 20 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 21 | # detectar si hay un rostro frontal o de perfil 22 | boxes,names = detector.face_orientation(gray) 23 | frame = BoundingBox.bounding_box(frame,boxes,names) 24 | # ---------------------------------------------------------------------------- 25 | end_time = time.time() - star_time 26 | FPS = 1/end_time 27 | cv2.putText(frame,f"FPS: {round(FPS,3)}",(10,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2) 28 | cv2.imshow('preview',frame) 29 | if cv2.waitKey(1) &0xFF == ord('q'): 30 | break -------------------------------------------------------------------------------- /results/perfil_detection.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/results/perfil_detection.gif -------------------------------------------------------------------------------- /results/perfil_detection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juan-csv/profile_detection/b47a953faffb9f239590b7e89b987c0676bec972/results/perfil_detection.png -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import time 3 | import imutils 4 | import numpy as np 5 | 6 | 7 | def detect(img, cascade): 8 | rects,_,confidence = cascade.detectMultiScale3(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), 9 | flags=cv2.CASCADE_SCALE_IMAGE, outputRejectLevels = True) 10 | #rects = cascade.detectMultiScale(img,minNeighbors=10, scaleFactor=1.05) 11 | if len(rects) == 0: 12 | return (),() 13 | rects[:,2:] += rects[:,:2] 14 | return rects,confidence 15 | 16 | def bounding_box(img,box,match_name=[]): 17 | for i in np.arange(len(box)): 18 | x0,y0,x1,y1 = box[i] 19 | img = cv2.rectangle(img, 20 | (x0,y0), 21 | (x1,y1), 22 | (0,255,0),3); 23 | if not match_name: 24 | continue 25 | else: 26 | cv2.putText(img, match_name[i], (x0, y0-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2) 27 | return img 28 | 29 | def convert_rightbox(img,box_right): 30 | res = np.array([]) 31 | _,x_max = img.shape 32 | for box_ in box_right: 33 | box = np.copy(box_) 34 | box[0] = x_max-box_[2] 35 | box[2] = x_max-box_[0] 36 | if res.size == 0: 37 | res = np.expand_dims(box,axis=0) 38 | else: 39 | res = np.vstack((res,box)) 40 | return res 41 | 42 | 43 | 44 | 45 | # crear el detector de rostros frontal 46 | detect_frontal_face = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml') 47 | # crear el detector de perfil rostros 48 | detect_perfil_face = cv2.CascadeClassifier('haarcascades/haarcascade_profileface.xml') 49 | 50 | img = cv2.imread("data_test/profile_juan.jpg") 51 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 52 | 53 | # frontal_face 54 | box_frontal,w_frontal = detect(gray,detect_frontal_face) 55 | if len(box_frontal)==0: 56 | box_frontal = [] 57 | name_frontal = [] 58 | else: 59 | name_frontal = len(box_frontal)*["frontal"] 60 | # left_face 61 | box_left, w_left = detect(gray,detect_perfil_face) 62 | if len(box_left)==0: 63 | box_left = [] 64 | name_left = [] 65 | else: 66 | name_left = len(box_left)*["left"] 67 | # right_face 68 | gray_flipped = cv2.flip(gray, 1) 69 | box_right, w_right = detect(gray_flipped,detect_perfil_face) 70 | if len(box_right)==0: 71 | box_right = [] 72 | name_right = [] 73 | else: 74 | box_right = convert_rightbox(gray,box_right) 75 | name_right = len(box_right)*["right"] 76 | 77 | boxes = list(box_frontal)+list(box_left)+list(box_right) 78 | names = list(name_frontal)+list(name_left)+list(name_right) 79 | 80 | 81 | 82 | 83 | 84 | 85 | res_img = bounding_box(img,boxes,names) 86 | cv2.imshow('profile_detection',res_img) 87 | cv2.waitKey(0) 88 | 89 | 90 | 91 | --------------------------------------------------------------------------------