def _camera_stream(self, fn_name, camera_type, camera_id): # Get the Function name to call if not self.channel: log.info("Connecting to API server") self.connect() stub_fn = getattr(self.stub, fn_name) h264_chunks = queue.Queue(maxsize=100) video_id = str(uuid.uuid4()) Stream( input_type=camera_type, id=camera_id, output_type='h264_pipe', output_config={ 'callback': lambda chunk: h264_chunks.put(chunk), }, reading=True, writing=True, ) # Create Generator Function for stream def gen(): while True: frame = h264_chunks.get(block=True, timeout=10) yield VideoStreamRequest(content=frame, video_id=video_id) return stub_fn( gen(), metadata=self.config.get('request_metadata') )
def stream_with_extension(extension): tmp_path = get_tmp_file(extension=extension) s = Stream( input_type='usb', id=SAMPLE_VIDEO, output_type='file', path=tmp_path, reading=True, writing=True, ) time.sleep(2) s.stop() assert os.stat(tmp_path).st_size > 0 rm_tmp_dir()
def ffmpeg_to_h264_to_raw(): ''' input: ffmpeg camera, reads in compressed frames in h264 format output: ffmpeg raw video ''' s = Stream( input_type='ffmpeg', id=0, input_config={'output_format': 'h264'}, output_type='ffmpeg', path='./tmp/usb.yuv', reading=True, writing=True, ) time.sleep(WRITE_TIME) s.stop()
def ffmpeg_to_compressed_video(): ''' input: ffmpeg camera, reads in raw frames output: ffmpeg compressed video, using a random supported compressed video extension ''' supported_video_extensions = ['avi', 'mp4', 'mkv'] s = Stream( input_type='ffmpeg', id=0, output_type='ffmpeg', path=f'./tmp/usb.{random.choice(supported_video_extensions)}', reading=True, writing=True, ) time.sleep(WRITE_TIME) s.stop()
def ffmpeg_to_raw_video(): ''' input: ffmpeg camera, reads in raw frames output: ffmpeg raw video functionally equivalent to output_type='raw' ''' s = Stream( input_type='ffmpeg', id=0, output_type='ffmpeg', path='./tmp/usb.yuv', # this is the same # output_type='raw', path='./tmp/usb.yuv', reading=True, writing=True, ) time.sleep(WRITE_TIME) s.stop()
def test_stream(): ''' Test an usb stream: usb -> file ''' TMP_FILE = get_tmp_file(extension='.raw') s = Stream( input_type='raw_video', id=SAMPLE_RAW_VIDEO, output_type='file', path=TMP_FILE, reading=True, writing=True, ) time.sleep(2) s.stop() assert os.stat(TMP_FILE).st_size > 0 rm_tmp_dir()
def test_stream(): ''' Test an ffmeg stream: ffmpeg -> file ''' try: s = Stream( input_type='ffmpeg', id=0, output_type='file', path=get_tmp_file(), reading=True, writing=True, ) except Exception as e: pytest.skip( f'Test could not be run; stream initialization failed with error: {e}' ) time.sleep(2) s.stop() assert os.stat(get_tmp_file()).st_size > 0 rm_tmp_dir()
FILE_PATH = './tmp/usb.mkv' # enable live preview of pyueye def on_frame_read(data, timestamp): cv2.imshow('wow', data) cv2.waitKey(1) s = Stream( input_type='ueye', id=CAMERA_ID, input_config={ 'fps': 60, 'exposure': 60, 'autofocus': 1, 'autogain': 1, 'focus_min': 780, 'focus_max': 900, }, on_read=on_frame_read, output_type='file', path=FILE_PATH, reading=True, writing=True, ) time.sleep(SLEEP_TIME) s.stop()
import time from senseye_cameras import Stream s = Stream( input_type='ueye', id=0, input_config={ 'fps': 60, 'exposure': 60, 'autofocus': 1, 'autogain': 1, }, output_type='raw', path='./tmp/ueye.raw', reading=True, writing=True, ) time.sleep(10) s.stop()
import time from senseye_cameras import Stream ''' Example pylon stream. ''' SLEEP_TIME = 5 CAMERA_ID = 0 FILE_PATH = './tmp/usb.mkv' s = Stream( input_type='pylon', id=CAMERA_ID, output_type='file', output_config={ 'fps': 80, }, path='./tmp/usb.mkv', reading=True, ) time.sleep(2) s.start_writing() time.sleep(10) s.stop()
def on_frame_written(data): global frames_written print(f'Wrote frame.') frames_written += 1 # create a stream using a usb camera that outputs to an avi file. # every time a frame is read, call the function on_frame_read. # every time a frame is written, call the function on_frame_written. s = Stream( input_type='usb', id=CAMERA_ID, output_type='file', path=FILE_PATH, on_read=on_frame_read, on_write=on_frame_written, reading=True, writing=False, ) time.sleep(1) # since the writing kwarg passed to the stream was False, we must manually start the stream s.start_writing() time.sleep(SLEEP_TIME) s.stop() print(f'frames_read: {frames_read}') print(f'frames_written: {frames_written}')
def test_stream_video_override(): '''Ensure that streams do not override video files.''' TMP_FILE = get_tmp_file(extension='.raw') s = Stream( input_type='usb', id=SAMPLE_VIDEO, input_config={'fps': 10}, output_type='raw', path=TMP_FILE, reading=False, writing=False, ) s.start_reading() s.start_writing() time.sleep(2) s.stop_writing() s.stop_reading() time.sleep(1) s.start_reading() s.start_writing() tmp_path = s.writer.output.tmp_path time.sleep(2) s.stop_writing() s.stop_reading() s.stop() assert os.stat(TMP_FILE).st_size > 0 assert os.stat(tmp_path).st_size > 0 rm_tmp_dir()
import time from senseye_cameras import Stream ''' Takes a usb input and writes to a file. The input_type can be easily switched to ffmpeg/pylon/ueye/raw_video. ''' SLEEP_TIME = 5 CAMERA_ID = 0 # supported file extensions # FILE_PATH = './tmp/usb.avi' # FILE_PATH = './tmp/usb.mp4' FILE_PATH = './tmp/usb.mkv' # FILE_PATH = './tmp/usb.yuv' # FILE_PATH = './tmp/usb.raw' s = Stream( input_type='usb', id=CAMERA_ID, output_type='file', path=FILE_PATH, reading=True, writing=True, ) time.sleep(SLEEP_TIME) s.stop()
import time from senseye_cameras import Stream ''' Takes a usb input and pipes it out as an h264 bytestream. The bytestream output can be intercepted by passing a callback to the output_config. ''' SLEEP_TIME = 5 CAMERA_ID = 0 # how many bytes from the bytestream should be passed to our callback BLOCK_SIZE = 2048 def my_callback(data): print(f'Got block of h264: {len(data)}') s = Stream( input_type='usb', id=CAMERA_ID, output_type='h264_pipe', output_config={ 'callback': my_callback, 'block_size': BLOCK_SIZE, }, reading=True, writing=True, ) time.sleep(SLEEP_TIME) s.stop()