-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleImageClassification.lf
62 lines (55 loc) · 2.12 KB
/
SimpleImageClassification.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
60
61
62
/**
* @file SimpleImageClassification.lf
* @author Vincenzo Barbuto
* @brief Examples of how to use the Image Classification reactor.
*/
target Python
import ImageClassifier 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 image classification results and inference time as inputs, and
* prints the results. It prints the top classification results along with their labels, indices,
* and confidence scores. It also prints the time taken for each inference in milliseconds.
*
* The main reactor wires up the `ImageInjector`, `ImageClassifier`, and `Actuator` reactors
* together. The ImageInjector reactor outputs the image data to the ImageClassifier reactor. The
* ImageClassifier reactor outputs the classification results and inference time to the Actuator
* reactor.
*
* @note Remember to set the `model` parameter to the absolute path of the image classification
* 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}) Head: {result['head']}; 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")
cls = new ImageClassifier(
model = {=
os.path.join(os.getcwd(),"models/vision/classification/tf2_mobilenet_v3_edgetpu_1.0_224_ptq.tflite")
=})
actuator = new Actuator()
inj.image_data -> cls.input_data
cls.results, cls.inference_time -> actuator.res, actuator.inference_time
}