前言
近期购买了一个Maix CAM试试效果,总体体验上来说比较不错,然后近期的核心是自己将代码进行打包并将其开机自启。
一、打包以及开机自启流程
1.1 编写代码
这边就直接使用了官网的uart例程代码并加入启动摄像头以及屏幕
from maix import uart,image, camera, display, app, time
cam = camera.Camera(512, 320) # Manually set resolution, default is too large
disp = display.Display() # MaixCAM default is 522x368
devices = uart.list_devices()
serial = uart.UART(devices[0], 115200)
while 1:
serial.write_str("hello ltq")
img = cam.read()
disp.show(img)
print("received:", serial.read(timeout = 2000))
time.sleep_ms(10) # sleep 1ms to make CPU free
1.2 打包
在MaixVision软件中可以直接进行打包,较为方便,如图左下角所示,点击即可
然后输入一些应用信息,方便后期的使用以及更改,之后就点确认即可
1.3 代码运行
断开连接后,可以发现屏幕多出了一个uart的应用,点击即可运行代码
效果在串口助手中可以看到
1.4 开机自启
开机自启也可以使用较为方便的过程, 直接在屏幕的初始界面中点击设置,拉到最底下有一个“开机自启”到里面点击想要的app即可在下次开机时自动启动此app。
二、删除打包以及开机自启流程
2.1 删除开机自启
在MaixVision软件中点击设备文件管理器,然后找到/maixapp/auto_start.txt,直接删除即可
2.2 删除打包
找到/maixapp/apps,然后找到刚才打包下载的uart,直接删除即可
三、问题解决及优化
3.1 无法回到初始界面
上述代码无法通过屏幕回到初始界面,这样程序只能在Maix Vision软件呢中进行更改,有点麻烦,
通过查询,发现官方仍有例程,可以通过设置一个exit的按键,直接退出程序,但是在与上述代码合并时,出现了显示摄像头的图像但是不显示按键的问题,代码还是有点问题。
from maix import touchscreen, app, time, display, image
ts = touchscreen.TouchScreen()
disp = display.Display()
img = image.Image(disp.width(), disp.height())
# draw exit button
exit_label = "< Exit"
size = image.string_size(exit_label)
exit_btn_pos = [0, 0, 8*2 + size.width(), 12 * 2 + size.height()]
img.draw_string(8, 12, exit_label, image.COLOR_WHITE)
img.draw_rect(exit_btn_pos[0], exit_btn_pos[1], exit_btn_pos[2], exit_btn_pos[3], image.COLOR_WHITE, 2)
def is_in_button(x, y, btn_pos):
return x > btn_pos[0] and x < btn_pos[0] + btn_pos[2] and y > btn_pos[1] and y < btn_pos[1] + btn_pos[3]
while not app.need_exit():
x, y, pressed = ts.read()
if is_in_button(x, y, exit_btn_pos):
app.set_exit_flag(True)
img.draw_circle(x, y, 1, image.Color.from_rgb(255, 255, 255), 2)
disp.show(img)
上述问题后续已经得到解决,直接在while里面添加以下代码即可
img.draw_string(8, 12, exit_label, image.COLOR_WHITE)
img.draw_rect(exit_btn_pos[0], exit_btn_pos[1], exit_btn_pos[2], exit_btn_pos[3], image.COLOR_WHITE, 2)
为了方便,直接将此代码与uart结合的代码奉上
from maix import uart,image, camera, display, app, time,touchscreen
cam = camera.Camera(512, 320) # Manually set resolution, default is too large
ts = touchscreen.TouchScreen()
disp = display.Display() # MaixCAM default is 522x368
devices = uart.list_devices()
serial = uart.UART(devices[0], 115200)
img = image.Image(disp.width(), disp.height())
# draw exit button
exit_label = "< Exit"
size = image.string_size(exit_label)
exit_btn_pos = [0, 0, 8*2 + size.width(), 12 * 2 + size.height()]
img.draw_string(8, 12, exit_label, image.COLOR_WHITE)
img.draw_rect(exit_btn_pos[0], exit_btn_pos[1], exit_btn_pos[2], exit_btn_pos[3], image.COLOR_WHITE, 2)
def is_in_button(x, y, btn_pos):
return x > btn_pos[0] and x < btn_pos[0] + btn_pos[2] and y > btn_pos[1] and y < btn_pos[1] + btn_pos[3]
while not app.need_exit():
x, y, pressed = ts.read()
img = cam.read()
# 绘制退出按钮
img.draw_string(8, 12, exit_label, image.COLOR_WHITE)
img.draw_rect(exit_btn_pos[0], exit_btn_pos[1], exit_btn_pos[2], exit_btn_pos[3], image.COLOR_WHITE, 2)
if is_in_button(x, y, exit_btn_pos):
app.set_exit_flag(True)
img.draw_circle(x, y, 1, image.Color.from_rgb(255, 255, 255), 2)
serial.write_str("hello world")
disp.show(img)
print("received:", serial.read(timeout = 2000))
time.sleep_ms(10) # sleep 1ms to make CPU free
基本上问题都已经解决,后续还有优化的话会进行更改。
3.2 问题待填充
注:有任何问题欢迎评论区交流讨论或者私信!后续有问题的话会进行更新!