Repository navigation
Expand file tree
/
Copy pathimage_capture.py
More file actions
62 lines (47 loc) · 1.64 KB
/
Copy pathimage_capture.py
File metadata and controls
62 lines (47 loc) · 1.64 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
import cv2
import os
from datetime import datetime
import time
# Change this to the name of the person you're photographing
PERSON_NAME = "Eric_Tyrer"
def create_folder(name):
dataset_folder = "dataset"
if not os.path.exists(dataset_folder):
os.makedirs(dataset_folder)
person_folder = os.path.join(dataset_folder, name)
if not os.path.exists(person_folder):
os.makedirs(person_folder)
return person_folder
def capture_photos(name):
folder = create_folder(name)
# Initialize the camera
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Allow camera to warm up
time.sleep(2)
photo_count = 0
print(f"Taking photos for {name}. Press SPACE to capture, 'q' to quit.")
while True:
# Capture frame from camera
ret, frame = cap.read()
if not ret:
break
# Display the frame
cv2.imshow('Capture', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord(' '): # Space key
photo_count += 1
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{name}_{timestamp}.jpg"
filepath = os.path.join(folder, filename)
cv2.imwrite(filepath, frame)
print(f"Photo {photo_count} saved: {filepath}")
elif key == ord('q'): # Q key
break
# Clean up
cv2.destroyAllWindows()
cap.release()
print(f"Photo capture completed. {photo_count} photos saved for {name}.")
if __name__ == "__main__":
capture_photos(PERSON_NAME)