Example: 01-Camera/00-Snapshot/snapshot_on_movement.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 移动时拍摄示例
#
# 注意:运行此示例需要一张SD卡。
#
# 此示例演示了如何使用OpenMV摄像头进行帧差分
# 运动检测。检测到运动后,您的OpenMV摄像头将拍摄照片。

import sensor
import random
import os
import machine

sensor.reset()  # 重置并初始化传感器。
sensor.set_pixformat(sensor.RGB565)  # 将像素格式设置为RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)  # 将帧大小设置为QVGA (320x240)
sensor.skip_frames(time=2000)  # 等待设置生效。
sensor.set_auto_whitebal(False)  # 关闭白平衡。

led = machine.LED("LED_RED")

if not "temp" in os.listdir():
    os.mkdir("temp")  # 创建一个临时目录

while True:
    print("About to save background image...")
    sensor.skip_frames(time=2000)  # 给用户时间准备。

    sensor.snapshot().save("temp/bg.bmp")
    print("Saved background image - Now detecting motion!")

    diff = 10  # 我们会在10帧运动后检测到运动。
    while diff:
        img = sensor.snapshot()
        img.difference("temp/bg.bmp")
        stats = img.statistics()
        # Stats 5 是光照颜色通道的最大值。以下代码
        # 当整个图像的光照最大值超过20时触发。
        # 光照差异最大值通常应为零。
        if stats[5] > 20:
            diff -= 1

    led.on()
    print("Movement detected! Saving image...")
    sensor.snapshot().save("temp/snapshot-%d.jpg" % random.getrandbits(32))  # 保存图片。
    led.off()

results matching ""

    No results matching ""