【01Studio MaixPy AI K210】9.多线程运行

本文介绍如何使用MaixPy进行多线程编程来控制两个LED灯的闪烁,展示了线程函数的定义及启动过程。通过具体代码实现了两个LED灯以不同频率交替亮灭的效果。

目录

引脚图:

导包:

开启线程:

例程:

引脚图:

导包:

from Maix import GPIO #IO口
from fpioa_manager import fm #引脚
import utime #时间模块
import _thread #导入线程模块
import time

开启线程:

'''
启动新线程并返回其标识符。线程使用参数列表 args(必须是元组)执行函数函数。
可选的 kwargs 参数指定关键字参数的字典。当函数返回时,线程将静默退出。
当函数终止并出现未处理的异常时,将打印堆栈跟踪,然后线程退出(但其他线程继续运行)。
'''
_thread.start_new_thread(function, args[, kwargs])
_thread.start_new_thread(func1,("1",)) #开启线程1,参数必须是元组
_thread.start_new_thread(func2,("2",)) #开启线程2,参数必须是元组

例程:

'''
main.py

说明:通过编程实现多线程。
'''

from Maix import GPIO #IO口
from fpioa_manager import fm #引脚
import utime #时间模块
import _thread #导入线程模块
import time

fm.register(12, fm.fpioa.GPIO0)
fm.register(13, fm.fpioa.GPIO1)

LED_B = GPIO(GPIO.GPIO0, GPIO.OUT,value=1)
LED_G = GPIO(GPIO.GPIO1, GPIO.OUT,value=1)

#线程函数
def func1(name):
    while True:
        print("线程 {}".format(name))
        print("111")
        print("111111")
        LED_B.value(0)
        time.sleep(1)
        LED_B.value(1)
        time.sleep(1)

def func2(name):
    while True:
        print("线程 {}".format(name))
        print("222")
        print("222222")
        LED_G.value(0)
        time.sleep(2)
        LED_G.value(1)
        time.sleep(2)

_thread.start_new_thread(func1,("1",)) #开启线程1,参数必须是元组
_thread.start_new_thread(func2,("2",)) #开启线程2,参数必须是元组

while True:
    pass

以下是一个示例代码,演示了如何在k210 maixpy平台上使用多线程运行两个模型: ```python import gc import sensor import image import lcd import KPU as kpu import time from machine import I2C from fpioa_manager import fm from board import board_info from Maix import GPIO from machine import UART from machine import Timer # 初始化I2C总线 fm.register(board_info.PIN10, fm.fpioa.GPIO0) i2c = I2C(I2C.I2C0, freq=400000, scl=10, sda=11) devices = i2c.scan() if devices: print("I2C addresses found:", [hex(i) for i in devices]) else: print("No I2C devices found") # 初始化GPIO口 fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) button_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) # 初始化LCD显示 lcd.init(freq=15000000) lcd.rotation(2) # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_vflip(1) sensor.run(1) # 初始化串口 fm.register(board_info.PIN4, fm.fpioa.UART1_TX, force=True) fm.register(board_info.PIN5, fm.fpioa.UART1_RX, force=True) uart1 = UART(UART.UART1, 115200, timeout=1000, read_buf_len=4096) # 初始化定时器 tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1000, unit=Timer.UNIT_MS, callback=lambda timer: print("tick")) # 加载人脸检测模型 task_detect = kpu.load('/sd/models/multi_face.kmodel') anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) kpu.init_yolo2(task_detect, 0.5, 0.3, 5, anchor) # 加载分类模型 task_classify = kpu.load('/sd/models/mobilenet.kmodel') # 定义多线程函数 def detect_thread(): while True: img = sensor.snapshot() t_start = time.ticks_ms() # 运行人脸检测模型 faces = kpu.run_yolo2(task_detect, img) t_stop = time.ticks_ms() print('Detection time: %dms' % (t_stop - t_start)) # 在LCD上绘制检测结果 lcd.display(img) lcd.draw_string(0, 0, 'face count: %d' % len(faces), lcd.WHITE, lcd.BLACK) for i in range(len(faces)): x, y, w, h = faces[i].rect() lcd.draw_rectangle(x, y, w, h, lcd.RED, thickness=2) face_img = img.cut(x, y, w, h) # 运行分类模型 t_start = time.ticks_ms() p = kpu.run(task_classify, face_img) t_stop = time.ticks_ms() print('Classification time: %dms' % (t_stop - t_start)) # 发送分类结果到串口 uart1.write('face %d: %s\r\n' % (i, p)) def button_thread(): while True: if button_a.value() == 0: print("Button A pressed!") # 开始多线程 import _thread _thread.start_new_thread(detect_thread, ()) _thread.start_new_thread(button_thread, ()) # 释放资源 gc.collect() ``` 该示例代码中,我们通过多线程的方式,同时运行了人脸检测和分类两个模型。其中,`detect_thread`函数用于运行人脸检测模型,并在LCD上绘制检测结果;`button_thread`函数用于监听按钮按下事件。在`detect_thread`函数中,我们首先调用`sensor.snapshot()`方法获取摄像头采集到的图像,然后使用`kpu.run_yolo2`方法运行人脸检测模型,获取检测结果。接着,我们在LCD上绘制检测结果,并针对每个人脸,使用`img.cut`方法获取人脸图像,然后使用`kpu.run`方法运行分类模型,获取分类结果。最后,我们将分类结果发送到串口中。在`button_thread`函数中,我们使用`button_a.value()`方法获取按钮状态,如果按钮被按下,则打印一条提示信息。 需要注意的是,由于k210 maixpy平台内存较小,因此在使用多线程时需要注意内存管理,尤其是在同时运行多个模型时。在示例代码中,我们通过`gc.collect()`方法释放内存。如果需要同时运行更多的模型或任务,可以考虑引入操作系统或使用更高级的开发框架。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

因心,三人水

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

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

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

打赏作者

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

抵扣说明:

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

余额充值