-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencvcap.py
More file actions
66 lines (47 loc) · 2.08 KB
/
opencvcap.py
File metadata and controls
66 lines (47 loc) · 2.08 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
import cv2
import os
import easygui
cam = cv2.VideoCapture(0)
#Initailising the face detection module in opencv library called Cascadeclassifier into face_cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cv2.namedWindow("python face detection app")
counter = 1
#To capture multiple images from the video its runned in a loop
while True:
ret,frame = cam.read()
if not ret:
print("failed to grab frame")
break
# This converts our grayscale image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces from grayscale frame(gray)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
# Crop the face region from the frame
face_image = frame[y:y+h, x:x+w]
# For Displaying the croped face image
cv2.imshow('Face Image', face_image)
if len(faces) > 0:
#For get a poped up prompt box for entering the name
name = easygui.enterbox("Enter the name of the person: ",title="Name Entry")
#Out of capturing 200 faces 100 of them is moved into a directory called Train and the rest 100 to Test
if counter<=100:
path = f"train/{name}"
if not os.path.exists(path):
os.makedirs(path)
img_name = "{}{}.png".format(name,counter)
img = os.path.join(path,img_name)
cv2.imwrite(img,face_image)
print(f"captured and saved to train- {counter}")
else:
path = f"test/{name}"
if not os.path.exists(path):
os.makedirs(path)
img_name = "{}{}.png".format(name,counter)
img = os.path.join(path,img_name)
cv2.imwrite(img,face_image)
print(f"captured and saved to test- {counter}")
counter +=1
if counter >200:
break
cam.release()