-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognition.py
48 lines (37 loc) · 1.38 KB
/
face_recognition.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
import cv2, os
# Funcao para busca de arquivos
# def find(name, path):
# for root, dirs, files in os.walk(path):
# if (name in files) or (name in dirs):
# print("O diretorio/arquivo {} encontra-se em: {}".format(name, root))
# return os.path.join(root, name)
# # Caso nao encontre, recursao para diretorios anteriores
# return find(name, os.path.dirname(path))
# Importar arquivo XML
# cv2path = os.path.dirname(cv2.__file__)
# print(cv2path)
# haar_path = find('haarcascades', cv2path)
# print(haar_path)
# xml_name = 'haarcascade_frontalface_alt2.xml'
# xml_path = os.path.join(haar_path, xml_name)
# TODO: Inicializar Classificador
clf = cv2.CascadeClassifier('/home/daniel/projects/scripts/haarcascade_frontalface_alt2.xml')
# Inicializar webcam
cap = cv2.VideoCapture(0)
# Loop para leitura do conteúdo
while(not cv2.waitKey(20) & 0xFF == ord('q')):
# Capturar proximo frame
ret, frame = cap.read()
# TODO: Converter para tons de cinza
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# TODO: Classificar
faces = clf.detectMultiScale(gray)
# TODO: Desenhar retangulo
for x, y, w, h in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255))
# Visualizar
cv2.imshow('frame',frame)
# Desligar a webcam
cap.release()
#Fechar janela do vídeo
cv2.destroyAllWin