Example: 01-Camera/01-Video-Recording/gif_on_movement.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 运动检测上的GIF视频录制示例
#
# 注意:运行此示例需要一张SD卡。
#
# 您可以使用OpenMV Cam录制gif文件。您可以向
# 录制器对象提供RGB565帧或灰度帧。使用照片编辑软件
# 如GIMP来压缩和优化Gif,然后再上传到网络。
#
# 此示例演示了如何使用OpenMV摄像头进行帧差分
# 运动检测。检测到运动后,您的OpenMV Cam将开始录制视频。

import sensor
import time
import gif
import os
import machine
import random

sensor.reset()  # 重置并初始化传感器。
sensor.set_pixformat(sensor.RGB565)  # 将像素格式设置为RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)  # 将帧大小设置为QQVGA(160x120)
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()
    g = gif.Gif("example-%d.gif" % random.getrandbits(32), loop=True)

    clock = time.clock()  # 跟踪FPS。
    for i in range(100):
        clock.tick()
        # clock.avg()返回帧之间的毫秒数 - gif延迟以
        g.add_frame(sensor.snapshot(), delay=int(clock.avg() / 10))  # 百分之一秒为单位。
        print(clock.fps())

    g.close()
    led.off()
    print("Restarting...")

results matching ""

    No results matching ""