Classification Examples

The classification examples use a quantized starter model from TensorFlow Lite:

  • mobilenet_v1_1.0_224_quant.tgz;

  • mobilenet_v2_1.0_224_quant.tgz.

Image Classification

Run the Image Classification Example

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/image_classification_tflite.py
python3 image_classification_tflite.py
  1. The output should be similar as the one below:

Image Example

Image Example Classified

car

car-converted

Image Classification Example Source Code: image_classification_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="classification"):
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("classification")
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("classification")
38
39draw = Overlay()
40
41output_image = draw.info(category="classification",
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 Classification", output_image)



Video Classification

Run the Video Classification Example

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/video_classification_tflite.py
python3 video_classification_tflite.py
  1. The output should be similar as the one below:

Video Example

Video Example Classified

street

street-classified

Video Classification Example Source code: video_classification_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="classification"):
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("classification")
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("classification")
44
45    output_frame = draw.info(category="classification",
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 Classification", output_frame)
54
55video.destroy()



Real Time Classification

Run the Real Time Classification Example

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/realtime_classification_tflite.py
python3 realtime_classification_tflite.py
Real Time Classification Example Source code: realtime_classification_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="classification"):
20    model_file_path = ftp.model
21    label_file_path = ftp.label
22
23labels = Label(label_file_path)
24labels.read_labels("classification")
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("classification")
48
49        output_frame = draw.info(category="classification",
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 Classification", output_frame)
59
60camera.destroy()