File size: 4,744 Bytes
ea3cff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# Copyright 2024,2026 NXP
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

import argparse
import random
import time

import cv2
import numpy as np
import tensorflow as tf

random.seed(42)

LABELS_FILE = 'coco_labels_list.txt'
INFERENCE_IMG_SIZE = 320
SCORE_THRESHOLD = 0.3

with open(LABELS_FILE, 'r') as f:
    COCO_CLASSES = [line.strip() for line in f.readlines()]


def gen_box_colors():
    colors = []
    for _ in range(len(COCO_CLASSES)):
        r = random.randint(100, 255)
        g = random.randint(100, 255)
        b = random.randint(100, 255)
        colors.append((r, g, b))
    return colors


BOX_COLORS = gen_box_colors()


def load_image(filename):
    orig_image = cv2.imread(filename, 1)
    image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (INFERENCE_IMG_SIZE, INFERENCE_IMG_SIZE))
    image = np.expand_dims(image, axis=0)
    return orig_image, image


def run_inference(interpreter, image):
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # Handle quantized input: the pre-quantized model expects uint8 [0, 255]
    # pixels, but a re-quantized int8 variant would need a shift.
    input_dtype = input_details[0]['dtype']
    if input_dtype == np.uint8:
        image = image.astype(np.uint8)
    elif input_dtype == np.int8:
        scale, zero_point = input_details[0]['quantization']
        if scale != 0:
            image = image.astype(np.float32) / scale + zero_point
        else:
            image = image.astype(np.float32) - 128
        image = image.astype(np.int8)
    else:
        image = image.astype(np.float32)

    interpreter.set_tensor(input_details[0]['index'], image)
    interpreter.invoke()

    # EfficientDet-lite0 keeps NMS in-graph and returns four float32 tensors:
    #   boxes (1, 25, 4), classes (1, 25), scores (1, 25), count (1,).
    # Sort them by shape so ordering is robust across interpreters.
    outputs = sorted(output_details, key=lambda d: -len(d['shape']))
    boxes = interpreter.get_tensor(outputs[0]['index'])
    classes = interpreter.get_tensor(outputs[1]['index'])
    scores = interpreter.get_tensor(outputs[2]['index'])

    return boxes, scores, classes


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="EfficientDet-lite0 object detection inference")
    parser.add_argument("-m", "--model",
                        default="assets/quantized_tflite_model/efficientdet-lite0_quant_int8.tflite",
                        help="Path to the TFLite model file")
    parser.add_argument("-i", "--input",
                        default="example_input.jpg",
                        help="Path to the input image")
    parser.add_argument("-o", "--output",
                        default="example_output.jpg",
                        help="Path to save the output image")
    args = parser.parse_args()

    interpreter = tf.lite.Interpreter(args.model)
    interpreter.allocate_tensors()

    orig_image, processed_image = load_image(args.input)

    start = time.time()
    boxes, scores, classes = run_inference(interpreter, processed_image)
    end = time.time()

    shp = orig_image.shape

    # Boxes are normalized [ymin, xmin, ymax, xmax]; rescale to image size.
    boxes = boxes * np.array([shp[0], shp[1], shp[0], shp[1]])
    boxes = boxes.astype(np.int32)
    boxes = np.squeeze(boxes, axis=0)
    classes = np.squeeze(classes, axis=0)
    scores = np.squeeze(scores, axis=0)

    print("Inference time", end - start, "s")

    for i in range(boxes.shape[0]):
        score = scores[i]
        if score < SCORE_THRESHOLD:
            continue
        box = boxes[i]

        cls = int(classes[i])
        class_name = COCO_CLASSES[cls]
        color = BOX_COLORS[cls]
        print(box, "class", class_name, "score", score)

        cv2.rectangle(orig_image, (int(box[1]), int(box[0])),
                      (int(box[3]), int(box[2])), color, 3)
        cv2.putText(orig_image, f"{class_name} {score:.2f}",
                    (int(box[1]), int(box[0]) - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)

    cv2.imwrite(args.output, orig_image)
    print(f"Output saved to {args.output}")