MaixPy3 基本使用示例
更新时间 | 负责人 | 内容 | 备注 |
---|---|---|---|
2022年1月4日 | dianjixz | 初次编写文档 | --- |
2022年1月18日 | dianjixz | 修订文档文档 | 使用 Jupyter notebook 进行编写文档 |
2022年3月22日 | rui | 修改部分表述错误 | ---- |
2022年5月7日 | dls | 增加更多常用 API 说明 | ---- |
2022年5月11日 | dls | 0.4.8 后 image.new 取代 image.Image().new | 支持 new load open 接口 |
"hello world!"
确保目标机器已经安装 maixpy3 软件包。
使用 maixpy3 包在图像上显示 "hello world!" 。
In [1]:
from maix import display, image
hello_img = image.Image().new(size = (240, 240),
color = (255, 0, 0), mode = "RGB") #创建一张红色背景图
hello_img.draw_string(30, 115, "hello world!", scale = 1.0,
color = (255, 255, 255), thickness = 1) #在红色背景图上写下hello world
display.show(hello_img) #把这张图显示出来
[ rpyc-kernel ]( running at Fri Jan 14 16:48:03 2022 )
In [1]:
from maix import camera, display, image #引入python模块包
while True:
img = camera.capture() #从摄像头中获取一张图像
display.show(img) #将图像显示出来
Out[1]:
Traceback (most recent call last): File "<string>", line unknown, in <module> Remote.KeyboardInterrupt
In [1]:
from maix import image, display
img = image.Image().new(size = (240, 240), color = (0, 0, 0),
mode = "RGB") #创建一张黑色背景图
img.draw_line(0, 0, 100, 100, color = (127, 127, 127),
thickness = 1) #画一条从(0,0)到(100,100)的白色线段
display.show(img)
[ rpyc-kernel ]( running at Fri Jan 14 16:47:26 2022 )
In [1]:
from maix import image, display
img = image.Image().new(size = (240, 240), color = (0, 0, 0),
mode = "RGB") #创建一张黑色背景图
img.draw_rectangle(10, 10, 60, 60, color=(255, 0, 0),
thickness=-1) #画一个从(10, 10)到(60, 60)的红色实心矩形
img.draw_rectangle(80, 160, 160, 200, color=(0, 0, 255),
thickness=1) #画一个从(80, 160)到(160, 200)的蓝色矩形外框
display.show(img)
[ rpyc-kernel ]( running at Fri Jan 14 16:41:39 2022 )
In [1]:
from maix import image, display
img = image.Image().new(size = (240, 240), color = (0, 0, 0),
mode = "RGB") #创建一张黑色背景图
img.draw_circle(50, 50, 20, color=(0, 0, 255),
thickness=1) #画一个中心点在(50,50),半径为20的空心蓝圆
img.draw_circle(150, 150, 20, color=(255, 0, 0),
thickness=-1) #画一个中心点在(150,150),半径为20的实心红圆
display.show(img)
[ rpyc-kernel ]( running at Fri Jan 14 16:45:05 2022 )
In [1]:
from maix import display, image #引入python模块包
hello_img = image.Image().new(size = (240, 240), color = (0, 0, 0),
mode = "RGB") #创建一张黑色背景图
hello_img.draw_string(30, 115, "hello world!", scale = 1.0, color = (255, 255, 255),
thickness = 1) #在黑色背景图上写下hello world
display.show(hello_img) #把这张图显示出来
[ rpyc-kernel ]( running at Fri Jan 14 16:49:17 2022 )
In [1]:
from maix import image, display
img = image.Image().new(color=(255, 255, 255))
image.load_freetype("/home/res/sans.ttf")
s = "二进制例程"
x, y = image.get_string_size(s, 3)
img.draw_string(0, 120 - (y + 5), s, 3, (255, 0, 0)) # show left-button
s = "可执行文件示例"
x, y = image.get_string_size(s, 2)
img.draw_string(240 - x, 0, s, 2, (0, 255, 0)) # show right-up # wait fix
image.free_freetype()
s = "bin example"
x, y = image.get_string_size(s, 1)
img.draw_string(0, 240 - (y + 5), s, 1, (0, 0, 255)) # show left-button
display.show(img)
In [1]:
from maix import image, display
img = image.Image().new(size = (240, 240), color = (0, 0, 0),
mode = "RGB") #创建一张黑色背景图
img.draw_ellipse(120, 40, 20, 50, 90, 0, 360, color=(0,255,0),
thickness = 1)
display.show(img)
[ rpyc-kernel ]( running at Fri Jan 14 16:49:49 2022 )
画椭圆的参数比较复杂,暂无 API 说明。
In [1]:
from maix import image, display
img = image.Image().new(color=(255, 0, 0))
img.draw_string(100, 100, "hello", 2, color=(0, 0, 255))
mk = img.crop(90, 90, 100, 50) # 截一张图出来, x, y, w, h
imga = image.Image().new(color=(0, 255, 0))
imga.draw_image(mk, 10, 10)
# imga.draw_image(imga) # py no-allow use self but libmaix support
imga.draw_image(mk, 100, 100, alpha=0.5)
display.show(imga)
[ rpyc-kernel ]( running at Sat May 7 11:54:46 2022 )
图像的缩放、旋转、翻转
在 0.4.8 后才支持翻转 。
resize(w, h, func) # width, height, cv::INTER_LINEAR
flip(code) # -1 horizontal & vertical, 1 horizontal, 0 vertical
rotate(angle, adjust) # [-360, +360], adjust uasge
In [1]:
from maix import image, display
img = image.Image().new(color=(255, 0, 0))
img.draw_string(100, 100, "KL", 2, color=(0, 0, 255))
mk = img.crop(90, 90, 50, 50)
imga = image.Image().new(color=(0, 255, 0))
imga.draw_image(mk, 10, 10) # 原图
imga.draw_image(mk.rotate(-90), 70, 10) # 顺时针 90 旋转
imga.draw_image(mk.flip(1), 130, 10) # 水平翻转 horizontal
imga.draw_image(mk.resize(80, 50), 10, 80) # 缩放到 (80, 50)
imga.draw_image(mk.flip(0), 100, 80) # 垂直翻转 vertical
imga.draw_image(mk.rotate(-135, adjust=0), 10, 150) # 顺时针 135 旋转
imga.draw_image(mk.rotate(+45, adjust=1), 100, 150) # 逆时针 45 旋转
display.show(imga)
[ rpyc-kernel ]( running at Wed May 11 16:05:34 2022 )
In [1]:
from maix import image, display
img = image.Image()
# 画一图
img.new(size=(240, 240), mode="RGB")
img.draw_line(0, 0, 240, 240)
img.draw_rectangle(40, 120, 160, 200, color=(255, 0, 0), thickness=16) #
img.draw_circle(120, 120, 20, color=(0, 255, 0))
img.draw_string(40, 40, "dalaoshu", 2, color=(0, 0, 255))
# 保存图片到 /root/ 中
img.save("/root/test.png")
tmp = image.Image()
# 读取 /root/ 中的图片
tmp.open("/root/test.png")
display.show(tmp)
In [1]:
from maix import image, display, camera
bull = [(13, 11, -91, 54, 48, -28)] # 蓝色的lab阈值
while True:
img = camera.capture()
blobs = img.find_blobs(bull) #在图片中查找lab阈值内的颜色色块
if blobs:
for i in blobs:
img.draw_rectangle(i["x"], i["y"], i["x"] + i["w"], i["y"] + i["h"],
color=(0, 0, 255), thickness=1) #将找到的颜色区域画出来
display.show(img)
Out[1]:
Traceback (most recent call last): File "<string>", line unknown, in <module> Remote.KeyboardInterrupt
In [1]:
from maix import image, display, camera
import time
while True:
img = camera.capture()
line = img.find_line()
img.draw_line(line["rect"][0], line["rect"][1], line["rect"][2],
line["rect"][3], color=(255, 255, 255), thickness=1)
img.draw_line(line["rect"][2], line["rect"][3], line["rect"][4],
line["rect"][5], color=(255, 255, 255), thickness=1)
img.draw_line(line["rect"][4], line["rect"][5], line["rect"][6],
line["rect"][7], color=(255, 255, 255), thickness=1)
img.draw_line(line["rect"][6], line["rect"][7], line["rect"][0],
line["rect"][1], color=(255, 255, 255), thickness=1)
img.draw_circle(line["cx"], line["cy"], 4,
color=(255, 255, 255), thickness=1)
display.show(img)
Out[1]:
Traceback (most recent call last): File "<string>", line unknown, in <module> Remote.KeyboardInterrupt
In [1]:
from maix import image, display, camera
import time
while True:
img = camera.capture()
colors = img.get_blob_color((100, 100, 10, 10), 0, 0)
img.draw_rectangle(100, 100, 110, 110,
color=(255, 0, 0), thickness=1) #将找到的颜色区域画出来
img.draw_rectangle(9, 9, 21, 21,
color=(255, 255, 255), thickness=1) #将找到的颜色区域画出来
img.draw_rectangle(10, 10, 20, 20,
color=(int(colors[0]), int(colors[1]), int(colors[2])), thickness=-1) #将找到的颜色区域画出来
display.show(img)
Out[1]:
Traceback (most recent call last): File "<string>", line unknown, in <module> Remote.KeyboardInterrupt
In [1]:
from maix import camera, display, zbar
while True:
img = camera.capture()
result = zbar.scan_codes(["qrcode", "code39"], img)
display.show(img.draw_string(10, 10, str(result), 2.0, (255, 0, 0)))
Out[1]:
Traceback (most recent call last): File "<string>", line unknown, in <module> Remote.KeyboardInterrupt