sensors.Utilities

Contains classes and functions to handle the peripherical devices and the the dataflow between devices and the Bokeh application.

Class InitDevices

class sensors.Utilities.initdevices.InitDevices(config)[source]

Bases: object

Initializes the audio device, the PiCamera and checks for connected light barriers and their corresponding GPIO pins.

get_GPIO_IR_pins()[source]

Get information about the GPIO pins with connected IR light barriers and sets GPIO switch for LED flash light to low.

Returns:

list

Return type:

integers

static get_as7341_sensor()[source]

Initialize AS7341 10-channel spectral color sensor if connected.

Returns:

sensor object if connected else None

Return type:

object, None

static get_audio_devices()[source]

Get speaker and microphone devices from the USB dongle.

Returns:

soundcard objects if connected else None

Return type:

object, None

static get_bh1750_sensor()[source]

Initialize bh1750 sensor for ambient light if connected.

Returns:

sensor object if connected else None

Return type:

object, None

static get_bme280_sensor()[source]

Initialize bme280 sensor for relative humidity, barometric pressure and ambient temperature if connected.

Returns:

sensor objects else None

Return type:

object, None

static get_camera()[source]

Initialize PiCamera if connected.

Returns:

picamera object if connected else None

Return type:

object, None

static get_pmsa003i_sensor()[source]

Initialize PMSA003I particulate matter (air quality) sensor if connected.

Returns:

sensor object if connected else None

Return type:

object, None

instance = None

Class PyAudioHandler

class sensors.Utilities.pyaudiohandler.PyAudioHandler(visual_trigger, visual_ringbuffer)[source]

Bases: Thread

Class to handle USB dongle input by reading chunks of mono data from the wingbeat device and sending data to the Bokeh application and saving it.

Parameters:
kill()[source]

Ends the audio thread.

py_error_handler(filename, line, function, err, fmt)[source]

Function to mute ALSA warnings on creation of pyaudio object.

save_all()[source]

Method to save the ringbuffer to a wav file with the corresponding time stamp.

save_trig_signal()[source]

Method to save a triggered signal to a wav file with the corresponding time stamp.

Returns:

triggered raw signal, filename

Return type:

1D float array, string

start()[source]

Starts the audio thread.

update_audio_data()[source]

Main thread function that gets initialized with the thread generation PyAudioHandler.start().

Class RingBuffer

class sensors.Utilities.numpy_ringbuffer.RingBuffer(length)[source]

Bases: object

Creates a 1D ring buffer object using numpy arrays.

Parameters:

length (int) – length of ring buffer

extend(array)[source]

Writes the array into the ring buffer.

Parameters:

array (1D float array) – added array

get()[source]

Returns the ring buffer.

Returns:

ring buffer

Return type:

1D float array

Class FrameDecoder

class sensors.Utilities.frame_decoder.FrameDecoder(config, video_path, data_json)[source]

Bases: object

Class to decode frames from h264 video.

Parameters:
  • config (object) – configuration including data.json template

  • video_path (string) – folder containing video file

  • data_json (dict) – json dictionary with import/output-format for KInsecta web app

convert_to_mp4()[source]

Convert h264 to mp4 with GPAC and get number of frames from its stdout.

create_filename()[source]

Creates UUID filename for frame and write this information to data.json.

frames()[source]

Loops over all the video frames and computes the mean pixel values and the difference to the mean of the preceded frame in each loop. The root mean square (rms) of the mean differences gets updated in each loop and if a frame’s mean difference is five times higher than the current rms, a sequence of frames is saved to disc. If no such trigger event occurs, a sequence of frames from the middle of the video gets saved.

get_video_filename()[source]

Returns the filename of the video.

Returns:

filename

Return type:

string

more()[source]

Check if frame queue is not empty or if the decoding process is not stopped yet.

Returns:

flag to decode next frame

Return type:

boolean

start()[source]

Start decoding thread to fill queue with frames.

stop()[source]

Stop the decoding thread and insert None as last queue item. Set corresponding flag.

update()[source]

Main decoding thread, that fills queue with decoded frames. Every second frame the faster grab method is used instead of decoding a frame.

static write_frame(frame, UUID_filename)[source]

Saves the frame with UUID filename.

Parameters:
  • frame (numpy array) – frame to save to disc

  • UUID_filename (string) – filename of frame

Class WriteMetaData

class sensors.Utilities.write_meta_data.WriteMetaData(config, data_json)[source]

Bases: object

Class to write meta data to data.json.

Parameters:
  • config (object) – configuration including sensor objects

  • data_json (dictionary) – json dictionary with import/output-format for KInsecta web app

prepare_data_json_upload()[source]

Method deletes keys and child properties that have null values from the data.json. Does not apply to all keys, some may have null values which won’t violate the import format of the KInsecta Web App.

static random_number(value_min, value_max, formatting)[source]
write_all_data()[source]

Method that calls all methods to write sensor measurements to data.json and prepares it for upload.

write_humidity_temperature_air_pressure()[source]

Writes humidity, temperature and air pressure measured with BME280 sensor to data.json.

write_luminosity()[source]

Writes luminosity measured with BH1750 sensor to data.json.

write_particulate_matter()[source]

Writes air quality and particle sizes measured with PMSA003I sensor to data.json.

write_rainfall()[source]
write_size_insect()[source]
write_spectrum()[source]

Writes spectral distribution measured with AS7441 sensor to data.json.

write_wind_sensor()[source]
sensors.Utilities.write_meta_data.random() x in the interval [0, 1).

Function Create png from signal

sensors.Utilities.signal_to_png.create_png_from_signal(signal_file, png_filename, decimation_factor)[source]

Function that saves a png file of the input audio signal and its PSD to the specified file name.

Parameters:
  • signal_file (string) – absolute path to signal file

  • png_filename (string) – file name of created png

  • decimation_factor (int) – downsampling factor

Returns:

False flag for png save in progress (computation ended)

Return type:

bool