【01Studio MaixPy AI K210】11.摄像头

目录

导包:

sensor库

image库

time库

例程:

导包:

import sensor, image, time, lcd

sensor库

#摄像头对象,通过 import 直接调用

#初始化摄像头
sensor.reset()

'''
设置像素格式。
pixformat 有 3 个参数。
sensor.GRAYSCAL:灰度图像,每像素 8 位(1 字节),处理速度快;
sensor.RGB565: 每像素为 16 位(2 字节),5 位用于红色,6 位用于绿色,5 位用于蓝色,处理速度比灰度图像要慢。
'''
sensor.set_pixformat(pixformat)

'''
设置每帧大小(即图像尺寸)。
常用的 framesize 参数有下面这些:
sensor.QQVGA: 160*120;
sensor.QVGA: 320*240;
sensor.VGA: 640*480;
'''
sensor.set_framesize(framesize)

'''
摄像头配置后跳过 n 帧或者等待时间 time 让其变稳定。
n:跳过帧数;
time:等待时间,单位 ms。
(如果 n 和 time 均没指定,则默认跳过 300 毫秒的帧。)
'''
sensor.skip_frames([n, time])

#使用相机拍摄一张照片,并返回 image 对象。
img=sensor.snapshot()

'''
设置摄像头垂直翻转
参数:
	enable: 1 表示开启垂直翻转 0 表示关闭垂直翻转
返回值:无
'''
sensor.set_vflip(enable)

image库

'''
从图像中的(x, y)位置开始绘制8x10文本。您可以单独传递x,y,也可以作为元组(x,y)传递。

text 是写入图像的字符串,不支持中文显示。 \n, \r, 和 \r\n 结束符将光标移至下一行。

color 是用于灰度或RGB565图像的RGB888元组。默认为白色。但是,您也可以传递灰度图像的基础像素值(0-255)或RGB565图像的字节反转RGB565值。

x_spacing 允许你在字符之间添加(如果是正数)或减去(如果是负数)x像素,设置字符间距。

y_spacing 允许你在字符之间添加(如果是正数)或减去(如果是负数)y像素,设置行间距。

scale 图像上文本的大小。仅整数值(例如,1/2/3 /等)。

mono_space 默认为True,强制文本间距固定。对于大文本,这看起来很糟糕。设置False以获得非固定宽度的字符间距,看起来好多了。

返回图像对象,以便您可以使用 . 表示法调用另一个方法。

不支持压缩图像和bayer图像。
'''
image.draw_string(x, y, text[, color[, scale=1[, x_spacing=0[, y_spacing=0[, mono_space=True]]]]])
img.draw_string(0, 0, 'FPS: '+str(clock.fps()), color = (255, 255,255), scale = 1,mono_space = False)

time库

#创建一个时钟。
clock=time.clock()

#开始追踪运行时间。
clock.tick()

'''
停止追踪运行时间,并返回当前 FPS(每秒帧数)。
在调用该函数前始终首先调用 tick 。
'''
clock.fps ()

例程:

'''
main.py

显示摄像头拍摄的图像
'''

import sensor, image, time, lcd

lcd.init(freq=15000000)             #初始化LCD
sensor.reset()                      #复位和初始化摄像头,执行sensor.run(0)停止。
sensor.set_vflip(1)                 #将摄像头设置成后置方式(所见即所得)


sensor.set_pixformat(sensor.RGB565) # 设置像素格式为彩色 RGB565 (或灰色)
sensor.set_framesize(sensor.QVGA)   # 设置帧大小为 QVGA (320x240)
sensor.skip_frames(time = 2000)     # 等待设置生效.
clock = time.clock()                # 创建一个时钟来追踪 FPS(每秒拍摄帧数)

while(True):
    clock.tick()                    # 更新 FPS 时钟.
    img = sensor.snapshot()         # 拍摄一个图片并保存.
    img.draw_string(0, 0, 'FPS: '+str(clock.fps()), color = (255, 255,255), scale = 1,mono_space = False)
    lcd.display(img)                # 在LCD上显示
    print(clock.fps())              # 注意: 当 K210 连接到 IDE 时候,运行速度减
                                    #半,因此当断开 IDE 时 FPS 会提升。

(代码是:#实验名称:人脸检测_68关键点 #翻译和注释:01Studio #实验平台:01Studio CanMV K210 mini #导入相关模块 import sensor, image, time, lcd from maix import KPU import gc lcd.init() # LCD初始化 sensor.reset() # 摄像头初始化 #设置摄像头颜色和帧率 sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 500) clock = time.clock() od_img = image.Image(size=(320,256), copy_to_fb=False) #构建KPU对象 #需要导入2个模型,分别是人脸检测模型和68关键点检测模型 anchor = (0.893, 1.463, 0.245, 0.389, 1.55, 2.58, 0.375, 0.594, 3.099, 5.038, 0.057, 0.090, 0.567, 0.904, 0.101, 0.160, 0.159, 0.255) kpu = KPU() print("ready load model") kpu.load_kmodel("/sd/face_detect.kmodel") kpu.init_yolo2(anchor, anchor_num=9, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.2, classes=1) lm68_kpu = KPU() print("ready load model") lm68_kpu.load_kmodel("/sd/landmark68.kmodel") RATIO = 0.08 while 1: gc.collect() #print("mem free:",gc.mem_free()) clock.tick() # Update the FPS clock. img = sensor.snapshot() a = od_img.draw_image(img, 0,0) od_img.pix_to_ai() kpu.run_with_output(od_img) dect = kpu.regionlayer_yolo2() fps = clock.fps() #识别到人脸 if len(dect) > 0: print("dect:",dect) for l in dect : a = img.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0)) x1_t = l[0] - RATIO*l[2] x2_t = l[0]+l[2] + RATIO*l[2] y1_t = l[1] - RATIO*l[3] y2_t = l[1]+l[3] + RATIO*l[3] x1 = int(x1_t) if x1_t>1 else 1 x2 = int(x2_t) if x2_t<320 else 319 y1 = int(y1_t) if y1_t>1 else 1 y2 = int(y2_t) if y2_t<256 else 255 cut_img_w = x2-x1+1 cut_img_h = y2-y1+1 #print("cut img ",x1, y1, cut_img_w, cut_img_h) face_cut = img.cut(x1,y1,cut_img_w,cut_img_h) face_cut_128 = face_cut.resize(128, 128) face_cut_128.pix_to_ai() #识别68个关键点 out = )
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因心,三人水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值