This repository was archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_data.py
72 lines (56 loc) · 2.02 KB
/
capture_data.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
60
61
62
63
64
65
66
67
68
69
70
71
72
import cv2
import json
import os
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
from util import get_contacts
# Source for code used in this project: https://www.hackster.io/mjrobot/real-time-face-recognition-an-end-to-end-project-a10826
def main():
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(.1)
face_detector = cv2.CascadeClassifier(
'haarcascade_frontalface_default.xml')
u_id = input('Enter the id for this user: ')
contacts = get_contacts()
if (u_id in contacts):
print('Updating data for user: [' + u_id + '] ' + contacts[(u_id)])
else:
name = input('Enter a name for this user: ')
contacts[u_id] = name
print('Creating data for user: [' + u_id + '] ' + contacts[u_id])
try:
with open('contacts.json', 'w') as f:
json.dump(contacts, f)
print('wrote json file')
except Exception as e:
print(e)
return
num_imgs = int(
input('Enter the number of pictures to take of this user: '))
count = 0
print('Please look at the camera')
for frame in camera.capture_continuous(
rawCapture, format="bgr", use_video_port=True):
img = frame.array
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
count += 1
print('Captured ' + str(count) + ' / ' + str(num_imgs) + ' images')
cv2.imwrite(
"dataset/User." + str(u_id) + '.' + str(count) + ".jpg",
gray[y:y + h, x:x + w])
k = cv2.waitKey(100) & 0xff
if k == 27:
break
elif count >= num_imgs:
break
rawCapture.truncate(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()