Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active March 6, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/dc9cd781b3566eb451d6b20a098bda2d to your computer and use it in GitHub Desktop.
Save ochilab/dc9cd781b3566eb451d6b20a098bda2d to your computer and use it in GitHub Desktop.
Python: OpenCVでウェブカメラ表示と録画
#!/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