-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameFind.py
165 lines (117 loc) · 7.85 KB
/
NameFind.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
160
161
162
163
164
165
# ----------- FUNCTION TO READ THE FILE AND ADD THE NAMES AND IDs IN TO TUPLES
import cv2
import math
import time
now_time = time.clock()
face = cv2.CascadeClassifier('Haar/haarcascade_frontalcatface.xml')
glass_cas = cv2.CascadeClassifier('Haar/haarcascade_eye_tree_eyeglasses.xml')
WHITE = [255, 255, 255]
def FileRead():
Info = open("Names.txt", "r") # Open th text file in readmode
NAME = [] # The tuple to store Names
while (True): # Read all the lines in the file and store them in two tuples
Line = Info.readline()
if Line == '':
break
NAME.append (Line.split(",")[1].rstrip())
return NAME # Return the two tuples
Names = FileRead() # Run the above Function to get the ID and Names Tuple
# ------------------- FUNCTION TO FIND THE NAME -----------------------------------------------------------
def ID2Name(ID, conf):
if ID > 0:
NameString = "Name: " + Names[ID-1] + " Distance: " + (str(round(conf)) ) # Find the Name using the index of the ID
else:
NameString = " Face Not Recognised " # Find the Name using the index of the ID
return NameString
# ------------------- THIS FUNCTION READ THE FILE AND ADD THE NAME TO THE END OF THE FILE -----------------
def AddName():
Name = raw_input('Enter Your Name ')
Info = open("Names.txt", "r+")
ID = ((sum(1 for line in Info))+1)
Info.write(str(ID) + "," + Name + "\n")
print ("Name Stored in " + str(ID))
Info.close()
return ID
# ------------------- DRAW THE BOX AROUND THE FACE, ID and CONFIDENCE -------------------------------------
def DispID(x, y, w, h, NAME, Image):
# --------------------------------- THE POSITION OF THE ID BOX ---------------------------------------------
Name_y_pos = y - 10
Name_X_pos = x + w/2 - (len(NAME)*7/2)
if Name_X_pos < 0:
Name_X_pos = 0
elif (Name_X_pos +10 + (len(NAME) * 7) > Image.shape[1]):
Name_X_pos= Name_X_pos - (Name_X_pos +10 + (len(NAME) * 7) - (Image.shape[1]))
if Name_y_pos < 0:
Name_y_pos = Name_y_pos = y + h + 10
# ------------------------------------ THE DRAWING OF THE BOX AND ID --------------------------------------
draw_box(Image, x, y, w, h)
cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), (0,0,0), -2) # Draw a Black Rectangle over the face frame
cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), WHITE, 1)
cv2.putText(Image, NAME, (Name_X_pos, Name_y_pos - 10), cv2.FONT_HERSHEY_DUPLEX, .4, WHITE) # Print the name of the ID
def draw_box(Image, x, y, w, h):
cv2.line(Image, (x, y), (x + (w/5) ,y), WHITE, 2)
cv2.line(Image, (x+((w/5)*4), y), (x+w, y), WHITE, 2)
cv2.line(Image, (x, y), (x, y+(h/5)), WHITE, 2)
cv2.line(Image, (x+w, y), (x+w, y+(h/5)), WHITE, 2)
cv2.line(Image, (x, (y+(h/5*4))), (x, y+h), WHITE, 2)
cv2.line(Image, (x, (y+h)), (x + (w/5) ,y+h), WHITE, 2)
cv2.line(Image, (x+((w/5)*4), y+h), (x + w, y + h), WHITE, 2)
cv2.line(Image, (x+w, (y+(h/5*4))), (x+w, y+h), WHITE, 2)
# --------------- SECOND ID BOX ----------------------
def DispID2(x, y, w, h, NAME, Image):
# --------------------------------- THE POSITION OF THE ID BOX -------------------------------------------------
Name_y_pos = y - 40
Name_X_pos = x + w/2 - (len(NAME)*7/2)
if Name_X_pos < 0:
Name_X_pos = 0
elif (Name_X_pos +10 + (len(NAME) * 7) > Image.shape[1]):
Name_X_pos= Name_X_pos - (Name_X_pos +10 + (len(NAME) * 7) - (Image.shape[1]))
if Name_y_pos < 0:
Name_y_pos = Name_y_pos = y + h + 10
# ------------------------------------ THE DRAWING OF THE BOX AND ID --------------------------------------
cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), (0,0,0), -2) # Draw a Black Rectangle over the face frame
cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), WHITE, 1)
cv2.putText(Image, NAME, (Name_X_pos, Name_y_pos - 10), cv2.FONT_HERSHEY_DUPLEX, .4, WHITE) # Print the name of the ID
# --------------- THIRD ID BOX ----------------------
def DispID3(x, y, w, h, NAME, Image):
# --------------------------------- THE POSITION OF THE ID BOX -------------------------------------------------
Name_y_pos = y - 70
Name_X_pos = x + w/2 - (len(NAME)*7/2)
if Name_X_pos < 0:
Name_X_pos = 0
elif (Name_X_pos +10 + (len(NAME) * 7) > Image.shape[1]):
Name_X_pos= Name_X_pos - (Name_X_pos +10 + (len(NAME) * 7) - (Image.shape[1]))
if Name_y_pos < 0:
Name_y_pos = Name_y_pos = y + h + 10
# ------------------------------------ THE DRAWING OF THE BOX AND ID --------------------------------------
cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), (0,0,0), -2) # Draw a Black Rectangle over the face frame
cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), WHITE, 1)
cv2.putText(Image, NAME, (Name_X_pos, Name_y_pos - 10), cv2.FONT_HERSHEY_DUPLEX, .4, WHITE) # Print the name of the ID
def DrawBox(Image, x, y, w, h):
cv2.rectangle(Image, (x, y), (x + w, y + h), (255, 255, 255), 1) # Draw a rectangle arround the face
# ----------------------------- THIS FUNCTION TAKES IN SPEC CASCADE, FACE CASCADE AND AN IMAGE
# ------------------------- IT RETURNS A CROPPED FACE AND IF POSSIBLE STRAIGHTENS THE TILT OF THE HEAD
def DetectEyes(Image):
Theta = 0
rows, cols = Image.shape
glass = glass_cas.detectMultiScale(Image) # This ditects the eyes
for (sx, sy, sw, sh) in glass:
if glass.shape[0] == 2: # The Image should have 2 eyes
if glass[1][0] > glass[0][0]:
DY = ((glass[1][1] + glass[1][3] / 2) - (glass[0][1] + glass[0][3] / 2)) # Height diffrence between the glass
DX = ((glass[1][0] + glass[1][2] / 2) - glass[0][0] + (glass[0][2] / 2)) # Width diffrance between the glass
else:
DY = (-(glass[1][1] + glass[1][3] / 2) + (glass[0][1] + glass[0][3] / 2)) # Height diffrence between the glass
DX = (-(glass[1][0] + glass[1][2] / 2) + glass[0][0] + (glass[0][2] / 2)) # Width diffrance between the glass
if (DX != 0.0) and (DY != 0.0): # Make sure the the change happens only if there is an angle
Theta = math.degrees(math.atan(round(float(DY) / float(DX), 2))) # Find the Angle
print "Theta " + str(Theta)
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), Theta, 1) # Find the Rotation Matrix
Image = cv2.warpAffine(Image, M, (cols, rows))
# cv2.imshow('ROTATED', Image) # UNCOMMENT IF YOU WANT TO SEE THE
Face2 = face.detectMultiScale(Image, 1.3, 5) # This detects a face in the image
for (FaceX, FaceY, FaceWidth, FaceHeight) in Face2:
CroppedFace = Image[FaceY: FaceY + FaceHeight, FaceX: FaceX + FaceWidth]
return CroppedFace
def tell_time_passed():
print 'TIME PASSED ' + str(round(((time.clock() - now_time)/60), 2)) + ' MINS'