Last active
May 11, 2018 08:13
-
-
Save kaeton/6926f0a6503ad58a2f735df41a70420d to your computer and use it in GitHub Desktop.
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 | |
# camera number | |
class CaptureMethod: | |
def __init__(self, number=2, x_axis=False, y_axis=False): | |
self.camera_number = number | |
self.x_axis = x_axis | |
self.y_axis = y_axis | |
def capture_camera(self, mirror=True, size=None): | |
"""Capture video from camera""" | |
# カメラをキャプチャする | |
# cap0 = cv2.VideoCapture(0) # 0はカメラのデバイス番号 | |
# cap1 = cv2.VideoCapture(1) # 0はカメラのデバイス番号 | |
# cap = [cap0, cap1] | |
cap = [] | |
for i in range(self.camera_number): | |
cap.append(cv2.VideoCapture(i-1)) | |
while True: | |
# retは画像を取得成功フラグ | |
ret0, frame0 = cap[0].read() | |
ret1, frame1 = cap[1].read() | |
# 鏡のように映るか否か | |
if mirror is True: | |
frame0 = frame0[:,::-1] | |
frame1 = frame1[:,::-1] | |
# フレームをリサイズ | |
# sizeは例えば(800, 600) | |
if size is not None and len(size) == 2: | |
frame0 = cv2.resize(frame0, size) | |
frame1 = cv2.resize(frame1, size) | |
# フレームを表示する | |
cv2.imshow('camera capture 0', frame0) | |
cv2.imshow('camera capture 1', frame1) | |
k = cv2.waitKey(1) # 1msec待つ | |
if k == 27: # ESCキーで終了 | |
break | |
# キャプチャを解放する | |
cap[0].release() | |
cap[1].release() | |
cv2.destroyAllWindows() | |
if __name__ == "__main__": | |
capture = CaptureMethod() | |
capture.capture_camera(mirror=False) |
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 argparse | |
import gzip | |
class CaptureMethod: | |
def __init__(self, | |
background_movie_file="", | |
fisheye_calibratio_file="", | |
camera_number=2, | |
imagesize=(640,480), | |
outputfilename="output",): | |
self.background_movie = background_movie_file | |
self.camera_number = camera_number | |
self.calibration_file = fisheye_calibratio_file | |
self.fourcc = cv2.VideoWriter_fourcc('m','p','4','v') | |
self.outfiles = [] | |
self.timestamp = gzip.open(str(outputfilename) + ".txt.gz", 'w') | |
for i in range(camera_number): | |
self.outfiles.append(cv2.VideoWriter( | |
str(outputfilename) + "_" + str(i) + ".mp4", | |
self.fourcc, | |
20.0, | |
imagesize) | |
) | |
def record_log(self, log): | |
with gzip.open("hello.txt.gz", 'w') as f: | |
for _ in range(N): | |
f.write(b"hello, world\n") | |
def capture_camera(self, mirror=False, size=None): | |
"""Capture video from camera""" | |
# カメラをキャプチャする | |
cap = [] | |
for i in range(self.camera_number): | |
cap.append(cv2.VideoCapture(i)) | |
while True: | |
frames = [c.read()[1] for c in cap] | |
# 鏡のように映るか否か | |
if mirror is True: | |
frames = [frame[:,::-1] for frame in frames] | |
# フレームをリサイズ | |
# sizeは例えば(800, 600) | |
if size is not None and len(size) == 2: | |
for frame in frames: | |
frame = cv2.resize(frame, size) | |
# フレームを表示する | |
for i, frame in enumerate(frames): | |
cv2.imshow('camera capture%d' % i, frame) | |
for outfile, frame in zip(self.outfiles, frames): | |
outfile.write(frame) | |
k = cv2.waitKey(1) # 1msec待つ | |
if k == 27: # ESCキーで終了 | |
break | |
# キャプチャを解放する | |
for c in cap: | |
c.release() | |
for outfile in self.outfiles: | |
outfile.release() | |
cv2.destroyAllWindows() | |
# def fisheye_calibration(self): | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('--out', type=str, nargs='+', | |
help='the output movie file name') | |
args = parser.parse_args() | |
if args.out is None: | |
capture_camera = CaptureMethod() | |
else: | |
capture_camera = CaptureMethod(outputfilename=args.out[0]) | |
capture_camera.capture_camera(mirror=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment