Example explanation 25-tf_object_detection FOMO person detection

This example uses the trained FOMO neural network model to detect people in the field of view and identify their faces. The neural network model is built into the firmware.

# TensorFlow Lite 目标点检测示例
#
# 本例程使用内置的FOMO模型检测人脸。

import sensor
import time
import tf
import math

sensor.reset()  # 重置并初始化感光元件
sensor.set_pixformat(sensor.RGB565)  # 设置图像格式为 RGB565 (或 GRAYSCALE)
sensor.set_framesize(sensor.QVGA)  # 设置图像大小为 QVGA (320x240)
sensor.set_windowing((240, 240))  # 设置图像为 240x240 窗口大小
sensor.skip_frames(time=2000)  # 跳过几帧使设置生效

min_confidence = 0.4

# 加载内置的 FOMO 人脸检测模型
labels, net = tf.load_builtin_model("fomo_face_detection")

# 或者,模型文件也可以从文件系统存储中加载。
# net = tf.load('<object_detection_network>', 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 = sensor.snapshot()

    # detect() 返回图像中检测到的所有的物体 (已经按照种类分类好)
    # 跳过索引0, 因为第0个分类是 背景 
    # 然后在识别到的物体中央画出圆圈

    for i, detection_list in enumerate(
        net.detect(img, thresholds=[(math.ceil(min_confidence * 255), 255)])
    ):
        if i == 0:
            continue  # 索引0是背景分类
        if len(detection_list) == 0:
            continue  # 没有检测到这个种类的物体

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

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

results matching ""

    No results matching ""