-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleObjectDetection.lf
59 lines (52 loc) · 2.06 KB
/
SimpleObjectDetection.lf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @file SimpleObjectDetection.lf
* @author Vincenzo Barbuto
* @brief Examples of how to use the Object Detection reactor.
*/
target Python
import ObjectDetector from "lib/ComputerVision.lf"
/**
* @brief Defines an ImageInjector reactor that reads an image from a file path and outputs the
* image data. The image path is specified as a parameter to the ImageInjector reactor.
*
* The Actuator reactor takes the detection results and inference time as inputs, and prints the
* results. It prints the detection results along with their detection box, indices, class name and,
* confidence scores. It also prints the time taken for each inference in milliseconds.
*
* The main reactor wires up the `ImageInjector`, `ObjectDetector`, and `Actuator` reactors
* together. The ImageInjector reactor outputs the image data to the ObjectDetector reactor. The
* ObjectDetector reactor outputs the detection results and inference time to the Actuator reactor.
*
* @note Remember to set the `model` parameter to the absolute path of the obect detection model you
* want to use, as well as the `img_path` parameter to the absolute path of the image file you want
* to use.
*/
reactor ImageInjector(img_path="") {
output image_data
preamble {=
import cv2
=}
reaction(startup) -> image_data {=
img = self.cv2.imread(self.img_path)
image_data.set(img)
=}
}
reactor Actuator {
input res
input inference_time
reaction(res, inference_time) {=
results = res.value
print("-"*70)
for i, result in enumerate(results):
print(f"{i}) Box: {result['box']}; Index: {result['index']}; Class: {result['label']}; Confidence: {result['score']*100:.2f}%")
print(f"Time per inference: {inference_time.value} ms")
=}
}
main reactor {
inj = new ImageInjector(img_path="/path/to/image.png")
obj = new ObjectDetector(
model = {= os.path.join(os.getcwd(),"models/vision/detection/ssd_mobilenet_v1.tflite") =})
actuator = new Actuator()
inj.image_data -> obj.input_data
obj.results, obj.inference_time -> actuator.res, actuator.inference_time
}