def stream_frame(frame): if frame.ndim == 2: height, width = frame.shape channels = 1 elif frame.ndim == 3: height, width, channels = frame.shape else: return # :( shape = [width, height, channels] rect = [0, 0, width // 3, height // 3] console.stream_image(rect, shape, frame.tobytes())
def stream(frame, to_console=True, to_labs=False, verbose=False): """ Stream the given `frame` (a numpy ndarray) to your device's console _and_ (optionally) to your `labs` account to be shown in your browser. The `frame` parameter must be a numpy ndarray with one of the following shapes: - (h, w, 3) meaning a single 3-channel RGB image of size `w`x`h` - (h, w, 1) meaning a single 1-channel gray image of size `w`x`h` - (h, w) meaning a single 1-channel gray image of size `w`x`h` """ if frame is None: if to_console: console.clear_image() if to_labs: send_message_to_labs({'base64_img': ''}) return # Publish the uncompressed frame to the console UI. if to_console: if frame.ndim == 3: if frame.shape[2] == 3: pass # all good elif frame.shape[2] == 1: pass # all good else: raise Exception("invalid number of channels") elif frame.ndim == 2: frame = np.expand_dims(frame, axis=2) assert frame.ndim == 3 and frame.shape[2] == 1 else: raise Exception(f"invalid frame ndarray ndim: {frame.ndim}") height, width, channels = frame.shape aspect_ratio = width / height if aspect_ratio != OPTIMAL_ASPECT_RATIO: final_frame = _add_white_bars(frame) height, width, channels = final_frame.shape else: final_frame = frame shape = [width, height, channels] rect = [0, 0, 0, 0] console.stream_image(rect, shape, final_frame.tobytes()) # Convert the frame to a JPG buffer and publish to the network connection. if to_labs: base64_img = base64_encode_image(frame) send_message_to_labs({'base64_img': base64_img}) if verbose: h, w = frame.shape[:2] print_all("Streamed frame of size {}x{}.".format(w, h))