K210产品介绍
- 嘉楠勘智K210产品官网
- 矽速AI模组官网
-
Win10下某些版本可能打开错误,换其它版本或使用命令行版本即可。
MaixPy IDE
注:以下所有MaixPy代码均基于MaixHub默认配置固件+lodepng内置模块配置。
Maix M1W Dock自检例程 - MaixPy 暂停更新
1 | #------------------------------------------------------------------------------# |
Maix M1W Dock人脸识别例程 - MaixPy
注意烧录的MaixPy固件必须支持kmodel。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 import sensor
import image
import lcd
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
# Sipeed offical demo, flash to SPIFFS, see https://www.maixhub.com/modelInfo?modelId=14
#task_fd = kpu.load(0x300000)
#task_ld = kpu.load(0x400000)
#task_fe = kpu.load(0x500000)
# Put models to SD card, notice that the filename must be correct
task_fd = kpu.load("/sd/FaceDetection.smodel")
task_ld = kpu.load("/sd/FaceLandmarkDetection.smodel")
task_fe = kpu.load("/sd/FeatureExtraction.smodel")
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(1)
sensor.run(1)
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437,
6.92275, 6.718375, 9.01025) # anchor for face detect
dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),
(81, 105)] # standard face key point position
a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
img_lcd = image.Image()
img_face = image.Image(size=(128, 128))
a = img_face.pix_to_ai()
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']
ACCURACY = 85
while (1):
img = sensor.snapshot()
clock.tick()
code = kpu.run_yolo2(task_fd, img)
if code:
for i in code:
# Cut face and resize to 128x128
a = img.draw_rectangle(i.rect())
face_cut = img.cut(i.x(), i.y(), i.w(), i.h())
face_cut_128 = face_cut.resize(128, 128)
a = face_cut_128.pix_to_ai()
# a = img.draw_image(face_cut_128, (0,0))
# Landmark for face 5 points
fmap = kpu.forward(task_ld, face_cut_128)
plist = fmap[:]
le = (i.x() + int(plist[0] * i.w() - 10), i.y() + int(plist[1] * i.h()))
re = (i.x() + int(plist[2] * i.w()), i.y() + int(plist[3] * i.h()))
nose = (i.x() + int(plist[4] * i.w()), i.y() + int(plist[5] * i.h()))
lm = (i.x() + int(plist[6] * i.w()), i.y() + int(plist[7] * i.h()))
rm = (i.x() + int(plist[8] * i.w()), i.y() + int(plist[9] * i.h()))
a = img.draw_circle(le[0], le[1], 4)
a = img.draw_circle(re[0], re[1], 4)
a = img.draw_circle(nose[0], nose[1], 4)
a = img.draw_circle(lm[0], lm[1], 4)
a = img.draw_circle(rm[0], rm[1], 4)
# align face to standard position
src_point = [le, re, nose, lm, rm]
T = image.get_affine_transform(src_point, dst_point)
a = image.warp_affine_ai(img, img_face, T)
a = img_face.ai_to_pix()
# a = img.draw_image(img_face, (128,0))
del (face_cut_128)
# calculate face feature vector
fmap = kpu.forward(task_fe, img_face)
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
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(i.x(), i.y(), ("%s :%2.1f" % (
names[index], max_score)), color=(0, 255, 0), scale=2)
else:
a = img.draw_string(i.x(), i.y(), ("X :%2.1f" % (
max_score)), color=(255, 0, 0), scale=2)
if start_processing:
record_ftr = feature
record_ftrs.append(record_ftr)
start_processing = False
break
fps = clock.fps()
print("%2.1f fps" % fps)
a = lcd.display(img)
gc.collect()
# kpu.memtest()
# a = kpu.deinit(task_fe)
# a = kpu.deinit(task_ld)
# a = kpu.deinit(task_fd)
Kendeyte IDE
- Github
- Gitee
- Official - beta
-
下载解压后双击
KendryteIDE.bat
,首次运行会自动下载安装并配置环境。 - 嘉楠BBS
其它
错误集锦
- OSError: no available NIC
没有连接网络。
MaixPy内部定义视频尺寸
- (实测Camera只能用QVGA。)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33typedef enum {
FRAMESIZE_INVALID = 0,
// C/SIF Resolutions
FRAMESIZE_QQCIF, // 88x72
FRAMESIZE_QCIF, // 176x144
FRAMESIZE_CIF, // 352x288
FRAMESIZE_QQSIF, // 88x60
FRAMESIZE_QSIF, // 176x120
FRAMESIZE_SIF, // 352x240
// VGA Resolutions
FRAMESIZE_QQQQVGA, // 40x30
FRAMESIZE_QQQVGA, // 80x60
FRAMESIZE_QQVGA, // 160x120
FRAMESIZE_QVGA, // 320x240
FRAMESIZE_VGA, // 640x480
FRAMESIZE_HQQQVGA, // 60x40
FRAMESIZE_HQQVGA, // 120x80
FRAMESIZE_HQVGA, // 240x160
// FFT Resolutions
FRAMESIZE_64X32, // 64x32
FRAMESIZE_64X64, // 64x64
FRAMESIZE_128X64, // 128x64
FRAMESIZE_128X128, // 128x128
FRAMESIZE_240X240, // 240x240
// Other
FRAMESIZE_LCD, // 128x160
FRAMESIZE_QQVGA2, // 128x160
FRAMESIZE_WVGA, // 720x480
FRAMESIZE_WVGA2, // 752x480
FRAMESIZE_SVGA, // 800x600
FRAMESIZE_SXGA, // 1280x1024
FRAMESIZE_UXGA, // 1600x1200
} framesize_t;
ESPAT库
注:实际使用时http获取返回头会很慢,有待优化。
1 | from network_espat import wifi |
MQTT库
1 | from umqtt_simple import MQTTClient |
local variable xxx referenced before assignment
在Python中有一个经典错误:1
2local variable xxx referenced before assignment
#赋值前引用的局部变量xxx
这里引入两个概念:
局部变量指的在函数内部定义并使用的变量,它只在函数内部有效。
全局变量指的是能作用于函数内外的变量,即全局变量既可以在各个函数的外部使用,也可以在各函数内部使用。
- Test_1:
1 | def main(): |
1 | #NameError: name 'num' is not defined |
这里是提示num没有被定义,因为num属于在main函数内部的变量,在函数体外面是不会被调用,解释器也不会认识这个num变量,只有当main函数调用时,才可以获得其内部一些变量,这就是局部变量。
1 | def main(): |
使用global声明num为全局变量就可以了,但是应当注意,在项目中,定义全局变量会造成进程死锁。
- Test_2:
1 | num=10 |
1 | UnboundLocalError: local variable 'num' referenced before assignment |
这就是赋前引用局部变量,num在main函数外界定义一次,但在main函数内部又定义一次,python将优先处理在函数体内的变量,且调用在定义之前,所以在第三行print(num)报错,后面num变量赋值num=5让num=10失去全局效果。
- Test_3:
1 | global num |
声明num 为全局变量后,依然报错赋前引用局部变量,则就进一步说明在函数体内部自称一体,num为全局变量,只能保证在函数体内部认识这个num变量,但在函数体内部对num变量进行的改变,依然要保证先赋值后调用。
1 | global num |
1 | main:5 |
1 | global num |
1 | main:10 |
1 | num=10 |
1 | 10 |
可以通过三个例子进一步总结。
Python 命名空间查找顺序:
假设我们要使用变量 runoob,则 Python 的查找顺序为:局部的命名空间去 -> 全局命名空间 -> 内置命名空间。
如果找不到变量 runoob,它将放弃查找并引发一个 NameError 异常:NameError: name ‘runoob’ is not defined
。
在实际运用中:1
2
3
4
5
6
def index():
if request.method == 'POST':
text='你好'
#return render_template('index.html',text=text)
return render_template('index.html',text=text)
在flask中text传入前端,就会报错 local variable text referenced before assignment
,解决方案就是取消注释那一句,讲最后一句text删除。
实际上这个错误可以归结于变量作用域问题,参考文章:https://www.cnblogs.com/fireporsche/p/7813961.html
版权声明:本文为CSDN博主「菜鸟上路_lbz」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_44198436/article/details/100051651