-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacerecon.py
49 lines (42 loc) · 1.99 KB
/
facerecon.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
import cv2
from faces import get_faces
import uuid
class FaceRecognition():
def __init__(self, video: str, fullpic=False):
self.vidcap = cv2.VideoCapture(video)
self.face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
self.profile_cascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
self.fullpic = fullpic
sec = 0
frameRate = 1 #//it will capture image in each 0.5 second
count=1
success = self._getFrame(sec, count)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = self._getFrame(sec, count)
def _getFrame(self, sec, count):
self.vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = self.vidcap.read()
if hasFrames:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.2, 5)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
cropped = image[y:y+h, x:x+h]
if cropped.size != 0:
cv2.imwrite("crop/face" + str(count)+ '_' + str(uuid.uuid1())+".jpg", cropped)
profiles = self.profile_cascade.detectMultiScale(gray, 1.2, 5)
for (x, y, w, h) in profiles:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cropped = image[y:y+h, x:x+h]
if cropped.size != 0:
cv2.imwrite("side/face" + str(count)+ '_' + str(uuid.uuid1())+".jpg", cropped)
if self.fullpic:
img_name = "images/image"+str(count)+".jpg"
cv2.imwrite(img_name, image) # save frame as JPG file
# get_faces(img_name)
# cv2.imwrite("images/image"+str(count)+".jpg", image) # save frame as JPG file
return hasFrames
fr = FaceRecognition('tbbt_science.mp4')