Created
March 3, 2023 15:07
-
-
Save CnrLwlss/e6fcb23edb05099fefede4cdf5ffb4d4 to your computer and use it in GitHub Desktop.
Previewing and then capturing HD video frames from webcam with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import time | |
from pathlib import Path | |
import os | |
cam = cv2.VideoCapture(1) | |
cam.set(3, 1920) | |
cam.set(4, 1080) | |
cv2.namedWindow("test") | |
img_counter = 0 | |
frame_counter = 0 | |
Path("output").mkdir(parents=True, exist_ok=True) | |
while True: | |
ret, frame = cam.read() | |
if not ret: | |
print("failed to grab frame") | |
break | |
cv2.imshow("Choose aperture, ISO etc.", frame) | |
k = cv2.waitKey(1) | |
if k%256 == 27: | |
# ESC pressed | |
print("Escape hit, closing...") | |
break | |
elif k%256 == 32: | |
# SPACE pressed | |
img_name = "opencv_frame_{}.png".format(img_counter) | |
cv2.imwrite(img_name, frame) | |
print("{} written!".format(img_name)) | |
img_counter += 1 | |
cv2.destroyAllWindows() | |
while True: | |
time.sleep(5) | |
ret, frame = cam.read() | |
if not ret: | |
print("failed to grab frame") | |
break | |
img_name = os.path.join("output",f'MovieFrame{frame_counter:05}.jpg') | |
cv2.imwrite(img_name, frame,[cv2.IMWRITE_JPEG_QUALITY, 80]) | |
print("{} written!".format(img_name)) | |
frame_counter += 1 | |
cam.release() | |
#ffmpeg -framerate 15 -pattern_type glob -i '*.jpg' -c:v libx264 -pix_fmt yuv420p out.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment