Last active
March 6, 2020 16:29
-
-
Save ochilab/dc9cd781b3566eb451d6b20a098bda2d to your computer and use it in GitHub Desktop.
Python: OpenCVでウェブカメラ表示と録画
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
#!/usr/bin/env python | |
from PIL import Image | |
import cv2 as cv | |
# FPS | |
fps = 30 | |
# Frame size | |
WIDTH=1920 | |
HEIGHT=1080 | |
size = (WIDTH, HEIGHT) | |
#Setting recording format | |
fourcc = cv.VideoWriter_fourcc(*'XVID') | |
#fourcc = cv.VideoWriter_fourcc('M', 'J', 'P', 'G') | |
#Recording file Setting | |
video = cv.VideoWriter('sample.avi', fourcc, fps, size) | |
#Camera Setting | |
camera = cv.VideoCapture(0) | |
camera.set(cv.CAP_PROP_FRAME_WIDTH, WIDTH) | |
camera.set(cv.CAP_PROP_FRAME_HEIGHT, HEIGHT) | |
try: | |
while True: | |
#frame read | |
_, frame = camera.read() | |
cv.imshow('image', frame) | |
#record frame | |
video.write(frame) | |
key = cv.waitKey(1) | |
if key == 27: # ESC key for closing | |
video.release() | |
f.close() | |
break | |
except KeyboardInterrupt: | |
print("Close me? -> Enter ESC") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment