-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathface_recognition.py
More file actions
133 lines (105 loc) · 4.81 KB
/
face_recognition.py
File metadata and controls
133 lines (105 loc) · 4.81 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
# REFRENCED CODE:
# https://www.datacamp.com/community/tutorials/face-detection-python-opencv
#
# DATE: October 12th, 2021
# DESCRIPTION: This program is meant to provide facial-recognition capabilities to the rest of the project.
#
# DEPENDENCIES:
# OpenCV | pip install opencv-python \\For computer vision
# matplotlib | pip install -U matplotlib \\For testing
# numpy | pip install numpy \\For matrix manipulation
__opencv_version__ = r'4.1.1'
import sys
import cv2 as cv
import math as ma
# import BotInterface as bi
#import numpy as np
#import matplotlib.pyplot as plt
#import matplotlib.image as mpimg
size =(1300,800)
if(__opencv_version__ != cv.__version__):
print('WARNING: The OpenCV version being used ({}) is different from the OpenCV version this module was written in! ({})'.format(cv.__version__, __opencv_version__))
__training_xml__ = cv.data.haarcascades + 'haarcascade_frontalface_alt2.xml'
__cascade__ = cv.CascadeClassifier(__training_xml__) #load the already-trained facial-recongition model
#specify which detection model to use
def use_model(cascade):
__cascade__ = cascade
#given an image and a scale factor, find faces in an image and return that image with rectangles around the faces
def detect_faces(image):
image_gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY) #create a grayscale copy of the image (used for face detection)
#find the faces.
#TODO: Have an explanation as to how 'detectMultiScale' works so we can better-utilize scaleFactor and minNeighbors
#** scaleFactor is a parameter specifying how much the image size is reduced at each image scale.
#** minNeighbors is a parameter specifying how many neighbors each candidate rectangle should have to retain it.
faces_rects = __cascade__.detectMultiScale(image_gray, scaleFactor = 1.3, minNeighbors = 5)
#print how many faces were found
# print('Faces found: {}'.format(len(faces_rects)))
return faces_rects
def createLine(im,st, en,co):
cv.line(im,st, en,co , 5)
def draw_rects(image, rects):
#draw rectangles around the bounds of the detected faces
xx,yy,ww,hh = 0,0,0,0
area = 0
stat = False
for (x, y, w, h) in rects:
stat = True
#draws a red rectangle with a thickness of 2
if w*h > area:
area = w*h
xx,yy,ww,hh = x,y,w,h
cv.rectangle(image, (x, y), (x + w - 15, y + h - 15), (0, 255, 0), 2)
if(stat):
start = ((int)(size[0] / 2),(int) (size[1] / 2))
tl = ((int)(xx + (ww / 2)), (int)(yy + (hh / 2)))
th = ((int)(size[0] / 2), (int)(yy + (hh / 2)))
thd = start[1] - th[1]
tw = ((int)(xx + (ww / 2)), (int)(size[1] / 2))
twd = start[0] - tl[0]
createLine(image, start, tl , (255,0,0))
createLine(image, start, th, (0, 255, 0))
createLine(image, th, tl, (0, 0, 255))
print("Angle = ",ma.tan(twd/(thd+0.001)))
cv.rectangle(image, (xx, yy), (xx + ww, yy + hh), (0, 0, 255), 2) # The closest person has a red rectangle
if __name__ == "__main__":
# bi.Calibrat()
# Using camera
cap = cv.VideoCapture(0) # The Parameter is the index of the camera
if not cap.isOpened():
print("Unable to capture camera")
while cap.isOpened():
ret, image = cap.read() # Read each frame as an image
# test_image = cv.imread(image)
if ret:
face_rects = detect_faces(image) # detect the faces
draw_rects(image, face_rects)
#TODO: change from an image display to a video display
# #show the image and waits for a key press before exiting
image = cv.resize(image, size)
cv.imshow('Detected Faces', image)
key = cv.waitKey(1) #waits for 2 milliseconds for a key press on a OpenCV window
if key == 113: # it breaks when q is pressed
break
else:
print("Unable to get video frame from camera")
break
cap.release()
cv.destroyAllWindows()
# if __name__ == '__main__':
# import sys
# test_image = cv.imread(sys.argv[1]) #load the image from the file specified from the command line
# result_image = detect_faces(test_image) #detect the faces
# # #show the image and waits for a key press before exiting
# cv.imshow('Detected Faces', result_image)
# cv.waitKey(0)
# cv.destroyAllWindows()
# For testing from code
# image = r'/Users/rediettadesse/Documents/GitHub/ProjectX0/image3.jpeg' #image full path
# test_image = cv.imread(image) #load the image from the file specified from the command line
# result_image = detect_faces(test_image) #detect the faces
# draw_rects(test_image,result_image)
# #show the image and waits for a key press before exiting
# cv.imshow('Detected Faces', test_image)
# # # print(cv.waitKey(0))
# cv.waitKey(0)
# cv.destroyAllWindows()