-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceDetectionAndCropping.py
51 lines (49 loc) · 1.48 KB
/
FaceDetectionAndCropping.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
import cv2
import os
def CapNCrop(name):
os.mkdir('./Datasets/Train/' + name)
# Load the cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
i=1
k=1
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
# cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi = img[y:y+h, x:x+h]
roi = cv2.resize(roi,(270,270))
fname = './Datasets/Train/' + name + '/' + str(i) + ".jpg"
print(fname)
cv2.imwrite(filename=fname, img=roi)
print(roi.shape)
i += 1
if i==151:
k=27
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
cv2.waitKey(30)
if k == 27:
break
# Release the VideoCapture object
cap.release()
def main():
name = str(input("Enter Your Name to Save Your Face Data And Make Sure No One Else Is In Frame!!"))
try:
CapNCrop(name)
except:
print("UserName Exists")
main()
main()
import main
main