tf_object_detection.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2024 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# TensorFlow Lite 物体检测示例
#
# 此示例使用内置的FOMO模型检测人脸。

import csi
import time
import ml
from ml.postprocessing.edgeimpulse import Fomo
import math
csi0 = csi.CSI()
csi0.reset()  # 重置并初始化传感器。
csi0.pixformat(csi.RGB565)  # 将像素格式设置为RGB565 (or GRAYSCALE)
csi0.framesize(csi.QVGA)  # 将帧大小设置为QVGA (320x240)
csi0.window((240, 240))  # 设置240x240窗口。
csi0.snapshot(time=2000)  # 让相机进行自动调整。

# 加载内置的FOMO人脸检测模型
model = ml.Model("/rom/fomo_face_detection.tflite", postprocess=Fomo(threshold=0.4))
print(model)

# 或者,也可以从文件系统存储中加载模型。
# model = ml.Model('<object_detection_modelwork>.tflite', load_to_fb=True)
# labels = [line.rstrip('\n') for line in open("labels.txt")]

colors = [  # 如果一次检测超过7类目标,请添加更多颜色。
    (255, 0, 0),
    (0, 255, 0),
    (255, 255, 0),
    (0, 0, 255),
    (255, 0, 255),
    (0, 255, 255),
    (255, 255, 255),
]

clock = time.clock()
while True:
    clock.tick()
    img = csi0.snapshot()

    for i, detection_list in enumerate(model.predict([img])):
        if i == 0:
            continue  # 背景类别
        if len(detection_list) == 0:
            continue  # 该类别没有检测结果?

        print("********** %s **********" % model.labels[i])
        for (x, y, w, h), score in detection_list:
            center_x = math.floor(x + (w / 2))
            center_y = math.floor(y + (h / 2))
            print(f"x {center_x}\ty {center_y}\tscore {score}")
            img.draw_circle((center_x, center_y, 12), color=colors[i])

    print(clock.fps(), "fps", end="\n")

results matching ""

    No results matching ""