You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于TensorFlow Lite的人员检测报警功能实现遇迭代错误求助

解决TensorFlow Lite DetectionResult不可迭代错误

问题根源

你遇到的TypeError: 'DetectionResult' object is not iterable错误,是因为直接尝试遍历DetectionResult对象导致的。TFLite Support Library返回的DetectionResult本身不是可迭代序列,它的检测结果列表存储在**detections属性**中,必须通过这个属性访问具体的检测目标。

核心修复

修改代码中遍历检测结果的部分:

错误代码

# Check if the specific label is detected
alarm_triggered = False
for obj in detection_result:
    if obj.class_name == alarm_label:
        alarm_triggered = True
        # Trigger the alarm sound
        alarm_sound.play()

修复后代码

# Check if the specific label is detected
alarm_triggered = False
for obj in detection_result.detections:
    if obj.class_name == alarm_label:
        alarm_triggered = True
        # Trigger the alarm sound
        alarm_sound.play()

额外优化(可选)

为了避免每帧检测到人员时重复播放音频(导致声音重叠),可以添加状态变量控制播放逻辑:

# 在初始化pygame mixer后添加
is_alarm_playing = False

# 遍历检测结果时调整逻辑
alarm_triggered = False
current_detected = False
for obj in detection_result.detections:
    if obj.class_name == alarm_label:
        current_detected = True
        alarm_triggered = True

# 控制音频播放
if current_detected and not is_alarm_playing:
    alarm_sound.play()
    is_alarm_playing = True
elif not current_detected and is_alarm_playing:
    is_alarm_playing = False

验证修复

修改后重新运行代码,检测到人员时会正常触发红色边框和音频提示,不会再出现迭代错误。

内容的提问来源于stack exchange,提问作者Kyle Greenwood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:43:24