Skip to content

Instantly share code, notes, and snippets.

@kaeton
Created July 11, 2018 02:42
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 kaeton/5da23b3c0ab07881e5d774c8a198fc34 to your computer and use it in GitHub Desktop.
Save kaeton/5da23b3c0ab07881e5d774c8a198fc34 to your computer and use it in GitHub Desktop.
usbカメラのデータ取得用
import cv2
from datetime import datetime
# camera number
class CaptureMethod:
def __init__(self, imagesize, x_axis=False, y_axis=False):
self.x_axis = x_axis
self.y_axis = y_axis
self.fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
self.imagesize = imagesize
tdatetime = datetime.now()
tstr = tdatetime.strftime('%Y%m%d_%H%M%S.mp4')
self.outfile = cv2.VideoWriter(
tstr,
self.fourcc,
30.0,
self.imagesize
)
def capture_camera(self, size=None):
cap = cv2.VideoCapture(1)
while True:
# retは画像を取得成功フラグ
ret0, frame0 = cap.read()
if ret0 is False:
print(ret0)
continue
# フレームをリサイズ
# sizeは例えば(800, 600)
# フレームを表示する
cv2.imshow('camera capture 0', frame0)
frame0 = cv2.resize(frame0, self.imagesize)
self.outfile.write(frame0)
k = cv2.waitKey(1) # 1msec待つ
if k == 27: # ESCキーで終了
break
# キャプチャを解放する
ret_cap = cap.release()
ret_writer = self.outfile.release()
print(ret_cap, ret_writer)
cv2.destroyAllWindows()
if __name__ == "__main__":
capture = CaptureMethod(imagesize=(640,480))
capture.capture_camera()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment