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 argparse import ArgumentParser
 5
 6from pyvar.ml.engines.tflite import TFLiteInterpreter
 7from pyvar.ml.utils.label import Label
 8from pyvar.ml.utils.overlay import Overlay
 9from pyvar.ml.utils.resizer import Resizer
10from pyvar.ml.utils.retriever import FTP
11from pyvar.multimedia.helper import Multimedia
12
13ftp = FTP()
14parser = ArgumentParser()
15parser.add_argument('--num_threads', type=int)
16args = parser.parse_args()
17
18if ftp.retrieve_package(category="detection"):
19    model_file_path = ftp.model
20    label_file_path = ftp.label
21    image_file_path = ftp.image
22
23labels = Label(label_file_path)
24labels.read_labels("detection")
25
26engine = TFLiteInterpreter(model_file_path=model_file_path,
27                           num_threads=args.num_threads)
28
29resizer = Resizer()
30resizer.set_sizes(engine_input_details=engine.input_details)
31
32image = Multimedia(image_file_path)
33resizer.resize_image(image.video_src)
34
35engine.set_input(resizer.image_resized)
36engine.run_inference()
37engine.get_result("detection")
38
39draw = Overlay()
40
41output_image = draw.info(category="detection",
42                         image=resizer.image,
43                         top_result=engine.result,
44                         labels=labels.list,
45                         inference_time=engine.inference_time,
46                         model_name=model_file_path,
47                         source_file=resizer.image_path)
48
49image.show_image("TFLite: Image Detection", output_image)



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 argparse import ArgumentParser
 5
 6from pyvar.ml.engines.tflite import TFLiteInterpreter
 7from pyvar.ml.utils.label import Label
 8from pyvar.ml.utils.overlay import Overlay
 9from pyvar.ml.utils.resizer import Resizer
10from pyvar.ml.utils.retriever import FTP
11from pyvar.multimedia.helper import Multimedia
12
13ftp = FTP()
14parser = ArgumentParser()
15parser.add_argument('--num_threads', type=int)
16args = parser.parse_args()
17
18if ftp.retrieve_package(category="detection"):
19    model_file_path = ftp.model
20    label_file_path = ftp.label
21    video_file_path = ftp.video
22
23labels = Label(label_file_path)
24labels.read_labels("detection")
25
26engine = TFLiteInterpreter(model_file_path=model_file_path,
27                           num_threads=args.num_threads)
28
29resizer = Resizer()
30resizer.set_sizes(engine_input_details=engine.input_details)
31
32video = Multimedia(video_file_path)
33video.set_v4l2_config()
34
35draw = Overlay()
36
37while video.loop:
38    frame = video.get_frame()
39    resizer.resize_frame(frame)
40
41    engine.set_input(resizer.frame_resized)
42    engine.run_inference()
43    engine.get_result("detection")
44
45    output_frame = draw.info(category="detection",
46                             image=resizer.frame,
47                             top_result=engine.result,
48                             labels=labels.list,
49                             inference_time=engine.inference_time,
50                             model_name=model_file_path,
51                             source_file=video.video_src)
52
53    video.show("TFLite: Video Detection", output_frame)
54
55video.destroy()



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 argparse import ArgumentParser
 5
 6from pyvar.ml.engines.tflite import TFLiteInterpreter
 7from pyvar.ml.utils.framerate import Framerate
 8from pyvar.ml.utils.label import Label
 9from pyvar.ml.utils.overlay import Overlay
10from pyvar.ml.utils.resizer import Resizer
11from pyvar.ml.utils.retriever import FTP
12from pyvar.multimedia.helper import Multimedia
13
14ftp = FTP()
15parser = ArgumentParser()
16parser.add_argument('--num_threads', type=int)
17args = parser.parse_args()
18
19if ftp.retrieve_package(category="detection"):
20    model_file_path = ftp.model
21    label_file_path = ftp.label
22
23labels = Label(label_file_path)
24labels.read_labels("detection")
25
26engine = TFLiteInterpreter(model_file_path=model_file_path,
27                           num_threads=args.num_threads)
28
29resizer = Resizer()
30resizer.set_sizes(engine_input_details=engine.input_details)
31
32camera = Multimedia("/dev/video1", resolution="vga")
33camera.set_v4l2_config()
34
35framerate = Framerate()
36
37draw = Overlay()
38draw.framerate_info = True
39
40while camera.loop:
41    with framerate.fpsit():
42        frame = camera.get_frame()
43        resizer.resize_frame(frame)
44
45        engine.set_input(resizer.frame_resized)
46        engine.run_inference()
47        engine.get_result("detection")
48
49        output_frame = draw.info(category="detection",
50                                 image=resizer.frame,
51                                 top_result=engine.result,
52                                 labels=labels.list,
53                                 inference_time=engine.inference_time,
54                                 model_name=model_file_path,
55                                 source_file=camera.dev.name,
56                                 fps=framerate.fps)
57
58        camera.show("TFLite: Real Time Detection", output_frame)
59
60camera.destroy()