## 3 Approach
### Programming Interface
#### 3.4.1 Blocks  
A Block is a fundamental component of the REIP SDK, and is implemented as a Python class that represents one unit of computation (e.g., get audio from the microphone, compute machine learning outputs, upload data to server etc.). Each Block runs in its own independent thread and uses Queues to pass data to others. Blocks are designed to be easily extendable to suit diverse use cases. A Block consists of: an initialisation function \(init()\) that is called at the start and which can be used to acquire resources and set initial values; a process function \(process(\dots)\) that is called repeatedly with data from parent blocks as an input and returns 0 or more outputs to the next block(s); and a cleanup function \(finish()\) that is called at the end to release any resources acquired. This general program structure encapsulates a wide family of programs and is fundamental in Object Oriented Programming and Python context managers. An example block implementation is shown on Figure 2a. Note that for a custom block, any of these functions can be omitted if not used. Blocks can operate in four approximate roles describing how they relate to the data that they are handling (definitions are not binding):  
* Data source (0 inputs, \(\geq\)1 outputs, e.g., sensing device such as a microphone);
* Data processing (\(\geq\)1 inputs, \(\geq\)1 outputs, e.g., object detection in an image);
* Data sink (\(\geq\)1 inputs, 0 outputs, e.g., data storage to disk);
* Operational (0 inputs, 0 outputs, e.g., disk usage monitoring). Pipelines can be constructed by connecting a data source block to any number of data processing and/or sink blocks. Operational blocks are typically considered as standaloneblocks that perform operations without needing to communicate with any other blocks, e.g., a block that monitors and maintains the network connectivity or available disk space. Users are also able to customise the rules around when a block will execute. For example, a user can customise the strategy used to determine when the framework should call the process function based on the input queue status. So one can change whether a block needs to wait for all inputs to have a value or if it should be executed when at least one of them has a value. A user can also configure the max rate at which a block will run, or the strategy used to get items from the queue, e.g., should a block process the latest value in the queue only or process every buffer. ```
importreip importrandom fromtensorflow.keras.modelsimportload_model classObjectDetection(reip.Block): definit(self): #Initializethemodel self.model=load_model('path/to/model.h5') defprocess(self,*frames,meta): #Ifwehavemultiplecameras,pickone i=random.randrange(0,len(frames)) frame=frames[i] #Preprocessingimageformodel frame=frame/255.0*2-1 #Callthekerasmodelontheimage predictions=self.model.predict(frame) #Returnthemodelpredictions returnpredictions,{'frame_index':i} deffinish(self): #Freemodeltobegarbagecollected self.model=None importreip.blocksasB
#Graphdefinition withreip.Graph()asgraph: #Readvideofromtwocameras withreip.Task('cam0'): cam0=B.video.Camera(device=0) withreip.Task('cam1'): cam1=B.video.Camera(device=1)
#Performobjectdetectionandwritetofile objects=ObjectDetection( cam0,cam1,throughput=\"large\") B.JSONWriter('objects/(time).json')(objects) #Uploadfilestoaserverendpoint B.upload.UploadFiles( '[[https://myserver.com/api/upload](https://myserver.com/api/upload)]([https://myserver.com/api/upload](https://myserver.com/api/upload))', 'objects/*') #Graphexecution graph.run()
```  
In its current state, the REIP SDK offers dozens of blocks covering audio tasks (recording, SPL computation, etc. ), video tasks (recording, pose, object or motion detection), data output, data encryption, data upload and general utilities. They are organised into corresponding block libraries that serve two main purposes. The first is to speed up the development of sensing applications by means of reusing pre-existing blocks for common tasks.