【基于人脸识别的自动打卡健走计时系统】 K210 MAIXBIT用二维码识别作为用户信息
基于之前设计的扫码二维码,然后提示音频播放的代码,再完善一下,考虑用线程将扫码任务和播放音频任务进行分开
(一)关于多线程
创建:audio_play_thread和qrcode_detect_thread
import _thread
import time
#---------------------------------------------------------------------------------------------------
clock = time.clock()
print(os.listdir("/"))
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)
sensor.run(1)
sensor.skip_frames(30)
audio_state = 0
lock = _thread.allocate_lock()
_thread.start_new_thread(qrcode_detect_thread,("qrcodes detect thread",))
_thread.start_new_thread(audio_play_thread,("audio play thread",))
while True:
pass
(二)创建二维码扫码
需要在线程中调用time.sleep()将时间切出给音频播放任务
import sensor
import image
import lcd
import time
#---------------------------------------------------------------------------------------------------
#二维码识别
def qrcode_detect_thread(name):
global audio_state
while True:
img = sensor.snapshot()
res = img.find_qrcodes()
fps =clock.fps()
if len(res) > 0:
img.draw_string(2, 2, res[0].payload(), color=(0,128,0), scale=2)
print(res[0].payload())
lock.acquire()
audio_state = 1
print(lock.locked())
lock.release()
time.sleep_ms(200)
else:
lock.acquire()
audio_state = 0
print(lock.locked())
lock.release()
print("hello {}".format(name),"fps", fps)
lcd.display(img)
(三)关于音频播放线程
关于音频播放线程何时执行,靠判断变量audio_state状态得到
# I2S 音频播放模块
import uos, os
from fpioa_manager import *
from Maix import I2S, GPIO
import audio
#---------------------------------------------------------------------------------------------------
# 音频播放
def audio_init():
# register i2s(i2s0) pin
fm.register(34, fm.fpioa.I2S0_OUT_D1, force=True)
fm.register(35, fm.fpioa.I2S0_SCLK, force=True)
fm.register(33, fm.fpioa.I2S0_WS, force=True)
# init i2s(i2s0)
wav_dev = I2S(I2S.DEVICE_0)
print("i2s init")
return wav_dev
def audio_play(wav_dev, file_path, volume):
# init audio
#player = audio.Audio(path="/sd/2.wav") #需要支持从SD卡读取音频文件
player = audio.Audio(path=file_path)
player.volume(volume)
print("audio play init")
# read audio info
wav_info = player.play_process(wav_dev)
# config i2s according to audio info
wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER, resolution=I2S.RESOLUTION_16_BIT,
cycles=I2S.SCLK_CYCLES_16, align_mode=I2S.RIGHT_JUSTIFYING_MODE)
wav_dev.set_sample_rate(wav_info[1])
# loop to play audio
while True:
ret = player.play()
if ret == None:
print("format error")
break
elif ret == 0:
print("end")
break
time.sleep_us(20)
player.finish()
def audio_play_thread(name):
dev = audio_init()
print("audio init")
while True:
lock.acquire()
print(lock.locked())
state = audio_state
lock.release()
print("hello {}".format(name))
if (state == 1):
print("audio play start...\r\n")
audio_play(dev, "/sd/2.wav", 50) #识别成功
print("audio play end...\r\n")
#audio_play(dev, "/sd/2.wav", 50) #识别成功
time.sleep(1)
基于之前设计的扫码二维码,然后提示音频播放的代码,再完善一下,考虑用线程将扫码任务和播放音频任务进行分开。看来项目快完工了。