forked from ch-tseng/Boss-Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect-faces.py
59 lines (47 loc) · 1.67 KB
/
collect-faces.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob, os
from sklearn.preprocessing import LabelEncoder
from imutils import paths
from scipy import io
import numpy as np
import imutils
import cv2
import sys
import time
import datetime
savePath = "collected"
cam_id = 0
face_size = (47, 62)
monitor_winSize = (640, 480)
cam_resolution = (1080,960)
face_cascade = cv2.CascadeClassifier('objects\\haarcascade_frontalface_default.xml')
#face_cascade = cv2.CascadeClassifier('objects\\lbpcascade_frontalface.xml')
if not os.path.exists(savePath):
os.makedirs(savePath)
camera = cv2.VideoCapture(cam_id)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, cam_resolution[0])
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, cam_resolution[1])
while True:
(grabbed, img) = camera.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor= 1.3,
minNeighbors=10,
minSize=face_size,
flags=cv2.CASCADE_SCALE_IMAGE
)
i = 0
for (x,y,w,h) in faces:
if(w>face_size[0] and h>face_size[1]):
roi_color = img[y:y+h, x:x+w]
now=datetime.datetime.now()
faceName = '%s_%s_%s_%s_%s_%s_%s.jpg' % (now.year, now.month, now.day, now.hour, now.minute, now.second, i)
cv2.imwrite(savePath+"\\" + faceName, roi_color)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
r = monitor_winSize[1] / img.shape[1]
dim = (monitor_winSize[0], int(img.shape[0] * r))
img2 = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Frame", img2)
key = cv2.waitKey(1)