File size: 1,744 Bytes
e0ee8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# Copyright 2024-2026 NXP
#
# SPDX-License-Identifier: BSD-3-Clause
#
import cv2
import argparse
from dperson import dperson


def parse_args():
    parser = argparse.ArgumentParser(
        description='FastestDet person detection example',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument(
        '-m', '--model',
        dest='model',
        required=False,
        type=str,
        default='assets/quantized_tflite_model_mcu/persondetect_220_220.tflite',
        help='path to the .tflite model'
    )
    parser.add_argument(
        '-i', '--input',
        dest='input',
        required=False,
        type=str,
        default='example_input.jpg',
        help='path to the input image'
    )
    parser.add_argument(
        '-o', '--output',
        dest='output',
        required=False,
        type=str,
        default='example_output.jpg',
        help='path to write the annotated output image'
    )
    parser.add_argument(
        '--thresh',
        dest='thresh',
        required=False,
        type=float,
        default=0.6,
        help='detection score threshold'
    )
    return parser.parse_args()


if __name__ == '__main__':
    args = parse_args()

    # instantiate detector model
    detector = dperson(args.model)

    # run inference on the input image
    img = cv2.imread(args.input)
    if img is None:
        raise FileNotFoundError(f"could not read input image: {args.input}")
    bboxes = detector.detect(img, args.thresh)
    print(f"detected {len(bboxes)} person(s)")

    # draw bounding boxes and save the result
    detector.show(img)
    cv2.imwrite(args.output, img)
    print(f"annotated image written to {args.output}")