import sensor, image, time
import car
from pid import PID
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # use GRAYSCALE.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
sensor.set_contrast(3)
sensor.set_gainceiling(16)
# 跳过几帧,让感光元件稳定下来,使设置生效
sensor.skip_frames(time = 2000)
# 把True改成False就可以关闭纵向翻转
#sensor.set_vflip(True)
# 加载Haar算子
# 默认情况下,这将使用所有阶段,较低的阶段更快但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25)
print(face_cascade)
# 第一组关键点
kpts1 = None
# Find a face!
# 找一张脸!
while (kpts1 == None):
img = sensor.snapshot()
img.draw_string(0, 0, "Looking for a face...")
# Find faces
objects = img.find_features(face_cascade, threshold=0.5, scale=1.25)
if objects:
# 在每个方向上将ROI扩大31个像素
face = (objects[0][0]-31, objects[0][1]-31,objects[0][2]+31*2, objects[0][3]+31*2)
# 使用检测面大小作为ROI提取关键点
kpts1 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, roi=face)
# 在第一个人脸周围画一个矩形
img.draw_rectangle(objects[0],color=120, thickness=10,fill=True )在脸前面加一个灯光,灯光照在人脸上。