Skip to content

Instantly share code, notes, and snippets.

@bml1g12
bml1g12 / foo
Last active February 17, 2020 06:04
TestingOveride
def _create_config():
return {"foo_key":0}
def create_foo_thing():
config = _create_config()
return config
@bml1g12
bml1g12 / 0_prepare_random_frame.py
Last active January 1, 2021 09:08
Example consumer
def prepare_frame(np_arr_shape, frames_written):
"""Emulate an I/O limited step that returns a frame,
e.g. reading from a quick-to-decode file. Note that we could instead simulate
a CPU intensive task here and the resulting benchmark runs much faster
when using multiprocessing than multithreading.
:param Tuple[int, int] np_arr_shape: Dimension of the numpy array to be produced.
:param int frames_written:
"""
frame = np.ones(np_arr_shape) * frames_written
@bml1g12
bml1g12 / 1_worker_producer_shared_memory.py
Last active March 9, 2021 18:59
Shared memory for communicating Numpy arrays
def worker_producer_shared_memory(np_arr_shape, shared_memory, n_frames):
"""A frame producer function that writes to shared memory"""
mp_array, np_array = shared_memory
for _ in range(n_frames):
mp_array.acquire()
np_array[:] = prepare_random_frame(np_arr_shape) # produce a fresh array
@bml1g12
bml1g12 / baseline.py
Last active December 28, 2020 15:20
A baseline consumer
for _ in range(n_frames):
# produce the frame
np_arr = prepare_random_frame(np_arr_shape)
# consume the frame and do some example processing
_ = np_arr.astype("uint8").copy() * 2
@bml1g12
bml1g12 / seekToOpenCVReader.py
Created January 3, 2021 12:04
Alternative to cv2.VideoCapture's set method for reliable frame seeking
def seekTo(cap, position):
'''
Work around bug in OpenCV set method: https://github.com/opencv/opencv/issues/9053
The alternative routine to seek in the video file, a bit slow.
Required because FFMPEG can seek only to closest I-frames and we
have to manually read all the P-frames until we reach the position
'''
positiontoset = position
pos = -1
@bml1g12
bml1g12 / typical_video_reading.py
Created March 28, 2021 03:44
Typical video reading script in Python using OpenCV
import cv2
cap = cv2.VideoCapture("myvideo.mkv")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
@bml1g12
bml1g12 / test_greengrass_iot_certificate.sh
Last active January 31, 2023 17:14
A bash script to test if AWS IoT Device Certificate is working as expected on a Greengrass v2 device
#!/usr/bin/env bash
set -Eeuxo pipefail
# If running this script on a Greengrass v2 device, it should return a dictionary containing temporary credentials.
# If it fails, check if the THING_NAME, ROLE_ALIAS and IOT_GET_CREDENTIAL_ENDPOINT are correct
# IOT_GET_CREDENTIAL_ENDPOINT can be verified via running ` aws iot describe-endpoint --endpoint-type iot:CredentialProvider --output text`
# on a different device with sufficent IAM to run this command, in the same AWS account and region.
THING_NAME=`sudo cat /greengrass/v2/config/effectiveConfig.yaml | grep -i thingName | awk '{ print $2 }' | tr -d '"'`
ROLE_ALIAS=`sudo cat /greengrass/v2/config/effectiveConfig.yaml | grep -i rolealias | awk '{ print $2 }' | tr -d '"'`
IOT_GET_CREDENTIAL_ENDPOINT=`sudo cat /greengrass/v2/config/effectiveConfig.yaml | grep iotCredEndpoint | awk '{print $2}' | tr -d '"'`