Example: 01-Camera/01-Video-Recording/gif_on_face_detection.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 人脸检测上的GIF视频录制示例
#
# 注意:运行此示例需要一张SD卡。
#
# 您可以使用OpenMV Cam录制gif文件。您可以向
# 录制器对象提供RGB565帧或灰度帧。使用照片编辑软件
# 如GIMP来压缩和优化Gif,然后再上传到网络。
#
# 此示例演示了如何在OpenMV摄像头上使用面部追踪功能拍摄一张
# gif。
import sensor
import image
import time
import gif
import machine
import random
sensor.reset() # 初始化相机传感器。
sensor.set_pixformat(sensor.GRAYSCALE) # 将像素格式设置为RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # 将帧大小设置为QVGA
sensor.skip_frames(time=2000) # 等待设置生效。
led = machine.LED("LED_RED")
# 加载面部检测的HaarCascade。这是您的OpenMV摄像头
# 可以使用下面的find_features()方法来检测人脸。您的OpenMV
# 摄像头内置了frontalface HaarCascade。默认情况下,所有阶段的
# HaarCascade都会被加载。然而,您可以调整阶段数以加快
# 处理速度,但会牺牲准确性。frontalface HaarCascade有25个
# 阶段。
face_cascade = image.HaarCascade("frontalface", stages=25)
while True:
print("About to start detecting faces...")
sensor.skip_frames(time=2000) # 给用户时间准备。
print("Now detecting faces!")
diff = 10 # 我们会在10帧后检测到人脸。
while diff:
img = sensor.snapshot()
# 阈值可以在0.0到1.0之间。较高的阈值会导致
# 更高的检测率,但会有更多的误报。比例值
# 控制匹配比例,允许您检测更小的面孔。
faces = img.find_features(face_cascade, threshold=0.5, scale_factor=1.5)
if faces:
diff -= 1
for r in faces:
img.draw_rectangle(r)
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...")