学习日志4

人脸识别:如果识别到了人脸舵机转动

import sensor, time, image
from pyb import Servo

s1 = Servo(1)

sensor.reset()

sensor.set_contrast(3)
sensor.set_gainceiling(16)

sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)


face_cascade = image.HaarCascade("frontalface", stages=25)

print(face_cascade)

# FPS clock
clock = time.clock()

while (True):
    clock.tick()

    img = sensor.snapshot()


    objects = img.find_features(face_cascade, threshold=0.5, scale=1.5)

    print(objects)

    if objects:
        s1.angle(90)
    else:
        s1.angle(-90)
    for r in objects:
        img.draw_rectangle(r)

颜色识别:若识别到了红色则p7舵机转动,若识别到了绿色则p8舵机转动

import sensor, image, time, math
from pyb import LED
from pyb import Servo

s1 = Servo(1) # P7
s2 = Servo(2) # P8

red=LED(1)
green=LED(2)
#bule=LED(3)

threshold_index = 0 # 0 for red, 1 for green, 2 for blue

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True):

        # These values are stable all the time.
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())

    if blob.code()==1:
        for i in range(90,-90):
            s1.angle(i)
    if blob.code()==2:
        for i in range(90,-90):
            s2.angle(i)
    if blob.code()!=1 or blob.code()!=2:
        for i in range(-90,90):
            s1.angle(0)
            s2.angle(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值