Detection Examples¶
The detection examples use a quantized starter model from TensorFlow Lite:
ssd_mobilenet_v1_1_default_1.zip.
Image Detection Example¶
Run the Image Detection Example¶
Retrieve the example, and execute it on the SoM:
curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/detection/image_detection_tflite.py
python3 image_detection_tflite.py
The output should be similar as the one below:
Image Example |
Image Example Detected |
---|---|
1# Copyright 2021 Variscite LTD
2# SPDX-License-Identifier: BSD-3-Clause
3
4"""
5This script performs object detection using the TFLiteInterpreter engine.
6
7It performs the following steps:
8
91. Retrieves the detection package using a HTTPS retriever instance.
102. Loads the labels from the label file.
113. Creates an TFLiteInterpreter engine instance and a resizer instance.
124. Resizes the input image to the engine's input size.
135. Runs inference and gets the detection result.
146. Creates an overlay instance and draws the output image with the
15 detection result and other information.
167. Shows the output image using the Multimedia helper.
17
18Example:
19
20To run this script:
21 $ python3 object_detection_tflite.py
22
23Args:
24None.
25
26Returns:
27None.
28"""
29
30from argparse import ArgumentParser
31
32from pyvar.ml.engines.tflite import TFLiteInterpreter
33from pyvar.ml.utils.label import Label
34from pyvar.ml.utils.overlay import Overlay
35from pyvar.ml.utils.resizer import Resizer
36from pyvar.ml.utils.retriever_https import HTTPS
37from pyvar.multimedia.helper import Multimedia
38
39https = HTTPS()
40parser = ArgumentParser()
41parser.add_argument('--num_threads', type=int)
42args = parser.parse_args()
43args.num_threads = 2
44
45if https.retrieve_package(category="detection"):
46 model_file_path = https.model
47 label_file_path = https.label
48 image_file_path = https.image
49
50labels = Label(label_file_path)
51labels.read_labels("detection")
52
53engine = TFLiteInterpreter(model_file_path=model_file_path,
54 num_threads=args.num_threads)
55
56resizer = Resizer()
57resizer.set_sizes(engine_input_details=engine.input_details)
58
59image = Multimedia(image_file_path)
60resizer.resize_image(image.video_src)
61
62engine.set_input(resizer.image_resized)
63engine.run_inference()
64engine.get_result("detection")
65
66draw = Overlay()
67
68output_image = draw.info(category="detection",
69 image=resizer.image,
70 top_result=engine.result,
71 labels=labels.list,
72 inference_time=engine.inference_time,
73 model_name=model_file_path,
74 source_file=resizer.image_path)
75
76image.show_image("TFLite: Image Detection", output_image)
Video Detection¶
Run the Video Detection Example¶
Retrieve the example, and execute it on the SoM:
curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/detection/video_detection_tflite.py
python3 video_detection_tflite.py
The output should be similar as the one below:
Video Example |
Video Example Detected |
---|---|
1# Copyright 2021 Variscite LTD
2# SPDX-License-Identifier: BSD-3-Clause
3
4"""
5This script performs object detection on a video stream using the
6TFLiteInterpreter engine.
7
8It performs the following steps:
9
101. Retrieves the detection package using a HTTPS retriever instance.
112. Loads the labels from the label file.
123. Creates an TFLiteInterpreter engine instance and a resizer instance.
134. Resizes each frame of the input video to the engine's input size.
145. Runs inference and gets the result for each frame.
156. Creates an overlay instance and draws the output image with the detection
16 result and other information for each frame.
177. Shows the output video using the Multimedia helper.
18
19Example:
20
21To run this script:
22 $ python3 video_detection_tflite.py
23
24Args:
25None.
26
27Returns:
28None.
29"""
30
31from argparse import ArgumentParser
32
33from pyvar.ml.engines.tflite import TFLiteInterpreter
34from pyvar.ml.utils.label import Label
35from pyvar.ml.utils.overlay import Overlay
36from pyvar.ml.utils.resizer import Resizer
37from pyvar.ml.utils.retriever_https import HTTPS
38from pyvar.multimedia.helper import Multimedia
39
40https = HTTPS()
41parser = ArgumentParser()
42parser.add_argument('--num_threads', type=int)
43args = parser.parse_args()
44args.num_threads = 2
45
46if https.retrieve_package(category="detection"):
47 model_file_path = https.model
48 label_file_path = https.label
49 video_file_path = https.video
50
51labels = Label(label_file_path)
52labels.read_labels("detection")
53
54engine = TFLiteInterpreter(model_file_path=model_file_path,
55 num_threads=args.num_threads)
56
57resizer = Resizer()
58resizer.set_sizes(engine_input_details=engine.input_details)
59
60video = Multimedia(video_file_path)
61video.set_v4l2_config()
62
63draw = Overlay()
64
65while video.loop:
66 frame = video.get_frame()
67 resizer.resize_frame(frame)
68
69 engine.set_input(resizer.frame_resized)
70 engine.run_inference()
71 engine.get_result("detection")
72
73 output_frame = draw.info(category="detection",
74 image=resizer.frame,
75 top_result=engine.result,
76 labels=labels.list,
77 inference_time=engine.inference_time,
78 model_name=model_file_path,
79 source_file=video.video_src)
80
81 video.show("TFLite: Video Detection", output_frame)
82
83video.destroy()
Real Time Detection¶
Run the Real Time Detection Example¶
Retrieve the example, and execute it on the SoM:
curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/detection/realtime_detection_tflite.py
python3 realtime_detection_tflite.py
1# Copyright 2021-2023 Variscite LTD
2# SPDX-License-Identifier: BSD-3-Clause
3
4"""
5This script performs real-time object detection using the TFLiteInterpreter
6engine.
7
8It performs the following steps:
9
101. Retrieves the detection package using a HTTPS retriever instance.
112. Loads the labels from the label file.
123. Creates an TFLiteInterpreter engine instance and a resizer instance.
134. Resizes each frame of the input video from a camera to the engine's input size.
145. Runs inference and gets the result for each frame.
156. Creates an overlay instance and draws the output image with the
16 detection result and other information for each frame.
177. Shows the output video with the detection results using the Multimedia helper.
18
19Example:
20
21To run this script:
22 $ python3 realtime_detection_tflite.py
23
24Args:
25None.
26
27Returns:
28None.
29"""
30
31from argparse import ArgumentParser
32
33from pyvar.ml.engines.tflite import TFLiteInterpreter
34from pyvar.ml.utils.framerate import Framerate
35from pyvar.ml.utils.label import Label
36from pyvar.ml.utils.overlay import Overlay
37from pyvar.ml.utils.resizer import Resizer
38from pyvar.ml.utils.retriever_https import HTTPS
39from pyvar.multimedia.helper import Multimedia
40
41https = HTTPS()
42parser = ArgumentParser()
43parser.add_argument('--num_threads', type=int)
44args = parser.parse_args()
45args.num_threads = 2
46
47if https.retrieve_package(category="detection"):
48 model_file_path = https.model
49 label_file_path = https.label
50
51labels = Label(label_file_path)
52labels.read_labels("detection")
53
54engine = TFLiteInterpreter(model_file_path=model_file_path,
55 num_threads=args.num_threads)
56
57resizer = Resizer()
58resizer.set_sizes(engine_input_details=engine.input_details)
59
60camera = Multimedia("/dev/video1", resolution="vga")
61camera.set_v4l2_config()
62
63framerate = Framerate()
64
65draw = Overlay()
66draw.framerate_info = True
67
68while camera.loop:
69 with framerate.fpsit():
70 frame = camera.get_frame()
71 resizer.resize_frame(frame)
72
73 engine.set_input(resizer.frame_resized)
74 engine.run_inference()
75 engine.get_result("detection")
76
77 output_frame = draw.info(category="detection",
78 image=resizer.frame,
79 top_result=engine.result,
80 labels=labels.list,
81 inference_time=engine.inference_time,
82 model_name=model_file_path,
83 source_file=camera.dev.name,
84 fps=framerate.fps)
85
86 camera.show("TFLite: Real Time Detection", output_frame)
87
88camera.destroy()