2023-11-07 09:27:35 -06:00
|
|
|
import cv2
|
|
|
|
import time
|
2023-11-07 09:50:50 -06:00
|
|
|
from PIL import Image
|
|
|
|
import numpy as np
|
2023-11-07 09:27:35 -06:00
|
|
|
|
|
|
|
# Folder
|
|
|
|
folder = "frames"
|
|
|
|
|
|
|
|
# Initialize the webcam
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
|
|
|
|
# Check if the webcam is opened correctly
|
|
|
|
if not cap.isOpened():
|
|
|
|
raise IOError("Cannot open webcam")
|
|
|
|
|
|
|
|
# Wait for the camera to initialize and adjust light levels
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
ret, frame = cap.read()
|
|
|
|
if ret:
|
2023-11-07 09:50:50 -06:00
|
|
|
# Convert the frame to a PIL image
|
|
|
|
pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
|
|
# Resize the image
|
|
|
|
max_size = 250
|
|
|
|
ratio = max_size / max(pil_img.size)
|
|
|
|
new_size = tuple([int(x*ratio) for x in pil_img.size])
|
|
|
|
resized_img = pil_img.resize(new_size, Image.LANCZOS)
|
|
|
|
|
|
|
|
# Convert the PIL image back to an OpenCV image
|
|
|
|
frame = cv2.cvtColor(np.array(resized_img), cv2.COLOR_RGB2BGR)
|
|
|
|
|
2023-11-07 09:27:35 -06:00
|
|
|
# Save the frame as an image file
|
|
|
|
print("📸 Say cheese! Saving frame.")
|
|
|
|
path = f"{folder}/frame.jpg"
|
|
|
|
cv2.imwrite(path, frame)
|
|
|
|
else:
|
|
|
|
print("Failed to capture image")
|
|
|
|
|
2023-11-07 11:41:34 -06:00
|
|
|
# Wait for 2 seconds
|
2023-11-07 09:27:35 -06:00
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
# Release the camera and close all windows
|
|
|
|
cap.release()
|
|
|
|
cv2.destroyAllWindows()
|