人脸识别:如果识别到了人脸舵机转动
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)