【Automatic clock-in walking timing system based on face recognition】Use multi-threaded processing (QR code scanning + audio playback)
[Copy link]
[Automatic clock-in walking timing system based on face recognition] K210 MAIXBIT uses QR code recognition as user information
Based on the previously designed code for scanning QR codes and then prompting audio playback, we will further improve it and consider using threads to separate the code scanning task and the audio playback task.
(I) About multithreading
Create: audio_play_thread and 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
(II) Create a QR code for scanning
You need to call time.sleep() in the thread to cut out time for the audio playback task.
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)
(III) About audio playback thread
When the audio playback thread is executed is determined by judging the state of the variable 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)
|