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

  1. 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
  1. The output should be similar as the one below:

Image Example

Image Example Detected

street-people

street-people_detected

Image Detection Example Source code: image_detection_tflite.py
 1# Copyright 2021 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from pyvar.ml.engines.tflite import TFLiteInterpreter
 5from pyvar.ml.utils.label import Label
 6from pyvar.ml.utils.overlay import Overlay
 7from pyvar.ml.utils.resizer import Resizer
 8from pyvar.ml.utils.retriever import FTP
 9from pyvar.multimedia.helper import Multimedia
10
11ftp = FTP()
12
13if ftp.retrieve_package(category="detection"):
14    model_file_path = ftp.model
15    label_file_path = ftp.label
16    image_file_path = ftp.image
17
18labels = Label(label_file_path)
19labels.read_labels("detection")
20
21engine = TFLiteInterpreter(model_file_path)
22
23resizer = Resizer()
24resizer.set_sizes(engine_input_details=engine.input_details)
25
26image = Multimedia(image_file_path)
27resizer.resize_image(image.video_src)
28
29engine.set_input(resizer.image_resized)
30engine.run_inference()
31engine.get_result("detection")
32
33draw = Overlay()
34
35output_image = draw.info(category="detection",
36                         image=resizer.image,
37                         top_result=engine.result,
38                         labels=labels.list,
39                         inference_time=engine.inference_time,
40                         model_name=model_file_path,
41                         source_file=resizer.image_path)
42
43image.show_image("TFLite: Image Detection", output_image)

Note

You can try the same example using Arm NN as inference engine image_detection_armnn.py.




Video Detection

Run the Video Detection Example

  1. 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
  1. The output should be similar as the one below:

Video Example

Video Example Detected

street

street_detected

Video Detection Example Source code: video_detection_tflite.py
 1# Copyright 2021 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from pyvar.ml.engines.tflite import TFLiteInterpreter
 5from pyvar.ml.utils.label import Label
 6from pyvar.ml.utils.overlay import Overlay
 7from pyvar.ml.utils.resizer import Resizer
 8from pyvar.ml.utils.retriever import FTP
 9from pyvar.multimedia.helper import Multimedia
10
11ftp = FTP()
12
13if ftp.retrieve_package(category="detection"):
14    model_file_path = ftp.model
15    label_file_path = ftp.label
16    video_file_path = ftp.video
17
18labels = Label(label_file_path)
19labels.read_labels("detection")
20
21engine = TFLiteInterpreter(model_file_path)
22
23resizer = Resizer()
24resizer.set_sizes(engine_input_details=engine.input_details)
25
26video = Multimedia(video_file_path)
27video.set_v4l2_config()
28
29draw = Overlay()
30
31while video.loop:
32    frame = video.get_frame()
33    resizer.resize_frame(frame)
34
35    engine.set_input(resizer.frame_resized)
36    engine.run_inference()
37    engine.get_result("detection")
38
39    output_frame = draw.info(category="detection",
40                             image=resizer.frame,
41                             top_result=engine.result,
42                             labels=labels.list,
43                             inference_time=engine.inference_time,
44                             model_name=model_file_path,
45                             source_file=video.video_src)
46
47    video.show("TFLite: Video Detection", output_frame)
48
49video.destroy()

Note

You can try the same example using Arm NN as inference engine video_detection_armnn.py.




Real Time Detection

Run the Real Time Detection Example

  1. 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
Real Time Detection Example Source code: realtime_detection_tflite.py
 1# Copyright 2021 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from pyvar.ml.engines.tflite import TFLiteInterpreter
 5from pyvar.ml.utils.framerate import Framerate
 6from pyvar.ml.utils.label import Label
 7from pyvar.ml.utils.overlay import Overlay
 8from pyvar.ml.utils.resizer import Resizer
 9from pyvar.ml.utils.retriever import FTP
10from pyvar.multimedia.helper import Multimedia
11
12ftp = FTP()
13
14if ftp.retrieve_package(category="detection"):
15    model_file_path = ftp.model
16    label_file_path = ftp.label
17
18labels = Label(label_file_path)
19labels.read_labels("detection")
20
21engine = TFLiteInterpreter(model_file_path)
22
23resizer = Resizer()
24resizer.set_sizes(engine_input_details=engine.input_details)
25
26camera = Multimedia("/dev/video1", resolution="vga")
27camera.set_v4l2_config()
28
29framerate = Framerate()
30
31draw = Overlay()
32draw.framerate_info = True
33
34while camera.loop:
35    with framerate.fpsit():
36        frame = camera.get_frame()
37        resizer.resize_frame(frame)
38
39        engine.set_input(resizer.frame_resized)
40        engine.run_inference()
41        engine.get_result("detection")
42
43        output_frame = draw.info(category="detection",
44                                 image=resizer.frame,
45                                 top_result=engine.result,
46                                 labels=labels.list,
47                                 inference_time=engine.inference_time,
48                                 model_name=model_file_path,
49                                 source_file=camera.dev.name,
50                                 fps=framerate.fps)
51
52        camera.show("TFLite: Real Time Detection", output_frame)
53
54camera.destroy()

Note

You can try the same example using Arm NN as inference engine realtime_detection_armnn.py.