Skip to content

Instantly share code, notes, and snippets.

@goksinan
Created May 29, 2019 18:59
Show Gist options
  • Save goksinan/04ca12c1d85812fbb26a337b75e57cd0 to your computer and use it in GitHub Desktop.
Save goksinan/04ca12c1d85812fbb26a337b75e57cd0 to your computer and use it in GitHub Desktop.
Continuous video recording using an Allied Vision camera
"""
Continuous video recording using an Allied Vision camera.
Video is saved in mp4 format.
See "https://github.com/morefigs/pymba" to learn more about pymba wrapper.
Big part of the code is borrowed from their Examples directory.
I added a video writer object (OpenCV).
Also, frame number is being displayed in the same line.
"""
from time import sleep
from pymba import Vimba, Frame
from typing import Optional
import cv2
def display_frame(frame: Frame, delay: Optional[int] = 1) -> None:
"""
Displays the acquired frame.
:param frame: The frame object to display.
:param delay: Display delay in milliseconds, use 0 for indefinite.
"""
PIXEL_FORMATS_CONVERSIONS = {
'BayerRG8': cv2.COLOR_BAYER_RG2RGB,
}
print('Frame: %d\r' % frame.data.frameID, end="")
# get a copy of the frame data
image = frame.buffer_data_numpy()
# convert colour space if desired
try:
image = cv2.cvtColor(image, PIXEL_FORMATS_CONVERSIONS[frame.pixel_format])
except KeyError:
pass
# write image to output file
out.write(image)
# display image
cv2.imshow('Image', image)
cv2.waitKey(delay)
with Vimba() as vimba:
camera = vimba.camera(0)
camera.open()
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter('output.mp4', fourcc, 30.0, (640, 480))
camera.Height = 480
camera.Width = 640
camera.AcquisitionFrameRateAbs = 30
# arm the camera and provide a function to be called upon frame ready
camera.arm('Continuous', display_frame)
camera.start_frame_acquisition()
# stream images for a while...
sleep(5)
# stop frame acquisition
# start_frame_acquisition can simply be called again if the camera is still armed
camera.stop_frame_acquisition()
sleep(0.2)
camera.disarm()
camera.close()
out.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment