Openmv学习

供电

OpenMV有两种供电方式:

①通过USB口供电,插入数据线即可

②通过外接电源,比如电池或者稳压电源插入VCC与GND,3.7V到5V,推荐5V

(板子里面的二极管可以同时USB与VCC供电)

错误:不能直接向3V3引脚供电很容易烧毁芯片:

3.3V引脚是连接到STM32芯片上的,不能直接给这个引脚供电,可以输出3.3V给其它的模块供电

import sensor,image ,time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)

ROI=(80,30,15,15)

while(True)
    img=sensor.snapshot()
    statistics=img.get_statistics(roi=ROI)
    color_l=statistics.l_mode()
    color_a=statistics.a_mode()
    color_b=statistics.b_mode()
    print(color_l,color_a,color_b)
    img.draw_rectangle(ROI)
import sensor,image ,time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)

while(True)
    img=sensor.snapshot()
    img.draw_line((20,30,40,50));
    img.draw_line((80,50,100,100),color=(255,0,0))
    img.draw_rectangle((20,30,41,51),color=(255,0,0))
    img.draw_circle(50,50,30)
    img.draw_cross(90,60,size=10)
    img.draw_string(10,10,"hello world!")
mport sensor,image ,time,math

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(30)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock=time.clock()

while(True)
    clock.tick()
    img=sensor.snapshot()
    for tag in img.find_apriltags()
        img.draw_rectangle(tag.rect(),color=(255,0,0))
        img.draw_cross(tag.cx(),tag.cy(),color=(0,255,0))
        degress=180*tag.rotation()/math.pi
        print(tag.id(),degress)
mport sensor,image ,time,math

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(30)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock=time.clock()

f_x=(2.8/3.984)*160
f_y=(2.8/2.952)*120
c_x=160*0.5
c_y=120*0.5

def degress(radians):
    return (180*radians)/math.pi

while(True)
    clock.tick()
    img=sensor.snapshot()
    for tag in img.find_apriltags(fx=f_x,fy=f_y,cx=c_x,cy=c_y):
        img.draw_rectangle(tag.rect(),color=(255,0,0))
        img.draw_cross(tag.cx(),tag.cy(),color=(0,255,0))
        print_args=(tag.x_translation(),tag.y_translation(),tag.z_translation(),
                    \degress(tag.x_rotation(),tag.y_rotation(),tag.z_rotation()))
        print("Tx:%f,Ty %f,Tz %f,Rx %f,Ry %f,Rz %f"%print_args)
    print(clock.fps())
import time,sensor,image
from image import SEARCH_EX,SEARCH_DS

sensor.reset()

sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

template=image.Image("/template.pgm")

clock=time.clock()

while(True):
    clock.tick()
    img=sensor.snapshot()
    r=img.find_template(template,0.70,step=4,search=SEARCH_EX)
    
    if r:
        img.draw_rectangle(r)
        
    print(clock.fps())
import time,sensor,image
from image import SEARCH_EX,SEARCH_DS

sensor.reset()

sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

templates=["/0.pgm","/1,pgm","/2.pgm","/6.pgm"]

clock=time.clock()

while(True):
    clock.tick()
    img=sensor.snapshot()
    
    for t in templates:
        template=image.Image(t)
        r=img.find_template(template,0.70,step=4,search=SEARCH_EX)
    
    if r:
        img.draw_rectangle(r)
        print(t)
import time,sensor,image

sensor.reset()

sensor.set_framesize(sensor.VGA)
sensor.set_windowing((320,240))
sensor.set_pixformat(sensor.GRAYSCALE)

sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)

def draw_keypoints(img,kpts):
    if kpts:
        print(kpts)
        img.draw_keypoints(kpts)
        img.sensor.snapshot()
        time.sleep_ms(1000)
        
kpts1=None

Clock=time.clock()

