-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (120 loc) · 5 KB
/
main.py
File metadata and controls
134 lines (120 loc) · 5 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 sys, os
class faceExtractor:
def __init__(self):
self.images = os.listdir("faces")
self.video_source = 'camera'
self.capacities = []
self.histograms = []
self.faceX = 1
self.faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
self.initilize()
def initilize(self):
if len(sys.argv) >= 1:
if sys.argv[1] == '-h' or sys.argv[1] == '--help':
print('Usage> python3 main.py VIDEO_PATH_TO_NAME(optional- default is your camera0 for live streaming)')
return False
self.video_source = sys.argv[1]
self.handler()
def existsFace(self, histogram):
self.images = os.listdir("faces")
self.capacities = []
self.histograms = []
for img in self.images:
try:
image = cv2.imread("faces/" + img)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
self.histograms.append(cv2.calcHist([gray_image], [0],
None, [256], [0, 256]))
self.capacities.append(0)
except:
None
row = 0
for histogramX in self.histograms:
i = 0
try:
while i<len(histogram) and i<len(histogramX):
self.capacities[row]+=(histogram[i]-histogramX[i])**2
i+= 1
self.capacities[row] = self.capacities[row]**(1 / 2)
except:
None
row += 1
mostSimilarIndex = 0
mostSimilar = 999999999999
i = 0
for c in self.capacities:
if c < mostSimilar:
mostSimilar = c
mostSimilarIndex = i
i += 1
if mostSimilarIndex == 0:
return False
return True
def handler(self):
try:
if self.video_source != 'camera0' or self.video_source != 'camera1':
video_capture = cv2.VideoCapture(self.video_source)
while(video_capture.isOpened()):
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
face = gray[y:y+h, x:x+w]
histogram = cv2.calcHist([face], [0],
None, [256], [0, 256])
if not self.existsFace(histogram):
cv2.imwrite("faces/{}.png".format(self.faceX), frame[y:y+h, x:x+w])
self.faceX += 1
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
if self.video_source == 'camera0':
video_capture = cv2.VideoCapture(0)
else:
video_capture = cv2.VideoCapture(1)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
face = gray[y:y+h, x:x+w]
histogram = cv2.calcHist([face], [0],
None, [256], [0, 256])
if not self.existsFace(histogram):
cv2.imwrite("faces/{}.png".format(self.faceX), frame[y:y+h, x:x+w])
self.faceX += 1
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
except:
sys.exit(0)
return False
# Starting
ob = faceExtractor()