import sensor
import image
import lcd
import os
import ubinascii
import KPU as kpu
import time
from Maix import FPIOA, GPIO
import gc
from fpioa_manager import fm
from board import board_info
import utime
A_1=A_2=0
B_1=B_2=B_3=B_4=0
color_R = (255, 0, 0)
color_G = (0, 255, 0)
color_B = (0, 0, 255)
e_1=[0,0,0,0,0,0,0,0,0,0]
a_1=a_2=0
from Maix import FPIOA
from Maix import GPIO
fpioa = FPIOA()
fpioa.set_function(0,fpioa.GPIOHS0)
led1 = GPIO(GPIO.GPIOHS0,GPIO.OUT)
fpioa.set_function(1,fpioa.GPIOHS1)
led2 = GPIO(GPIO.GPIOHS1,GPIO.OUT)
feature_file_exists = 0
for v in os.ilistdir('/sd'):
if v[0] == 'features.txt' and v[1] == 0x8000:
feature_file_exists = 1
record_ftr=[]
record_ftrs=[]
names = ['MR.1', 'MR.2', 'MR.3', 'Mr.4', 'Mr.5', 'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9' , 'Mr.10']
reco = ''
record = []
def save_feature(feat):
with open('/sd/features.txt','a') as f:
record =ubinascii.b2a_base64(feat)
f.write(record)
st = ''
if(feature_file_exists):
print("start")
with open('/sd/features.txt','rb') as f:
s = f.readlines()
print(len(s))
for line in s:
record_ftrs.append(bytearray(ubinascii.a2b_base64(line)))
def lvbo(c_1):
global e_1
global a_1
global a_2
if a_1<10:
e_1[a_1]=c_1
a_1=a_1+1
print("e_1",e_1)
else:
a_2=max(e_1, default='列表为空', key=lambda v: e_1.count(v))
if a_2==0:
print("啥也没有")
if a_2==1:
print("已佩戴口罩")
led1.value(0)
if a_2==2:
print("未佩戴口罩")
led1.value(1)
if a_2==3:
print("人脸识别成功")
led2.value(0)
if a_2==4:
print("人脸识别失败")
led2.value(1)
a_1=1
def drawConfidenceText(image, rol, classid, value):
global A_1
global A_2
text = ""
_confidence = int(value * 100)
if classid == 1:
text = 'mask: ' + str(_confidence) + '%'
lvbo(1)
else:
text = 'no_mask: ' + str(_confidence) + '%'
lvbo(2)
image.draw_string(rol[0], rol[1], text, color=color_R, scale=2.5)
class_IDs = ['no_mask', 'mask']
task_fe = kpu.load(0x500000)
task = kpu.load(0x300000)
clock = time.clock()
fm.register(board_info.BOOT_KEY, fm.fpioa.GPIOHS0)
key_gpio = GPIO(GPIO.GPIOHS0, GPIO.IN)
start_processing = False
BOUNCE_PROTECTION = 50
def set_key_state(*_):
global start_processing
start_processing = True
utime.sleep_ms(BOUNCE_PROTECTION)
key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT)
lcd.init()
sensor.reset(dual_buff=True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(1)
sensor.set_vflip(0)
sensor.run(1)
anchor = (0.1606, 0.3562, 0.4712, 0.9568, 0.9877, 1.9108, 1.8761, 3.5310, 3.4423, 5.6823)
dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),
(81, 105)]
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
b = kpu.init_yolo2(task_fe, 0.5, 0.3, 5, anchor)
img_lcd = image.Image()
img_face = image.Image(size=(128, 128))
a = img_face.pix_to_ai()
ACCURACY = 75
while (1):
img = sensor.snapshot()
clock.tick()
code = kpu.run_yolo2(task, img)
if code:
totalRes = len(code)
for item in code:
a = img.draw_rectangle(item.rect())
face_cut = img.cut(item.x(), item.y(), item.w(), item.h())
face_cut_128 = face_cut.resize(128, 128)
a = face_cut_128.pix_to_ai()
confidence = float(item.value())
itemROL = item.rect()
classID = int(item.classid())
if confidence < 0.52:
_ = img.draw_rectangle(itemROL, color=color_B, tickness=5)
continue
if classID == 1 and confidence > 0.65:
_ = img.draw_rectangle(itemROL, color_G, tickness=5)
if totalRes == 1:
drawConfidenceText(img, (0, 0), 1, confidence)
else:
_ = img.draw_rectangle(itemROL, color=color_R, tickness=5)
if totalRes == 1:
drawConfidenceText(img, (0, 0), 0, confidence)
fmap = kpu.forward(task_fe, face_cut_128)
feature = kpu.face_encode(fmap[:])
reg_flag = False
scores = []
for j in range(len(record_ftrs)):
score = kpu.face_compare(record_ftrs[j], feature)
scores.append(score)
max_score = 0
index = 0
del (face_cut_128)
for k in range(len(scores)):
if max_score < scores[k]:
max_score = scores[k]
index = k
if max_score > ACCURACY:
a = img.draw_string(item.x(), item.y(), ("%s :%2.1f" % (
names[index], max_score)), color=(0, 255, 0), scale=2)
lvbo(3)
else:
a = img.draw_string(item.x(), item.y(), ("X :%2.1f" % (
max_score)), color=(255, 0, 0), scale=2)
lvbo(4)
if start_processing:
record_ftrs = []
record_ftr = feature
record_ftrs.append(record_ftr)
save_feature(record_ftr)
print(feature)
start_processing = False
break
lvbo(0)
fps = clock.fps()
img.draw_string(0, 200, "%2.1f fps" % fps, scale=2, color=(255, 0, 0))
a = lcd.display(img)
gc.collect()
b= img.draw_rectangle(80,22,159,195)