while(True)
    clock.tick()
    img=sensor.snapshot()
    if(kpts1==None):
        kpts1=img.find_keypoints(max_keypoints=150,threshold=10,scale_factor=1.2)
        draw_keypoints(img,kpts1)
    else:
        kpts2=img.find_keypoints(max_keypoints=150,threshold=10,normalzed=True)
        if(kpts2):
            match=image.match_descriptor(kpts1,kpts2,threshold=85)
            
            if(match.count()>10):
                img.draw_rectangle(match.rect())
                img.draw_cross(match.cx(),match.cy(),size=10)
                
            print(kpts2,"matched:%d dt"%d"%(match.count(),match.theta()))
            
img.draw_string(0,0,"FPS:%.2f"%(clock.fps()))
import time,sensor,image

yellow_threshold=(56, 83, 5, 57,63,80)

sensor.reset()

sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock=time.clock()

k=5000

while(True)
    clock.tick()
    img=sensor.snapshot()
    blobs=img.find_blobs([yellow_threshold])
    if len(blobs)==1
        b=blobs[0]
        img.draw_rectangle(b[0:4])
        img.draw_cross(b[5],b[6])
        Lm=(b[2]+b[3])/2
        length=K/Lm
        print(length)

首先我们进行圆形识别,然后在识别到的圆形区域内进行颜色统计,判断区域内最多的颜色是否是红色。

import time,sensor,image

sensor.reset()
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock=time.clock()

while(True)
    clock.tick()
    img=sensor.snapshot(),lens_corr(1.8)
    for c in img.find_circles(threshold=3500,x_margin=10,y_margin=10,r_margin=10,
                             r_min=2,r_max=100,r_step=2):
        area=(c.x()-c.r(),c.y()-c.r,2*c.r(),2*c.r())
        
        statistics=img.get_statistics(roi=area)
        print(statistics)
        if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127
            img.draw_circle(c.x(),c.y(),c.r(),color(255,0,0))
        else:
            img.draw_rectangle(area,color=(255,255,255))
    print("FPS %f" % clock.fps())        
#使用OpenMV的PWM
import time
from pyb import Pin,Timer

#设置频率,初始化定时器4,将其设置为50HZ,也就是说一个PWM周期为20ms
tim=Timer(4,freq=50)
#设置频率,初始化定时器2,将其设置为50HZ,也就是说一个PWM周期为20ms
Tim=Timer(2,freq=50) 

# 生成50HZ方波,使用TIM4,channels 1,2,3分别是 5% , 10% ,15占空比。
tim.channel(1,Timer.PWM,pin=Pin("P7"),pulse_width_percent=5)
tim.channel(2,Timer.PWM,pin=Pin("P8"),pulse_width_percent=10)
tim.channel(3,Timer.PWM,pin=Pin("P9"),pulse_width_percent=15)


# 生成50HZ方波,使用TIM2,channel  1,2,3分别是 20% , 25% ,30占空比。
Timer.channel(1,Timer.PWM,pin=Pin("P6"),pulse_width_percent=20)
Timer.channel(4,Timer.PWM,pin=Pin("P5"),pulse_width_percent=25)
Timer.channel(3,Timer.PWM,pin=Pin("P4"),pulse_width_percent=30)

while(True):
    time.sleep_ms(1000)

人脸照片采集代码

import sensor
import image
import pyb

# 定义 LED 引脚
RED_LED_PIN = 1  # 根据你的硬件调整这个值
BLUE_LED_PIN = 2  # 根据你的硬件调整这个值

sensor.reset()  # 初始化sensor
sensor.set_pixformat(sensor.GRAYSCALE)  # 设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种
sensor.set_framesize(sensor.B128X128)  # 设置图像像素大小
sensor.set_windowing(92,112)
sensor.skip_frames(10)
sensor.skip_frames(time=2000)  # 让新的设置生效

num = 1

n=20

while (n):
    pyb.LED(RED_LED_PIN).on()
    sensor.skip_frames(time = 3000)
    
    pyb.LED(RED_LED_PIN).off()
    pyb.LED(BLUE_LED_PIN).on()
    
    print(n)
    sensor.snapshot().save("zhinengmenjin/s%s/%s.pgm"%(num,n))
    
    n -=1
    
    pyb.LED(BLUE_LED_PIN).off()
    print("Done! Reset the camera to see the saved image.")
        

人脸识别代码

import sensor
import time
import image
import pyb
import display
from pyb import UART

# 初始化传感器
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.B128X128)
sensor.set_windowing(92, 112)
sensor.skip_frames(time=5000)

# 初始化UART和LCD
uart = UART(3, 115200)
lcd = display.SPIDisplay()

NUM_SUBJECTS = 1
NUM_SUBJECTS_IMGS = 20

# 获取基准图像特征
img = sensor.snapshot()
d0 = img.find_lbp((0, 0, img.width(), img.height()))
img = None 
pmin = float('inf')  # 设置为正无穷
num = -1  # 初始化为-1表示未找到

def find_min(pmin, a, s):
    global num 
    if a < pmin:
        pmin = a 
        num = s 
    return pmin
    
for s in range(1, NUM_SUBJECTS + 1):
    dist = 0
    for i in range(2, NUM_SUBJECTS_IMGS + 1):
        try:
            img = image.Image("zhinengmenjin/s%d/%d.pgm" % (s, i))
            d1 = img.find_lbp((0, 0, img.width(), img.height()))
            dist += image.match_descriptor(d0, d1)
        except OSError:
            print("Error reading image: zhinengmenjin/s%d/%d.pgm" % (s, i))
            continue  # 跳过这次循环
        
    avg_dist = dist / NUM_SUBJECTS_IMGS
    print("Average dist for subject %d: %.2f" % (s, avg_dist))
    pmin = find_min(pmin, avg_dist, s)
    print("Current minimum dist: ", pmin)
        
print("Subject with minimum distance:", num)

# 发送结果
if pmin > 10000:
    uart.write(b'\x01')
else:
    uart.write(b'\x00')

while True:
    lcd.write(sensor.snapshot())  # 拍照并显示图像
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值