(野火) 《Python应用开发实战指南 基于LubanCat RK系列板卡》 - 20230314
(野火) 《Python应用开发实战指南 基于LubanCat RK系列板卡》 - 20230314
LubanCat-RK
EmbedFire
2023 03 14
Python
Contents
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
LubanCat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 +Python= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 APT python pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 python pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4.1 pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4.2 apt pip . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4.3 setuptools . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 python IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.4 import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
论坛:https://www.firebbs.cn i 天猫:https://yehuosm.tmall.com
Python
3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.3.1 String( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.3.2 List( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3.3 Tuple( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5 GPIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.1 libgpiod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.3 python3-libgpiod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.3.1 python3-libgpiod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.3.2 libgpiod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.3.3 libgpiod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.4 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.4.1 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.4.2 periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.5 Adafruit Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.5.1 Adafruit Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
论坛:https://www.firebbs.cn ii 天猫:https://yehuosm.tmall.com
Python
5.5.3 Adafruit-Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6 PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.1 PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.3 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.3.1 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.3.2 periphery PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7 UART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.1.1 UART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.2 pyserial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.2.1 pyserial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.2.2 pyserial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.3 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7.3.1 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.3.2 periphery UART . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8 I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
8.1 I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
8.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.2.1 I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
8.3 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8.3.1 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8.3.2 periphery I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8.4 Adafruit Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
8.4.1 Adafruit Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
8.4.2 Adafruit-Blinka I2C . . . . . . . . . . . . . . . . . . . . . . . . . . 72
8.4.3 Adafruit_CircuitPython_SSD1306 . . . . . . . . . . . . . . . . . . . . . . . . 76
9 SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.1 SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.2.1 SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
9.3 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
9.3.1 python-periphery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
9.3.2 periphery SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
9.4 Adafruit Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9.4.1 Adafruit Blinka . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
9.4.2 Adafruit-Blinka SPI . . . . . . . . . . . . . . . . . . . . . . . . . . 85
10 -Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
10.1 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
10.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
10.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
10.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
10.2.3 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
10.2.4 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
10.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
10.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
10.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
10.4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
11 -Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
11.1 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
11.2.1 Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
11.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
11.2.3 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
11.2.4 Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
11.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
论坛:https://www.firebbs.cn iv 天猫:https://yehuosm.tmall.com
Python
11.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
11.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
12 -Opencv. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
12.1 opencv-python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
12.2 camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
12.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
12.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
12.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
12.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
13 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
13.1 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
13.2 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
13.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
13.3 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
13.4 Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
13.4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
13.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
14 PIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
14.1 PIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
14.2 PIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
14.3 PIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
15 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
15.1 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
15.2 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
15.3 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
16 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
16.1 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
16.2 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
16.3 Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
论坛:https://www.firebbs.cn v 天猫:https://yehuosm.tmall.com
Python
论坛:https://www.firebbs.cn vi 天猫:https://yehuosm.tmall.com
Python
20.2.1 Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
20.3 PyQt5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
20.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
20.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
20.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
20.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
20.4 Qt Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
20.4.1 UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
20.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
20.4.3 ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
20.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
21.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
21.2 RK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
21.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
22 NPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
22.1 RKNN-Toolkit2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
22.1.1 RKNN-Toolkit2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
22.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
22.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
22.2 RKNN Toolkit Lite2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
22.2.1 RKNN Toolkit Lite2 . . . . . . . . . . . . . . . . . . . . . . . . 178
22.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
22.3 rk356X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
22.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
23 PaddlePaddle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
23.1 PaddlePaddle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
23.2 PaddlePaddle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
23.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
23.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
23.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
23.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
23.3.4 ONNX . . . . . . . . . . . . . . . . . . . . . . . . . . 190
23.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
23.3.6 RKNN . . . . . . . . . . . . . . . . . . . . . . . . . . 194
23.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
23.4.1 RKNN Toolkit Lite2 . . . . . . . . . . . . . . . . . . . . . . . 196
23.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
23.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
23.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
24 PaddlePaddle FastDeploy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
24.1 FastDeploy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
24.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
24.2.1 PC . . . . . . . . . . . . . . . . . . . . . . . . . . 201
24.2.2 FastDeploy RKNPU2 . . . . . . . . . . . . . . . . . . . . 201
24.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
24.3.1 PicoDet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
24.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
25 TensorFlow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
25.1 tensorflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
25.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
25.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
25.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
25.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
25.2.4 TensorFlow Lite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
25.2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
25.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
25.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
25.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
26 ResNet18 PyTorch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
26.1 PyTorch ResNet18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
26.1.1 PyTorch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
论坛:https://www.firebbs.cn ix 天猫:https://yehuosm.tmall.com
Python
LubanCat
1 Linux Android
2
Linux
Python LubanCat_RK
Python
• python Python3
论坛:https://www.firebbs.cn 1 天猫:https://yehuosm.tmall.com
Python
LubanCat-RK
Linux LubanCat-RK
LubanCat-RK
Linux LubanCat_RK
LubanCat-RK Android
论坛:https://www.firebbs.cn/ 2 天猫:https://yehuosm.tmall.com
Python
STM32
• http://www.embedfire.com
• http://www.firebbs.cn
• github https://github.com/Embedfire
• gitee https://gitee.com/Embedfire
• https://yehuosm.tmall.com
• embedfire@embedfire.com
论坛:https://www.firebbs.cn 3 天猫:https://yehuosm.tmall.com
Python
• 0769-33894118
论坛:https://www.firebbs.cn/ 4 天猫:https://yehuosm.tmall.com
Python
1 Python
1.1 Python
Python
• Python
• Python AI
• Python
Python
• -Python
• Python
1.2
,
AI
论坛:https://www.firebbs.cn 5 天猫:https://yehuosm.tmall.com
Python
1.3 +Python=
PC Python
IO
DIY
Python
• AI
1.4
• python
• -Python
• Python
论坛:https://www.firebbs.cn/ 6 天猫:https://yehuosm.tmall.com
Python
2 Python
Python
#
# apt
sudo apt update
# python3
sudo apt -y install python3
# pip
sudo apt -y install python3-pip
: pip python
论坛:https://www.firebbs.cn 7 天猫:https://yehuosm.tmall.com
Python
2.2 Python
# python3
cat@lubancat:~$ python3 --version
#
Python 3.8.10
# python
cat@lubancat:~$ python2 --version
#
Python 2.7.18
# pip3
cat@lubancat:~$ pip3 --version
#
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
论坛:https://www.firebbs.cn/ 8 天猫:https://yehuosm.tmall.com
Python
# python python3
sudo ln -sf /usr/bin/python3 /usr/bin/python
# pip pip3
sudo ln -sf /usr/bin/pip3 /usr/bin/pip
# python
cat@lubancat:~$ which python
/usr/bin/python
# python
cat@lubancat:~$ python --version
#
Python 3.8.10
# pip
cat@lubancat:~$ pip --version
#
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
python3 pip3
论坛:https://www.firebbs.cn/ 9 天猫:https://yehuosm.tmall.com
Python
2.4
2.4.1 pip
Pypi
pip adafruit-circuitpython-ssd1306
# pip list
pip list | grep adafruit-circuitpython-ssd1306
# pip uninstall
sudo pip uninstall adafruit-circuitpython-ssd1306
pip
apt
apt pip
论坛:https://www.firebbs.cn/ 10 天猫:https://yehuosm.tmall.com
Python
2.4.3 setuptools
Python
pip apt
setuptools
#
sudo apt -y install python3-setuptools
setuptools
Github : jieba
Releases
setuptools
论坛:https://www.firebbs.cn/ 11 天猫:https://yehuosm.tmall.com
Python
#
git clone https://github.com/fxsjy/jieba.git
# jieba/
sudo python3 setup.py install
jieba
2.5
• Pypi
• debian
论坛:https://www.firebbs.cn/ 12 天猫:https://yehuosm.tmall.com
Python
3 Python
Python PC
3.1 python
python Python
Python
hello world!
exit()
#
python3
#
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
#
>>> print("hello world!")
#
hello world!
>>>
#
>>> 8+2
#
( )
论坛:https://www.firebbs.cn 13 天猫:https://yehuosm.tmall.com
Python
( )
10
# quit() Ctrl+D
>>> exit()
cat@lubancat:~$
# ipython
sudo pip3 install ipython
#
cat@lubancat:~$ ipython
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
# 8+2
In [1]: 8+2
Out[1]: 10
In [2]:
# exit
论坛:https://www.firebbs.cn/ 14 天猫:https://yehuosm.tmall.com
Python
3.2 python
Python hello.py
hello.py
1: base/hello.py
print("Hello LubanCat!")
# hello.py
# ls
ls
#
hello.py
#
python3 hello.py
#
Hello LubanCat!
VS Code remote
论坛:https://www.firebbs.cn/ 15 天猫:https://yehuosm.tmall.com
Python
论坛:https://www.firebbs.cn/ 16 天猫:https://yehuosm.tmall.com
Python
2: base/hello.py
#!/usr/bin/python
print("Hello LubanCat!")
#
cat@lubancat:~$ ./hello.py
Hello LubanCat!
cat@lubancat:~$
# apt
sudo apt install thonny
( xfce4 )
论坛:https://www.firebbs.cn/ 17 天猫:https://yehuosm.tmall.com
Python
论坛:https://www.firebbs.cn/ 18 天猫:https://yehuosm.tmall.com
Python
论坛:https://www.firebbs.cn/ 19 天猫:https://yehuosm.tmall.com
Python
3.4 import
Python
get_info.py
3: base/get_info.py
# psutil
import psutil
import datetime
#
print(" :", datetime.datetime.fromtimestamp(psutil.boot_time()).
,→ strftime("%Y-%m-%d %H: %M: %S"))
print("\n----------- -------------------")
mem = psutil.virtual_memory()
print(" :", mem)
mem_total = float(mem.total)
mem_used = float(mem.used)
mem_free = float(mem.free)
mem_percent = float(mem.percent)
print(f" {mem_total}")
print(f" {mem_used}")
print(f" {mem_free}")
print(f" {mem_percent}")
print("\n-----------CPU -------------------")
( )
论坛:https://www.firebbs.cn/ 20 天猫:https://yehuosm.tmall.com
Python
( )
print("CPU :", psutil.cpu_times())
print("cpu :", psutil.cpu_count())
print("cpu :", psutil.cpu_freq())
get_info.py
#
python3 get_info.py
#
: [suser(name='cat', terminal='ttyFIQ0', host='', started=1662631296.
,→ 0, pid=742)]
: 2022-11-01 16: 28: 20
----------- -------------------
: svmem(total=2059280384, available=892481536, percent=56.7,␣
,→ used=1123770368, free=30068736, active=1374724096, inactive=373829632,␣
,→ buffers=83066880, cached=822374400, shared=17391616, slab=232140800)
2059280384.0
1123770368.0
30068736.0
56.7
-----------CPU -------------------
CPU : scputimes(user=2365.82, nice=3.25, system=765.33, idle=335360.
,→ 41, iowait=79.58, irq=0.0, softirq=50.22, steal=0.0, guest=0.0, guest_
,→ nice=0.0)
( )
论坛:https://www.firebbs.cn/ 21 天猫:https://yehuosm.tmall.com
Python
( )
cpu : 4
cpu : scpufreq(current=1992.0, min=408.0, max=1992.0)
requests
# pip
sudo pip3 install requests
# apt
sudo apt -y install python3-requests
debian apt
apt , pip apt
Thonny ->
论坛:https://www.firebbs.cn/ 22 天猫:https://yehuosm.tmall.com
Python
3.5
• Python
论坛:https://www.firebbs.cn/ 23 天猫:https://yehuosm.tmall.com
Python
4 Python
Python , Python
4.1
1 #
2
3 '''
4
5 '''
6
7 """
8
9 """
10 print ("Hello!")
4.2
python
1 #for
2 for i in range(5):
( )
论坛:https://www.firebbs.cn 24 天猫:https://yehuosm.tmall.com
Python
( )
3 print("in")
4 print("out")
5
6 #
7 in
8 in
9 in
10 in
11 in
12 out
4.3
Python
1 a = 10 #
2 print(type(a))
3 a = 10.0 #
4 print(type(a))
5 a = "test" #
6 print(type(a))
1 #
2 <class 'int'>
3 <class 'float'>
4 <class 'str'>
论坛:https://www.firebbs.cn/ 25 天猫:https://yehuosm.tmall.com
Python
4.3.1 String( )
1 #!/usr/bin/python3
2 str0 = 'Python'
3
4 '''
5
6 +---+---+---+---+---+---+
7 | P | y | t | h | o | n |
8 +---+---+---+---+---+---+
9 0 1 2 3 4 5 ( )
10 -6 -5 -4 -3 -2 -1 ( )
11 : 1 2 3 4 5 : ( )
12 : -5 -4 -3 -2 -1 : ( )
13 '''
14 print (str0) # str0
15 print (str0[0]) #
16 print (str0[-6]) #
17 print (str0[:]) # str0
18 print (str0[2:]) #
19 print (str0[:-2]) #
20 print (str0 * 2) # print (2 * str0)
21 print (str0 + "TEST") #
论坛:https://www.firebbs.cn/ 26 天猫:https://yehuosm.tmall.com
Python
4.3.2 List( )
[ ]
0 1
1 #!/usr/bin/python3
2
6 print (list0) #
7 print (list0[0]) #
8 print (list[1:3]) #
9 print (list[2:]) #
10 print (tinylist * 2) #
11 print (list0 + list1) #
4.3.3 Tuple( )
Python ()
[]
1 #!/usr/bin/python3
2
论坛:https://www.firebbs.cn/ 27 天猫:https://yehuosm.tmall.com
Python
4.4
4.4.1
1 #!/usr/bin/python3
2
3 #
4 number = 20
5 guess = 0
6 print(" !")
7 while guess != number:
8 guess = int(input(" (0-100) "))
9
10 if guess == number:
11 print(" ")
12 elif guess < number:
13 print(" ...")
14 elif guess > number:
15 print(" ...")
4.4.2
1 #!/usr/bin/env python3
2
3 # while
4 n = 100
5 sum = 0
( )
论坛:https://www.firebbs.cn/ 28 天猫:https://yehuosm.tmall.com
Python
( )
6 counter = 1
7 while counter <= n:
8 sum = sum + counter
9 counter += 1
10
13 # range()
14 for i in range(4):
15 print(i)
4.4.3
1 #!/usr/bin/python3
2
3 list=[1,2,3,4]
4 it = iter(list) # for
5 for x in it:
6 print (x, end=" ")
4.5
Python print()
Python def
论坛:https://www.firebbs.cn/ 29 天猫:https://yehuosm.tmall.com
Python
def :
1 #!/usr/bin/python3
2 # my_function
3 def my_function(test):
4 print("Hello!"+ test)
5
6 # my_function
7 my_function(' Python')
4.6
4.6.1
Python
import
1 #!/usr/bin/python3
2
7 # from import
8 from periphery import I2C
论坛:https://www.firebbs.cn/ 30 天猫:https://yehuosm.tmall.com
Python
4.6.2
Python
A.B A B
python
4.7
Python
A
1.0 B 2.0 1.0 2.0
Python Python
1 #
2 cat@lubancat:~$ sudo apt-get install python3-venv
3
8 cat@lubancat:~/project-test$ ls -al
9 total 12
10 drwxr-xr-x 3 cat cat 4096 Dec 7 10:38 .
11 drwxr-xr-x 17 cat cat 4096 Dec 7 10:38 ..
( )
论坛:https://www.firebbs.cn/ 31 天猫:https://yehuosm.tmall.com
Python
( )
12 drwxr-xr-x 6 cat cat 4096 Dec 7 10:38 .test_env
13 cat@lubancat:~/project-test$
14
15 #
16 cat@lubancat:~/project-test$ source .test_env/bin/activate
17 (.test_env) cat@lubancat:~/project-test$
18 (.test_env) cat@lubancat:~/project-test$ pip list
19 Package Version
20 ------------- -------
21 pip 18.1
22 pkg-resources 0.0.0
23 setuptools 40.8.0
24 (.test_env) cat@lubancat:~/project-test$
25
26 #
27 (.test_env) cat@lubancat:~/project-test$ pip3 freeze > requirements.txt
28 #
29 (.test_env) cat@lubancat:~/project-test$ pip3 install -r requirements.txt
30
31 #
32 (.test_env) cat@lubancat:~/project-test$ deactivate
33 cat@lubancat:~/project-test$
4.8
https://docs.python.org/3.8/tutorial/index.html
https://www.runoob.com/python3/python3-basic-syntax.html
论坛:https://www.firebbs.cn/ 32 天猫:https://yehuosm.tmall.com
Python
5 GPIO
Python PC
Python GPIO PWM ADC I2C SPI PC
Python
5.1 libgpiod
#
sudo gpioinfo
( )
论坛:https://www.firebbs.cn 33 天猫:https://yehuosm.tmall.com
Python
( )
#
sudo apt -y install gpiod libgpiod-dev
# gpioinfo
gpiochip0 - 32 lines:
line 0: unnamed unused input active-high
line 1: unnamed unused input active-high
line 2: unnamed unused input active-high
line 3: unnamed unused input active-high
line 4: unnamed unused input active-high
line 5: unnamed "headset_gpio" input active-high [used]
# ...
libgpiod libgpiod IO
5.2
GPIO
GPIO LubanCat_RK
LubanCat-RK -40pin
root
sudo root
论坛:https://www.firebbs.cn/ 34 天猫:https://yehuosm.tmall.com
Python
5.3 python3-libgpiod
5.3.1 python3-libgpiod
#
sudo apt -y install python3-libgpiod
# python
python3
import gpiod
help(gpiod)
#
NAME
gpiod - Python bindings for libgpiod.
DESCRIPTION
This module wraps the native C API of libgpiod in a set of python␣
,→ classes.
# ...
5.3.2 libgpiod
python3-libgpiod GPIO
论坛:https://www.firebbs.cn/ 35 天猫:https://yehuosm.tmall.com
Python
1: io/gpio/libgpiod_io0.py
1 import time
2 import gpiod
3
9 gpio0_b0 = chip0.get_line(LINE_OFFSET)
10 gpio0_b0.request(consumer="gpio", type=gpiod.LINE_REQ_DIR_OUT, default_
,→ vals=[0])
11
12 print(gpio0_b0.consumer())
13
14 try:
15 while True:
16 gpio0_b0.set_value(1)
17 time.sleep(0.5)
18 gpio0_b0.set_value(0)
19 time.sleep(0.5)
20 finally:
21 gpio0_b0.set_value(1)
22 gpio0_b0.release()
• 9 chip0 line8
• 10 gpio
论坛:https://www.firebbs.cn/ 36 天猫:https://yehuosm.tmall.com
Python
LubanCat 2 (chip,line)=(0 8)
5.3.2.1
LubanCat 2
# python3-libgpiod
# blink.py root
python3 blink.py
# LED
gpioinfo gpio 0 8
论坛:https://www.firebbs.cn/ 37 天猫:https://yehuosm.tmall.com
Python
5.3.3 libgpiod
python3-libgpiod GPIO
2: io/gpio/libgpiod_io1.py
1 import gpiod
2
12 led = chip0_led.get_line(LED_LINE_OFFSET)
13 led.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0])
14
15 button = chip0_button.get_line(BUTTON_LINE_OFFSET)
16 button.request(consumer="BUTTON", type=gpiod.LINE_REQ_DIR_IN)
17
18 print(led.consumer())
19 print(button.consumer())
20
21 try:
22 while True:
23 led.set_value(button.get_value())
24 finally:
25 led.set_value(1)
26 led.release()
27 button.release()
论坛:https://www.firebbs.cn/ 38 天猫:https://yehuosm.tmall.com
Python
• 12 LED GPIO
• 15 GPIO
• 23 LED
5.4 python-periphery
5.4.1 python-periphery
python-periphery
#
sudo pip3 install python-periphery
5.4.2 periphery
python-periphery
3: io/gpio/periphery_io.py
( )
论坛:https://www.firebbs.cn/ 39 天猫:https://yehuosm.tmall.com
Python
( )
8 BUTTON_CHIP = "/dev/gpiochip0"
9 BUTTON_LINE_OFFSET = 18
10
14 try:
15 while True:
16 led.write(button.read())
17 finally:
18 led.write(True)
19 led.close()
20 button.close()
• 16 LED
gpioinfo
论坛:https://www.firebbs.cn/ 40 天猫:https://yehuosm.tmall.com
Python
Adafruit Blinka
#
sudo apt -y install python3-libgpiod #
sudo pip3 install Adafruit-Blinka
论坛:https://www.firebbs.cn/ 41 天猫:https://yehuosm.tmall.com
Python
5.5.2
RK Adafruit-Blinka Adafruit-Blinka
Adafruit-Blinka
Adafruit-Blinka
1. python3 python3
5. board. Tab
lubancat-zero :
LubanCat 1
LubanCat 2
论坛:https://www.firebbs.cn/ 42 天猫:https://yehuosm.tmall.com
Python
Adafruit-Blinka
5.5.3 Adafruit-Blinka
4: io/gpio/digital_io.py
1 import board
2 import digitalio
3
4 # GPIO
5 # LubanCat 1 board , board.GPIO11 40Ppin 11
board.GPIO11 = GPIO3_A5 = Pin 101 ( )
论坛:https://www.firebbs.cn/ 43 天猫:https://yehuosm.tmall.com
Python
( )
6 led = digitalio.DigitalInOut(board.GPIO11)
7 led.direction = digitalio.Direction.OUTPUT
8
9 try:
10 while True:
11 led.value=1
12 time.sleep(0.5)
13 led.value=0
14 time.sleep(0.5)
15
16 finally:
17 led.value = True
18 led.deinit()
• 6~7 board.GPIO11
• 9-14
LED LED
python-periphery
Adafruit Blinka
论坛:https://www.firebbs.cn/ 44 天猫:https://yehuosm.tmall.com
Python
5.6
• CircuitPython
• python-periphery
• Adafruit Blinka
论坛:https://www.firebbs.cn/ 45 天猫:https://yehuosm.tmall.com
Python
6 PWM
python3-libgpiod python-periphery Python GPIO
Python Python
LED
PWM
6.1 PWM
GPIO LED
GPIO
PWM
PWM LED
PWM LED LED
LubanCat 2 LED
PWM PWM
PWM Python
论坛:https://www.firebbs.cn 46 天猫:https://yehuosm.tmall.com
Python
6.2
GPIO
GPIO LubanCat_RK
LubanCat-RK -40pin
Permission denied
root sudo root
pwm LubanCat 2
# PWM
ls /sys/class/pwm/
论坛:https://www.firebbs.cn/ 47 天猫:https://yehuosm.tmall.com
Python
PWM
6.3 python-periphery
PWM PWM
6.3.1 python-periphery
python-periphery
#
sudo pip3 install python-periphery
论坛:https://www.firebbs.cn/ 48 天猫:https://yehuosm.tmall.com
Python
python-periphery PWM
1: io/pwm/pwm_test_periphery.py
4 try:
5 #
6 step = 0.05
7 # range
8 rangeMax = int(1/0.05)
9 # PWM 8, channel 0 , PWM8
10 pwm = PWM(1, 0)
11 # PWM 1 kHz
12 pwm.frequency = 1e3
13 # 0%
14 pwm.duty_cycle = 0.00
15 # PWM
16 pwm.enable()
17 while True:
18 for i in range(0,rangeMax):
19 # step
20 time.sleep(step)
21 # step% , round
22 pwm.duty_cycle = round(pwm.duty_cycle+step,2)
23 # 1
24 if pwm.duty_cycle == 0.0:
25 time.sleep(1)
26 for i in range(0,rangeMax):
( )
论坛:https://www.firebbs.cn/ 49 天猫:https://yehuosm.tmall.com
Python
( )
27 time.sleep(step)
28 pwm.duty_cycle = round(pwm.duty_cycle-step,2)
29 except:
30 print("Some errors occur!\n")
31 finally:
32 # LED
33 pwm.duty_cycle = 0.0
34 #
35 pwm.close()
• 10 PWM
• 12 14 PWM PWM
• 16 PWM
6.3.2.1
LubanCat 2
# python-periphery
# PWM pwm
# pwm_test.py
python3 pwm_test.py
论坛:https://www.firebbs.cn/ 50 天猫:https://yehuosm.tmall.com
Python
root
# root root
su root
LED LED
论坛:https://www.firebbs.cn/ 51 天猫:https://yehuosm.tmall.com
Python
7 UART
UART
UART
UART UART
Python USB
TTL
7.1
7.1.1 UART
GPIO
, LubanCat_RK LubanCat-RK -40pin
LubanCat 2 40 UART3
UART3
论坛:https://www.firebbs.cn 52 天猫:https://yehuosm.tmall.com
Python
( )
sudo vim /boot/uEnv/uEnvLubanCat 2.txt
# pwm
RK
# UART
ls /dev/ttyS*
UART
/dev/ttyS3 uart3
论坛:https://www.firebbs.cn/ 53 天猫:https://yehuosm.tmall.com
Python
7.1.2
USB TTL
7.2 pyserial
pyserial
pyserial
Linux UART
论坛:https://www.firebbs.cn/ 54 天猫:https://yehuosm.tmall.com
Python
7.2.1 pyserial
pyserial
#
sudo pip3 install pyserial
7.2.2 pyserial
1: io/uart/uart_test0.py
4 # uart3 115200 8 1
3s
5 with serial.Serial(
6 "/dev/ttyS3",
7 baudrate=115200,
8 bytesize=serial.EIGHTBITS,
9 stopbits=serial.STOPBITS_ONE,
10 parity=serial.PARITY_NONE,
11 timeout=3,
12 ) as uart3:
13 # "Hello World!\n"
14 uart3.write(b"Hello World!\n")
15
16 #
128 1 ( )
论坛:https://www.firebbs.cn/ 55 天猫:https://yehuosm.tmall.com
Python
( )
17 buf = uart3.read(128)
18
19 # Python bytes
20 #
21 print(" :\n", buf)
22 # gbk
23 data_strings = buf.decode("gbk")
24 #
25 print(" {:d} , :\n {:s}".format(len(buf), data_
,→ strings))
7.2.2.1
论坛:https://www.firebbs.cn/ 56 天猫:https://yehuosm.tmall.com
Python
1 2
# pyserial
# UART
# uart_test.py
python3 uart_test.py
# Hello World!\n
3 4
论坛:https://www.firebbs.cn/ 57 天猫:https://yehuosm.tmall.com
Python
pyserial API
7.3 python-periphery
论坛:https://www.firebbs.cn/ 58 天猫:https://yehuosm.tmall.com
Python
7.3.1 python-periphery
python-periphery
#
sudo pip3 install python-periphery
python-periphery UART
2: io/uart/uart_test1.py
4 try:
5 # /dev/ttyS3 115200 8
1
6 serial = Serial(
7 "/dev/ttyS3",
8 baudrate=115200,
9 databits=8,
10 parity="none",
11 stopbits=1,
12 xonxoff=False,
13 rtscts=False,
14 )
15 # "python-periphery!\n"
( )
论坛:https://www.firebbs.cn/ 59 天猫:https://yehuosm.tmall.com
Python
( )
16 serial.write(b"python-periphery!\n")
17
18 # 128
1
19 buf = serial.read(128, 1)
20
21 # Python bytes
22 #
23 print(" :\n", buf)
24
25 # gbk
26 data_strings = buf.decode("gbk")
27
28 #
29 print(" {:d} , :\n {:s}".format(len(buf), data_
,→ strings))
30 finally:
31 #
32 serial.close()
• 6 UART uart3
• 16
• 19
• 23~29
• 32 UART uart3
论坛:https://www.firebbs.cn/ 60 天猫:https://yehuosm.tmall.com
Python
7.3.2.1
LubanCat 2
# python-periphery
# UART
# uart_test.py
sudo python3 uart_test1.py
# Hello World!\n
3 4
论坛:https://www.firebbs.cn/ 61 天猫:https://yehuosm.tmall.com
Python
论坛:https://www.firebbs.cn/ 62 天猫:https://yehuosm.tmall.com
Python
8 I2C
I2C IIC
MPU6050 MLX90640
0.96 OLED SSD1306 I2C
I2C I2C
8.1 I2C
I2C Python
Demo API
论坛:https://www.firebbs.cn 63 天猫:https://yehuosm.tmall.com
Python
8.2
8.2.1 I2C
I2C
论坛:https://www.firebbs.cn/ 64 天猫:https://yehuosm.tmall.com
Python
# I2C
ls -l /dev/i2c-*
I2C
i2c I2C
8.2.2
LubanCat 2 I2C
0.96 OLED SSD1306 I2c3
VCC • 3.3V
论坛:https://www.firebbs.cn/ 65 天猫:https://yehuosm.tmall.com
Python
8.3 python-periphery
8.3.1 python-periphery
python-periphery
#
sudo pip3 install python-periphery
python-periphery I2C
3 # i2c1
4 i2c = I2C("/dev/i2c-1")
5
论坛:https://www.firebbs.cn/ 66 天猫:https://yehuosm.tmall.com
Python
( )
8 # I2C
9 i2c.transfer(0x50, msgs)
10 #
11 print("0x100: 0x{:02x}".format(msgs[1].data[0]))
12 # I2C
13 i2c.close()
• 4 I2C I2C1
• 7 I2C.Message I2C.Message
I2C
read=True I2C
data
• 9 I2C I2C
• 17 I2C I2C1
2: io/i2c/i2c_test_periphery.py
5 # i2c-3
6 i2c = I2C("/dev/i2c-3")
7
8 # 0x3c OLED
( )
论坛:https://www.firebbs.cn/ 67 天猫:https://yehuosm.tmall.com
Python
( )
9 I2CSLAVEADDR = 0x3C
10
11 # periphery i2c
12 def i2c_read_reg(devregaddr):
13 """
14 periphery i2c
15 """
16 #
17 # Message
18 # Message read=True
19 msgs = [I2C.Message([devregaddr]), I2C.Message([0x00], read=True)]
20 # i2cSlaveAddr
21 i2c.transfer(I2CSLAVEADDR, msgs)
22 print(" 0x{:02x} : 0x{:02x}".format(devregaddr, msgs[1].
,→ data[0]))
23
24
25 def oled_write_cmd(cmd):
26 """
27 periphery i2c
28 """
29 msgs = [I2C.Message([0x00, cmd])]
30 i2c.transfer(I2CSLAVEADDR, msgs)
31
32
33 def oled_write_data(data):
34 """
35 periphery i2c
36 """
37 msgs = [I2C.Message([0x40, data])]
38 i2c.transfer(I2CSLAVEADDR, msgs)
39
( )
论坛:https://www.firebbs.cn/ 68 天猫:https://yehuosm.tmall.com
Python
( )
40
41 def oled_init():
42 """
43 OLED 0.96 OLED
44 """
45 time.sleep(1)
46 oled_write_cmd(0xAE)
47 oled_write_cmd(0x20)
48 oled_write_cmd(0x10)
49 oled_write_cmd(0xB0)
50 oled_write_cmd(0xC8)
51 oled_write_cmd(0x00)
52 oled_write_cmd(0x10)
53 oled_write_cmd(0x40)
54 oled_write_cmd(0x81)
55 oled_write_cmd(0xFF)
56 oled_write_cmd(0xA1)
57 oled_write_cmd(0xA6)
58 oled_write_cmd(0xA8)
59 oled_write_cmd(0x3F)
60 oled_write_cmd(0xA4)
61 oled_write_cmd(0xD3)
62 oled_write_cmd(0x00)
63 oled_write_cmd(0xD5)
64 oled_write_cmd(0xF0)
65 oled_write_cmd(0xD9)
66 oled_write_cmd(0x22)
67 oled_write_cmd(0xDA)
68 oled_write_cmd(0x12)
69 oled_write_cmd(0xDB)
70 oled_write_cmd(0x20)
71 oled_write_cmd(0x8D)
( )
论坛:https://www.firebbs.cn/ 69 天猫:https://yehuosm.tmall.com
Python
( )
72 oled_write_cmd(0x14)
73 oled_write_cmd(0xAF)
74
75
76 def oled_fill(filldata):
77 """
78 OLED
79 """
80 for i in range(8):
81 oled_write_cmd(0xB0 + i)
82 # page0-page1
83 oled_write_cmd(0x00)
84 # low column start address
85 oled_write_cmd(0x10)
86 # high column start address
87 for j in range(128):
88 oled_write_data(filldata)
89
90
91 #
92 try:
93 # OLED SSD1306
94 oled_init()
95 # OLED
96 oled_fill(0xFF)
97 #
98 i2c_read_reg(0x10)
99 finally:
100 print(" ")
101 #
102 i2c.close()
论坛:https://www.firebbs.cn/ 70 天猫:https://yehuosm.tmall.com
Python
8.3.2.1
LubanCat 2
# i2c_test.py
sudo python3 i2c_test_periphery.py
# OLED
论坛:https://www.firebbs.cn/ 71 天猫:https://yehuosm.tmall.com
Python
Adafruit Blinka
#
sudo apt -y install python3-libgpiod python-periphery #
sudo pip3 install Adafruit-Blinka
Adafruit Blinka
1 import busio
2 from board import *
3
4 # I2C I2C1
5 i2c = busio.I2C(SCL, SDA)
6 # I2C1
7 print(i2c.scan())
( )
论坛:https://www.firebbs.cn/ 72 天猫:https://yehuosm.tmall.com
Python
( )
8 # I2C
9 i2c.deinit()
4: io/i2c/i2c_test_adafruit.py
5 # OLED
6 OledInitBuf = bytes(
7 [
8 0xAE,
9 0x20,
10 0x10,
11 0xB0,
12 0xC8,
13 0x00,
14 0x10,
15 0x40,
16 0x81,
17 0xFF,
18 0xA1,
19 0xA6,
20 0xA8,
21 0x3F,
22 0xA4,
23 0xD3,
( )
论坛:https://www.firebbs.cn/ 73 天猫:https://yehuosm.tmall.com
Python
( )
24 0x00,
25 0xD5,
26 0xF0,
27 0xD9,
28 0x22,
29 0xDA,
30 0x12,
31 0xDB,
32 0x20,
33 0x8D,
34 0x14,
35 0xAF,
36 ]
37 )
38
39 #
40 OutBuffer = bytearray(1)
41 InBuffer = bytearray(1)
42
43 try:
44 # i2c
45 i2c = busio.I2C(board.I2C3_SCL, board.I2C3_SDA)
46 # I2C
47 print(" I2C I2C ")
48 for i in i2c.scan():
49 print("0x%02x " % i)
50
论坛:https://www.firebbs.cn/ 74 天猫:https://yehuosm.tmall.com
Python
( )
56 #
57
65 # IIC (readfrom_into)
66 i2c.readfrom_into(0x3C, InBuffer)
67 #
68 print("(readfrom_into) 0x%02x" % InBuffer[0])
69
70 # OLED
71 for i in range(8):
72 i2c.writeto(0x3C, bytearray([0x00, 0xB0 + i])) # page0-page1
73 i2c.writeto(0x3C, bytearray([0x00, 0x00])) # low column start␣
,→ address
74 i2c.writeto(0x3C, bytearray([0x00, 0x10])) # high column start␣
,→ address
75 for j in range(128):
76 i2c.writeto(0x3C, bytearray([0x40, 0xFF]))
77
78 finally:
79 # i2c
80 i2c.deinit()
• 45 I2C I2C1
• 53 OledInitBuf I2C1
论坛:https://www.firebbs.cn/ 75 天猫:https://yehuosm.tmall.com
Python
0x3C
• 66 InBuffer
• 80 I2C I2C1
Adafruit Blinka
8.4.3 Adafruit_CircuitPython_SSD1306
Blinka CircuitPython
Demo 0.96 OLED
SSD1306 Demo
论坛:https://www.firebbs.cn/ 76 天猫:https://yehuosm.tmall.com
Python
# root
pip3 install adafruit-circuitpython-ssd1306
#
sudo pip3 install adafruit-circuitpython-ssd1306
Demo Demo
Demo
论坛:https://www.firebbs.cn/ 77 天猫:https://yehuosm.tmall.com
Python
Demo
python3 ssd1306_stats.py
论坛:https://www.firebbs.cn/ 78 天猫:https://yehuosm.tmall.com
Python
论坛:https://www.firebbs.cn/ 79 天猫:https://yehuosm.tmall.com
Python
9 SPI
I2C
SPI SPI
SPI
SPI
LCD AD nRF SPI-FLASH
SPI SPI
python-periphery SPI
9.1 SPI
SPI
SPI MOSI MISO
9.2
9.2.1 SPI
论坛:https://www.firebbs.cn 80 天猫:https://yehuosm.tmall.com
Python
SPI
# SPI
ls -l /dev/spi*
I2C
论坛:https://www.firebbs.cn/ 81 天猫:https://yehuosm.tmall.com
Python
9.2.2
LubanCat 2
9.3 python-periphery
论坛:https://www.firebbs.cn/ 82 天猫:https://yehuosm.tmall.com
Python
9.3.1 python-periphery
python-periphery
#
sudo pip3 install python-periphery
python-periphery SPI
1: io/spi/spi_test_periphery.py
4 #
5 data_out = [0xAA, 0xBB, 0xCC, 0xDD]
6
7 try:
8 # SPI spidev3.0 SPI 0
1MHz
9 spi = SPI("/dev/spidev3.0", 0, 1000000)
10
11 # data_in
12 data_in = spi.transfer(data_out)
13
14 #
15 print(" : [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".
,→ format(*data_out)) ( )
论坛:https://www.firebbs.cn/ 83 天猫:https://yehuosm.tmall.com
Python
( )
16 print(" : [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".
,→ format(*data_in))
17 finally:
18 # SPI
19 spi.close()
• 5 SPI SPI3
• 9
• 12 SPI SPI
• 19 SPI SPI3
9.3.2.1
LubanCat 2
# spi_test.py
sudo python3 spi_test.py
论坛:https://www.firebbs.cn/ 84 天猫:https://yehuosm.tmall.com
Python
Adafruit Blinka
#
sudo apt -y install python3-libgpiod python-periphery #
sudo pip3 install Adafruit-Blinka
Adafruit Blinka
2: io/spi/spi_test_adafruit.py
5 #
6 OutBuffer = [0xAA, 0xBB, 0xCC, 0xDD]
7 InBuffer = bytearray(4)
8
9 try:
( )
论坛:https://www.firebbs.cn/ 85 天猫:https://yehuosm.tmall.com
Python
( )
10 # spi
11 spi = busio.SPI(SCLK, MOSI, MISO)
12
13 # SPI
14 spi.try_lock()
15 # SPI 1MHz 0 0 8
16 spi.configure(1000000, 0, 0, 8)
17 #
18 spi.unlock()
19
20 # SPI OutBuffer
InBuffer
21 spi.write_readinto(OutBuffer, InBuffer)
22
23 print(
24 "(write_readinto) : [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x
,→ {:02x}]".format(
25 *InBuffer
26 )
27 )
28
29 # SPI
30 spi.write(OutBuffer)
31 print(
32 "(OutBuffer) : [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".
,→ format(*OutBuffer)
33 )
34
35 InBuffer = [0, 0, 0, 0]
36 # SPI
37 spi.readinto(InBuffer)
38 print(
( )
论坛:https://www.firebbs.cn/ 86 天猫:https://yehuosm.tmall.com
Python
( )
39 "(readinto) : [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".
,→ format(*InBuffer)
40 )
41 finally:
42 # spi
43 spi.deinit()
• 11 SPI SPI3
• 14~18 SPI3
• 30 SPI3
• 37 SPI3
io/spi/ssd1306_spi_status.py spi
i2c Adafruit_CircuitPython_SSD1306
论坛:https://www.firebbs.cn/ 87 天猫:https://yehuosm.tmall.com
Python
10 -Pygame
HDMI MIPI DSI
10.1 Pygame
10.2
10.2.1
#
cat@lubancat:~$ ls /dev/dri/card*
/dev/dri/card0 /dev/dri/card1
#
cat@lubancat:~/lcd$ ls /dev/fb*
/dev/fb0
LubanCat 2 ubuntu
论坛:https://www.firebbs.cn 88 天猫:https://yehuosm.tmall.com
Python
rk3568-LubanCat 2.dtb
# boot
cd /boot
# mipi
ln -sf dtb/rk3568-LubanCat 2-mipi.dtb rk-kernel.dtb
# mipi
sudo reboot
10.2.2
, mipi
5.5 LubanCat 2
10.2.3 Pygame
apt
论坛:https://www.firebbs.cn/ 89 天猫:https://yehuosm.tmall.com
Python
# Pygame
sudo apt -y install python3-pygame
#
10.2.4 Pygame
Pygame
/dev/fb0
#
cat /dev/urandom > /dev/fb0
framebuffer
10.3
1: io/lcd/lcd_test1.py
7 #
( )
论坛:https://www.firebbs.cn/ 90 天猫:https://yehuosm.tmall.com
Python
( )
8 os.environ["SDL_NOMOUSE"] = "1"
9
10 #
11 pygame.display.init()
12
13 #
14 screen = pygame.display.set_mode((448,418))
15
16 #
17 pygame.display.set_caption("test")
18
19 # pygame image
20 ball = pygame.image.load("test.png")
21
22 #
23 screen.blit(ball,(0,0))
24
25 #
26 pygame.display.flip()
27
28 time.sleep(5)
29 sys.exit()
mipi :
论坛:https://www.firebbs.cn/ 91 天猫:https://yehuosm.tmall.com
Python
10.3.1
Lcd
# sudo
sudo python3 lcd_test1.py
10.4
2: io/lcd/lcd_test2.py
论坛:https://www.firebbs.cn/ 92 天猫:https://yehuosm.tmall.com
Python
( )
4 import time
5 import pygame
6
7 class PyScope:
8 """ PyScope """
9
10 screen = None
11
12 def __init__(self):
13 "PyScope framebuffer pygame "
14
15 #
16 #os.environ["SDL_NOMOUSE"] = "1"
17 pygame.display.init()
18
19 # , pygame
20 self.screen = pygame.display.set_mode((600,600))
21
22 #
23 pygame.display.set_caption('lcd_test2.py')
24
25 #
26 self.screen.fill((156,156,156))
27
28 #
29 pygame.font.init()
30
31 #
32 font=pygame.font.Font(None,32)
33 text=font.render("Hello!",True,(255,255,255))
34 self.screen.blit(text,(100,100))
35
( )
论坛:https://www.firebbs.cn/ 93 天猫:https://yehuosm.tmall.com
Python
( )
36 #
37 text=font.render("Display Test!",True,(255,0,0))
38 self.screen.blit(text,(100,150))
39
40 #
41 pygame.display.flip()
42
43 def __del__(self):
44 " pygame "
45 pygame.display.quit()
46
47 def test(self):
48 while True:
49 #
50 for event in pygame.event.get():
51 #
52 if event.type == pygame.QUIT:
53 #
54 pygame.quit()
55 #
56 sys.exit()
57 #
58 #pygame.display.flip()
59
60 #
61 scope = PyScope()
62 # scope
63 scope.test()
__init__
论坛:https://www.firebbs.cn/ 94 天猫:https://yehuosm.tmall.com
Python
10.4.1
Lcd
#
sudo python3 lcd_test2.py
10.5
论坛:https://www.firebbs.cn/ 95 天猫:https://yehuosm.tmall.com
Python
11 -Pygame
LubanCat-RK LubanCat 2 LubanCat 1 rk809-codec
Python-Pygame music
11.1 Pygame
11.2
11.2.1 Sound
LubanCat 2
#
ls /dev/snd/
cat@lubancat:~/sound$ ls /dev/snd/
by-path controlC0 pcmC0D0c pcmC0D0p seq timer
论坛:https://www.firebbs.cn 96 天猫:https://yehuosm.tmall.com
Python
linux
11.2.2
LubanCat 1 LubanCat 2 +
11.2.3 Pygame
apt
# Pygame
sudo apt -y install python3-pygame
11.2.4 Pygame
Pygame
11.3
1: ../io/sound/sound_test.py
6 if len(sys.argv) == 2:
( )
论坛:https://www.firebbs.cn/ 97 天猫:https://yehuosm.tmall.com
Python
( )
7 m_filename = sys.argv[1]
8 else:
9 print(
10 """ :
11 python3 sound_test.py <music_name.mp3>
12 example: python3 sound_test.py test.mp3"""
13 )
14 sys.exit()
15
16 import pygame
17
18 exitright = 'e'
19
20 def get_quitc():
21 ' '
22 #
23 global exitright
24 #
25 exitright = input(" q \n")
26
27 try:
28 #
29 pygame.mixer.init()
30 #
31 if pygame.mixer.get_init() is None:
32 raise RuntimeError(' ',)
33 # mp3 pygam MP3
34 pygame.mixer.music.load(m_filename)
35 # 50%
36 pygame.mixer.music.set_volume(0.2)
37 # 1
38 pygame.mixer.music.play(0)
( )
论坛:https://www.firebbs.cn/ 98 天猫:https://yehuosm.tmall.com
Python
( )
39
40 #
41 quitthread = threading.Thread(target=get_quitc,daemon=True)
42 quitthread.start()
43 #
44 while pygame.mixer.music.get_busy():
45 # cpu
46 if exitright in ('q','Q'):
47 raise
48 time.sleep(1)
49 except pygame.error as e:
50 print(" :",e,"\n ")
51 #
52 pygame.mixer.music.stop()
53 #
54 while pygame.mixer.music.get_busy():
55 time.sleep(1)
56 finally:
57 #
58 print(" ")
59 pygame.mixer.quit()
60 sys.exit()
11.3.1
io/sound test
论坛:https://www.firebbs.cn/ 99 天猫:https://yehuosm.tmall.com
Python
#
python3 sound_test.py test.mp3
#
python3 sound_test.py test.wav
#
python3 sound_test.py test.ogg
pygame
~
11.3.2
12 -Opencv
RK 24pin mipi csi
ov5648 opencv-python
12.1 opencv-python
12.2 camera
# camera
ls /dev/video*
camera
linux v4l-utils
12.3
#
sudo apt update
sudo apt -y install python3-pil python3-numpy python3-pip git wget
#
sudo apt install libavcodec-dev libavformat-dev libswscale-dev
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
2 opencv-python
( ):
# apt python3-opencv
sudo apt-get install python3-opencv
# whl
pip install opencv_python-4.6.0.66-cp36-abi3-manylinux_2_17_aarch64.
,→ manylinux2014_aarch64.whl
Python ( IPython)
cat@lubancat:~$ python3
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.2.0
>>>
12.4
1: io/camera/camera_photo.py
1 import os
2 import cv2
3
4 #
5 width = 640
6 height = 480
7
8 num = 1
9
( )
( )
10 # VideoCapture
11 cap = cv2.VideoCapture(0)
12
13 #
14 if not cap.isOpened():
15 raise RuntimeError('Could not open camera.')
16
17 #
18 cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
19 cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
20
21 while(cap.isOpened()):
22
23 # ret frame ,
24 ret,frame = cap.read()
25
26 # Lubancat_Camera_test
27 cv2.imshow("Lubancat_Camera_test", frame)
28
29 # 1ms val
30 val = cv2.waitKey(1) & 0xFF
31
32 # s ,
33 if val == ord('s'):
34 print("=======================================")
35 # jpeg
36 cv2.imwrite("photo" + str(num) + ".jpg", frame)
37 print("width = ", width)
38 print("height = ", height)
39 print("success to save photo: "'photo' + str(num)+".jpg")
40 print("=======================================")
41 num += 1
( )
( )
42
43 # q
44 if val == ord('q'):
45 break
46
47 #
48 cap.release()
49 #
50 cv2.destroyAllWindows()
s q
:
12.5
2: io/camera/camera_video01.py
1 import os
2 import cv2
3
4 #
5 width = 640
6 height = 480
7 fps = 25.0
8
9 # VideoCapture
10 video = cv2.VideoCapture(0)
11
12 #
13 video.set(cv2.CAP_PROP_FRAME_WIDTH, width)
14 video.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
15
16 # , YUV (
)
17 # fourcc = cv2.VideoWriter_fourcc(*'MJPG')
18 fourcc = cv2.VideoWriter_fourcc('I','4','2','0')
19
20 # ,
21 # , ,
22 # out = cv2.VideoWriter('output.mp4',fourcc, fps, (width,height))
23 out = cv2.VideoWriter('output.avi',fourcc, fps, (width,height))
24
25 while(video.isOpened()):
( )
( )
26 ret, frame = video.read()
27 if ret is True:
28 # Lubancat_Camera_video
29 cv2.imshow('Lubancat_Camera_video',frame)
30
31 #
32 out.write(frame)
33
34 # 1ms q
35 if cv2.waitKey(1) & 0xFF == ord('q'):
36 break
37 else:
38 print("Unable to read camera!")
39 break
40
41 #
42 video.release()
43 out.release()
44 cv2.destroyAllWindows()
cv2.VideoCapture
3: io/camera/camera_video02.py
1 import cv2
2
3 # VideoCapture
4 cap = cv2.VideoCapture('output.avi')
5
6 while(cap.isOpened()):
( )
( )
7 ret, frame = cap.read()
8
9 # Lubancat_Camera_video
10 cv2.imshow('Lubancat_Camera_video',frame)
11
12 # 1ms q
13 if cv2.waitKey(1) & 0xFF == ord('q'):
14 break
15
16 #
17 cap.release()
18 cv2.destroyAllWindows()
12.6
https://docs.opencv.org/4.6.0/dc/d4d/tutorial_py_table_of_contents_gui.html
13 Jupyter Notebook
Jupyter Notebook
Jupyter Notebook
13.2.1
Jupyter Notebook
: user
#
sudo apt -y install ipython3
sudo apt -y install jupyter
13.2.2
# Jupyter Notebook
jupyter notebook --generate-config
# :
Writing default config to: /home/cat/.jupyter/jupyter_notebook_config.py
--cat
# Jupyter Notebook
mkdir /home/cat/jupyter
# vim
vim /home/cat/.jupyter/jupyter_notebook_config.py
c.NotebookApp.allow_remote_access = True #
c.NotebookApp.ip = '*' # ip
c.NotebookApp.notebook_dir = '/home/debian/jupyter' # jupyter
13.2.3
token
Jupyter Notebook
#
ipython3
( )
( )
# python
from notebook.auth import passwd
passwd()
# Enter password:
Out[2]: 'sha1:274e5a04060e:e65098703b7e03836b01c261b6ca6d959049a3e9'
# ctrl+z ipython3
# vim
vim /home/cat/.jupyter/jupyter_notebook_config.py
# token
# c.NotebookApp.token = ''
#
c.NotebookApp.password =
,→ 'sha1:274e5a04060e:e65098703b7e03836b01c261b6ca6d959049a3e9'
Jupyter Notebook
: Jupyter Notebook
Jupyter Notebook
13.4.1
# Jupyter Notebook
# root
jupyter notebook --no-browser --allow-root
Jupyter Notebook
LubanCat 2
PC IP
Jupyter Notebook
13.4.2
jupyter
14 PIL
14.1 PIL
14.2 PIL
apt
# PIL
sudo apt -y install python3-pil
14.3 PIL
jupyter
libdemo\PIL\PIL.ipynb jupyter
Python Python3
pillow PIL
15 Numpy
15.1 Numpy
15.2 NumPy
# numpy
sudo apt -y install python3-numpy
15.3 NumPy
jupyter (jupyter
Jupyter Notebook )
libdemo\Numpy\Numpy.ipynb jupyter
16 Pandas
16.1 Pandas
Python-Pandas jupyter
16.2 Pandas
apt
# Pandas
sudo apt -y install python3-pandas
16.3 Pandas
jupyter
libdemo\Pandas\Pandas.ipynb jupyter
17 Web - Flask
17.1 Flask
Python-flask
web
17.2 Flask
apt
# flask
sudo apt -y install python3-flask
17.3 Flask
web
17.3.1
# web
mkdir webapp_flask && cd webapp_flask
# webapp
mkdir -p app/static
mkdir app/templates
mkdir tmp
17.3.2
17.3.2.1 __init__.py
webapp_flask app :
# app __init__.py
vim __init__.py
1: __init__.py
( )
( )
3 app = Flask(__name__)
4 from app import view
17.3.2.2 views.py
views.py app :
# app view.py
vim view.py
2: view.py
3 @app.route('/')
4 @app.route('/index')
5 def index():
6 return "Hello, Lubancat!"
17.3.2.3 run.py
Web run.py
webapp_flask webapp_flask:
# webapp_flask run.py
nano run.py
3: run.py
1 #!usr/bin/python3
2 from app import app
3 # "192.168.103.108"
4 app.run(debug = True,host="192.168.103.108")
run.py
#
chmod a+x run.py
17.3.3 webapp
webapp app
# app
./run.py
app web
192.168.103.108 5000 http://192.168.103.108:
5000/
libdemo\Flask\webapp_flask
17.3.4
Flask
18 Web - Django
18.1 Django
18.2 Django
apt
# Django
sudo apt -y install python3-django
18.3 Django
web
18.3.1
# web
mkdir webapp
cd webapp
18.3.2
Django django-admin
#
django-admin startproject Lubancat
# Lubancat
cd ./Lubancat/
Django
#
cd ./Lubancat/
#
vim settings.py
# ALLOWED_HOSTS
ALLOWED_HOSTS = ['*']
18.3.3 webapp
Lubancat manage.py
# ,IP ip
python3 manage.py runserver 192.168.103.115:5001
webapp web
192.168.103.115 5001 http://192.168.103.
115:5001/
18.3.4 Django
# Django
pip3 install Django==3.1.7
#
sudo cp /home/cat/.local/bin/django-admin /usr/bin/
19 Modbus -pymodbus
Modbus
Modbus-RTU
19.1 Modbus
Modbus
python3-pymodbus
19.2 python3-pymodbus
python3-pymodbus
19.3
19.3.1 uart
: uart PC Modbus
uart pymodbus Modbus
GPIO
, LubanCat_RK LubanCat-RK -40pin
LubanCat 2 40
UART3 UART3
RK
# UART
ls /dev/ttyS*
UART
/dev/ttyS3 uart3
: Permission denied
root sudo root
19.3.2
Luban-
Cat 2 uart3 UART
USB TTL
: pymodbus pyserial
uart uart pymodbus rs485
pymodbus
19.3.3 pymodbus
#
sudo pip install -U pymodbus
# setuptools python
git clone https://github.com/riptideio/pymodbus.git
19.4
1: slave_server.py
( )
10 )
11 parser.add_argument(
12 "--framer",
13 choices=["rtu"],
14 help="set framer, default depends on --comm",
15 type=str,
16 )
17 parser.add_argument(
18 "--log",
19 choices=["critical", "error", "warning", "info", "debug"],
20 help="set log level, default is info",
21 default="info",
22 type=str,
23 )
24 parser.add_argument(
25 "--port",
26 help="set port",
27 type=str,
28 )
29 if server:
30 parser.add_argument(
31 "--store",
32 choices=["sequential"],
33 help="set type of datastore",
34 default="sequential",
35 type=str,
36 )
37 parser.add_argument(
38 "--slaves",
39 help="set number of slaves, default is 0 (any)",
40 default=0,
41 type=int,
( )
( )
42 nargs="+",
43 )
44 if extras:
45 for extra in extras:
46 parser.add_argument(extra[0], **extra[1])
47 args = parser.parse_args()
48
49 # set defaults
50 comm_defaults = {
51 "serial": ["rtu", "/dev/ptyp0"],
52 }
53 framers = {
54 "rtu": ModbusRtuFramer,
55 }
56 pymodbus_apply_logging_config()
57 _logger.setLevel(args.log.upper())
58 args.framer = framers[args.framer or comm_defaults[args.comm][0]]
59 args.port = args.port or comm_defaults[args.comm][1]
60 if args.comm != "serial" and args.port:
61 args.port = int(args.port)
62 return args
63
64 def setup_server(args):
65 """Run server setup."""
66 # The datastores only respond to the addresses that are initialized
67 # If you initialize a DataBlock to addresses of 0x00 to 0xFF, a request␣
,→ to
68 # 0x100 will respond with an invalid address exception.
69 # This is because many devices exhibit this kind of behavior (but not␣
,→ all)
70 _logger.info("### Create datastore")
71 # co 0 10 5 True 5 False
( )
( )
72 co_block = ModbusSequentialDataBlock(0, [True]*5 + [False]*5)
73 # di 0 10 5 True 5
False
74 di_block = ModbusSequentialDataBlock(0, [True]*5 + [False]*5)
75 # ir 0 10 0~10
76 ir_block = ModbusSequentialDataBlock(0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
77 # hr 0 10 0~10
78 hr_block = ModbusSequentialDataBlock(0, [9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
79
80 if args.slaves:
81 # The server then makes use of a server context that allows the␣
,→ server
82 # to respond with different slave contexts for different unit ids.
83 # By default it will return the same context for every unit id␣
,→ supplied
84 # (broadcast mode).
85 # However, this can be overloaded by setting the single flag to␣
,→ False and
86 # then supplying a dictionary of unit id to context mapping::
87 #
88 # The slave context can also be initialized in zero_mode which means
89 # that a request to address(0-7) will map to the address (0-7).
90 # The default is False which is based on section 4.4 of the
91 # specification, so address(0-7) will map to (1-8)::
92 context = {
93 0x01: ModbusSlaveContext(
94 di=di_block,
95 co=co_block,
96 hr=hr_block,
97 ir=ir_block,
98 zero_mode=True
99 ),
( )
( )
100 0x02: ModbusSlaveContext(
101 di=di_block,
102 co=co_block,
103 hr=hr_block,
104 ir=ir_block,
105 ),
106 0x03: ModbusSlaveContext(
107 di=di_block,
108 co=co_block,
109 hr=hr_block,
110 ir=ir_block,
111 ),
112 }
113 single = False
114 else:
115 context = ModbusSlaveContext(
116 di=di_block, co=co_block, hr=hr_block, ir=ir_block, unit=1
117 )
118 single = True
119
124
( )
132 context=args.context, # Data storage
133 #identity=args.identity, # server identify
134 timeout=1, # waiting time for request to complete
135 port=args.port, # serial port
136 # custom_functions=[], # allow custom handling
137 framer=args.framer, # The framer strategy to use
138 # handler=None, # handler for each session
139 stopbits=1, # The number of stop bits to use
140 bytesize=8, # The bytesize of the serial messages
141 # parity="N", # Which kind of parity to use
142 baudrate=115200, # The baud rate to use for the serial device
143 # handle_local_echo=False, # Handle local echo of the USB-to-RS485␣
,→ adaptor
144 # ignore_missing_slaves=True, # ignore request to a missing slave
145 # broadcast_enable=False, # treat unit_id 0 as broadcast address,
146 # strict=True, # use strict timing, t1.5 for Modbus RTU
147 # defer_start=False, # Only define server do not activate
148 )
149 return server
150
19.4.2
1 LubanCat 2
#
sudo python slave_server.py --comm serial --framer rtu --port /dev/ttyS3 --
,→ store sequential --log debug --slaves 1
#--comm serial --framer rtu --port --log
debug
# sudo python slave_server.py -h
#
cat@lubancat:~$ sudo python server0.py --comm serial --framer rtu --port /
,→ dev/ttyS3 --store sequential --log debug --slaves 1
15:10:47 INFO server0:123 ### Create datastore
15:10:47 DEBUG selector_events:59 Using selector: EpollSelector
15:10:47 INFO server0:181 ### start ASYNC server, listening on /dev/ttyS3 -
,→ serial
15:10:47 DEBUG async_io:130 Serial connection opened on port: /dev/ttyS3
15:10:47 DEBUG async_io:442 Serial connection established
2 Modbus Poll 30
https://modbustools.com/download.html
Read/write Definition ,
1 0 10
connection COM6
- -log debug
Read/write Definition
Modbus Poll
19.4.3
Modbus-RTU
modbus Poll
log debug
pymodbus Modbus
pymodbus
19.4.4
pymodbus-examples
pymodbus-doc
20 GUI - PyQt5
PyQt5 PyQt Qt
Creator ui
• RK
• Debian10
• Python Python3.7
• Qt 5.11.3
• PyQt PyQt5
20.1 PyQt5
20.2 PyQt5
20.2.1 Qt
# apt
sudo apt-get -y install python3-pyqt5
# pip
pip3 install pyqt5
# qt
sudo apt-get install qt5-default
cat@lubancat:~$ python3
Python 3.7.3 (default, Oct 31 2022, 14:04:00)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt5
>>>
PyQt5
: HDMI mipi
20.3 PyQt5
20.3.1
# sys
import sys
# QtWidgets QWidget QLCDNumber LCD
QSlider
# QVBoxLayout QApplication GUI
from PyQt5.QtWidgets import QWidget,QLCDNumber,QSlider,QVBoxLayout,
,→ QApplication
# PyQt5.QtCore.Qt,
from PyQt5.QtCore import Qt
# ``*``
from PyQt5.Qt import *
20.3.2
# WinForm QWidget
class WinForm(QWidget):
#
def __init__(self):
# init
super().__init__()
#
self.initUI()
def initUI(self):
# LCD
lcd = QLCDNumber(self)
slider = QSlider(Qt.Horizontal, self)
# LCD
slider.setMaximum(1000)
slider.setValue(666)
slider.setMinimumWidth(200)
slider.setFixedHeight(60)
lcd.display(666)
# QVboxLayout
vBox = QVBoxLayout()
vBox.addWidget(lcd)
vBox.addWidget(slider)
self.setLayout(vBox)
# valueChanged() Qslider slider
connect lcd
slider.valueChanged.connect(lcd.display)
( )
( )
#
slider.setStyleSheet(style)
#self.setGeometry(0,0,800,480)
#
self.setWindowTitle(" ")
20.3.3
# name , main
if __name__ == '__main__':
# QApplication sys.argv
app = QApplication(sys.argv)
#
form = WinForm()
#
form.resize(800, 480)
form.show()
# exit
sys.exit(app.exec_())
QApplication form
20.3.4
pyqt5_demo.py :
1: libdemo/PyQt5/pyqt5_demo.py
1 import sys
2 from PyQt5.QtWidgets import QWidget,QLCDNumber,QSlider,QVBoxLayout,
,→ QApplication
3 from PyQt5.QtCore import Qt
4
5 class WinForm(QWidget):
6 def __init__(self):
7 super().__init__()
8 self.initUI()
9
10 def initUI(self):
11 # LCD
12 lcd = QLCDNumber(self)
13 slider = QSlider(Qt.Horizontal, self)
14
15 slider.setMaximum(1000)
16 slider.setValue(666)
17 slider.setMinimumWidth(200)
18 slider.setFixedHeight(60)
19 lcd.display(666)
20
21 # QVboxLayout
22 vBox = QVBoxLayout()
23 vBox.addWidget(lcd)
24 vBox.addWidget(slider)
25
26 self.setLayout(vBox)
( )
( )
27 # valueChanged() Qslider slider
connect lcd
28 slider.valueChanged.connect(lcd.display)
29
35 slider.setStyleSheet(style)
36 #self.setGeometry(0,0,800,480)
37 self.setWindowTitle(" ")
38
39 if __name__ == '__main__':
40 app = QApplication(sys.argv)
41 form = WinForm()
42 form.resize(800, 480)
43 form.show()
44 sys.exit(app.exec_())
pyqt5_demo.py
python3 pyqt5_demo.py
libdemo\PyQt5\pyqt5_demo.py
20.4 Qt Designer
python
Qt Designer
PyQt UI Qt Designer UI
# apt Qt Creator
sudo apt install qtcreator
# qt designer
sudo apt install qttools5-dev-tools
: Qt Creator
PC ui pyqt5
Qt Creator Qt Designer
20.4.1 UI
Qt Creator file go to File -> New File or Project Files and Classes
QT , QT Designer Form , .ui
Main Window
20.4.2
ui Frame
Input Widgets Line Edit Buttons:PushButton
Display Widgets TextLabe Buttons CheckBox
*{
background:rgb(0, 0, 0);
font-size:15px;
font-style:MingLiU-ExtB;
}
QFrame{
border:sold 10px rgba(255,255,255);
background-image:url(/https/www.scribd.com/home/cat/qt/login/lubancat6.png);
}
QLineEdit{
color:#8d98a1;
background-color:rgb(0, 0, 0);
font-size:16px;
border-style:outset;
border-radius:10px;
font-style:MingLiU-ExtB;
}
QPushButton{
background:#ced1d8;
border-style:outset;
border-radius:10px;
font-style:MingLiU-ExtB;
}
QPushButton:pressed{
background-color:#405361;
border-style:inset;
font-style:MingLiU-ExtB;
}
QCheckBox{
background:rgba(85,170,255,0);
( )
( )
color:white;
font-style:MingLiU-ExtB;
}
QLabel{
background:rgba(85,170,255,0);
color:white;
font-style:MingLiU-ExtB;
font-size:14px;
}
: ui qt designer Qt
20.4.3 ui
1. uic .ui
2: libdemo/PyQt5/login/login.py
1 import sys
2 from PyQt5.QtWidgets import QApplication, QMainWindow
3 from PyQt5 import uic
4
5 class LoginWindow(QMainWindow):
6
13 if __name__ == '__main__':
14 app = QApplication(sys.argv)
15 window = LoginWindow()
16 window.show()
17 sys.exit(app.exec_())
2. python
# apt pyqt5-dev-tools
sudo apt-get install pyqt5-dev-tools
( )
( )
# .ui python
pyuic5 -o login.ui login_ui.py
python
3: libdemo/PyQt5/login/login_ui_form.py
1 import sys
2 from PyQt5.QtWidgets import QApplication, QMainWindow
3 # Ui_Form
4 from login_ui import Ui_Form
5
6 class LoginWindow(QMainWindow):
7
16 if __name__ == "__main__":
17 app = QApplication(sys.argv)
18 win = LoginWindow()
19 win.show()
20 sys.exit(app.exec())
#
python3 login.py
python Tool->Exteral->RunPython->RunPy
libdemo/PyQt5
20.5
PyQt5
https://www.riverbankcomputing.com/static/Docs/PyQt5/introduction.html
https://www.pythonguis.com/latest/
21
Python RK
RKNN
21.1
1 Artificial Intelligence AI
2 Machine Learning ML
3 Deep Learning DL
• TensorFlow
• PaddlePaddle
• MindSpore AI
Caffe keras
21.2 RK
21.3
https://zh.wikipedia.org/wiki/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0
https://zh.wikipedia.org/wiki/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0
https://www.paddlepaddle.org.cn/tutorials/projectdetail/3520300
https://tensorflow.google.cn/resources/learn-ml?hl=zh-cn
22 NPU
NPU
ASR ADAS
PC Ubuntu RKNN-Toolkit2
RKNN Toolkit Lite2
22.1 RKNN-Toolkit2
Toolkit-lite2 PC python
RKNN-Toolkit2 Ubuntu18.04(x64)
RKNN Toolkit2
22.1.1 RKNN-Toolkit2
PC ubuntu20.04
#
sudo apt-get install libxslt1-dev zlib1g-dev libglib2.0 libsm6 \
libgl1-mesa-glx libprotobuf-dev gcc
RKNN-Toolkit2
# ubuntu20.04 RKNN-Toolkit2
, python venv
mkdir project-Toolkit2 && cd project-Toolkit2
python3 -m venv .toolkit2_env
#
source .toolkit2_env/bin/activate
# RKNN-Toolkit2
git clone https://github.com/rockchip-linux/rknn-toolkit2.git
# pip3
pip3 config set global.index-url https://mirror.baidu.com/pypi/simple
# rknn-toolkit2\doc\requirements_cp38-1.4.0.txt
( )
( )
pip3 install numpy
pip3 install -r doc/requirements_cp38-1.4.0.txt
# rknn_toolkit2
pip3 install packages/rknn_toolkit2-1.4.0_22dcfef4-cp38-cp38-linux_x86_64.
,→ whl
22.1.2
# examples/onnx/yolov5
cd examples/onnx/yolov5
#
python test.py
yolov5 out.jpg
22.1.3
#
cd ../usr/bin
chmod +x rknn_server restart_rknn.sh start_rknn.sh
# rknn_server
restart_rknn.sh
# adbd active
sudo systemctl status adbd.service
sudo /usr/bin/adbd &
PC
# PC adb
sudo apt install adb
# adb server
adb start-server
# IP 5555
adb connect 192.168.103.115
# , device_id
( )
( )
adb devices
cd examples/onnx/yolov5
# ,
python test.py
( )
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
done
--> Export rknn model
done
--> Init runtime environment
W init_runtime: Flag perf_debug has been set, it will affect the␣
,→ performance of inference!
I NPUTransfer: Starting NPU Transfer Client, Transfer version 2.1.0␣
,→ (b5861e7@2020-11-23T11:50:36)
D RKNNAPI: ==============================================
D RKNNAPI: RKNN VERSION:
D RKNNAPI: API: 1.4.0 (bb6dac9 build: 2022-08-29 16:17:01)(null)
D RKNNAPI: DRV: rknn_server: 1.3.0 (121b661 build: 2022-04-29 11:11:47)
D RKNNAPI: DRV: rknnrt: 1.4.0 (a10f100eb@2022-09-09T09:07:14)
D RKNNAPI: ==============================================
done
=====================================================================================
Performance
#### The performance result is just for debugging, ####
#### may worse than actual performance! ####
=====================================================================================
Total Weight Memory Size: 7312768
Total Internal Memory Size: 7782400
Predict Internal Memory RW Amount: 134547200
Predict Weight Memory RW Amount: 7312768
ID OpType DataType Target InputShape ␣
,→ OutputShape DDR Cycles NPU Cycles Total Cycles ␣
,→ Time(us) MacUsage(%) RW(KB) FullName
0 InputOperator UINT8 CPU \ ␣
,→ (1,3,640,640) 0 0 0 ␣
,→ 19 \ 1200.00 InputOperator:images
( )
( )
1 Conv UINT8 NPU (1,3,640,640),(32,3,6,6),(32) ␣
,→ (1,32,320,320) 1493409 691200 1493409 ␣
,→ 11956 9.64 4409.25 Conv:Conv_0
2 exSwish INT8 NPU (1,32,320,320) ␣
,→ (1,32,320,320) 2167674 0 2167674 ␣
,→ 5408 \ 6400.00 exSwish:Sigmoid_1_2swish
3 Conv INT8 NPU (1,32,320,320),(64,32,3,3),(64) ␣
,→ (1,64,160,160) 1632022 921600 1632022 ␣
,→ 4153 36.99 4818.50 Conv:Conv_3
4 exSwish INT8 NPU (1,64,160,160) ␣
,→ (1,64,160,160) 1083837 0 1083837 ␣
,→ 2849 \ 3200.00 exSwish:Sigmoid_4_2swish
5 Conv INT8 NPU (1,64,160,160),(32,64,1,1),(32) ␣
,→ (1,32,160,160) 813640 102400 813640 ␣
,→ 1455 11.73 2402.25 Conv:Conv_6
6 exSwish INT8 NPU (1,32,160,160) ␣
,→ (1,32,160,160) 541919 0 541919 ␣
,→ 1456 \ 1600.00 exSwish:Sigmoid_7_2swish
........( )
142 Conv INT8 NPU (1,128,80,80),(255,128,1,1),(255) ␣
,→ (1,255,80,80) 824352 408000 824352 ␣
,→ 1040 65.38 2433.88 Conv:Conv_198
143 OutputOperator INT8 CPU (1,255,80,80),(1,80,80,256) ␣
,→ \ 0 0 0 ␣
,→ 1011 \ 3200.00 OutputOperator:output
144 OutputOperator INT8 CPU (1,255,40,40),(1,40,40,256) ␣
,→ \ 0 0 0 ␣
,→ 216 \ 800.00 OutputOperator:327
145 OutputOperator INT8 CPU (1,255,20,20),(1,20,20,256) ␣
,→ \ 0 0 0 ␣
,→ 178 \ 220.00 OutputOperator:328
Total Operator Elapsed Time(us): 117367
( )
( )
=====================================================================================
eval_perf
rknn-toolkit2 https://github.com/rockchip-linux/rknn-
toolkit2/tree/master/examples/functions
LubanCat 2 Debian10
# python
sudo apt-get install python3-dev python3-pip gcc
#
sudo apt-get install -y python3-opencv
sudo apt-get install -y python3-numpy
sudo apt -y install python3-setuptools
pip3 install wheel
Toolkit Lite2
22.2.2
( )
I RKNN: [10:29:36.524] RKNN Model Information: version: 1, toolkit version:␣
,→ 1.4.0-22dcfef4(compiler version: 1.4.0 (3b4520e4f@2022-09-05T20:52:35)),␣
,→ target: RKNPU lite, target platform: rk3568, framework name: ONNX,␣
,→ framework layout: NCHW
done
--> Running model
done
class: person, score: 0.8352155685424805
box coordinate left,top,right,down: [211.9896697998047, 246.17460775375366,␣
,→ 283.70787048339844, 515.0830216407776]
class: person, score: 0.8330121040344238
box coordinate left,top,right,down: [473.26745200157166, 231.93780636787415,
,→ 562.1268351078033, 519.7597033977509]
class: person, score: 0.7970154881477356
box coordinate left,top,right,down: [114.68497347831726, 233.11390662193298,
,→ 207.21904873847961, 546.7821505069733]
class: person, score: 0.4651743769645691
box coordinate left,top,right,down: [79.09242534637451, 334.77847719192505,␣
,→ 121.60038471221924, 518.6368670463562]
class: bus , score: 0.7837686538696289
box coordinate left,top,right,down: [86.41703361272812, 134.41848754882812,␣
,→ 558.1083570122719, 460.4184875488281]
out.jpg
: ../yolov5/test.py cv2.imshow()
22.3 rk356X
1 CPU
# CPU
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
# CPU
cat /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
#
cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies
#
echo 1800000 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed
2 DDR
# DDR
cat /sys/class/devfreq/dmc/cur_freq
# DDR
cat /sys/class/devfreq/dmc/governor
# DDR
cat /sys/class/devfreq/dmc/available_frequencies
( )
( )
# 1056000000
echo 1056000000 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed
3 NPU
# NPU
cat /sys/class/devfreq/fde40000.npu/available_frequencies
# npu
cat /sys/class/devfreq/fde40000.npu/cur_freq
22.4
RKNPU2 https://github.com/rockchip-linux/rknpu2
RKNN-toolkit2 https://github.com/rockchip-linux/rknn-toolkit2
23
PaddlePaddle
23.1 PaddlePaddle
PaddlePaddle
PaddlePaddle 477 56 18
AI AI
2022 12
535 20
67
PaddlePaddle
PaddlePaddle
23.2 PaddlePaddle
# PC pip3 CPU
pip3 install paddlepaddle -i https://mirror.baidu.com/pypi/simple
# python
python
import paddle
paddle.utils.run_check()
#
print(paddle.__version__)
paddlepaddle
23.3
Paddle
23.3.1
28*28 MNIST
Paddle MNIST
# DataSet
train_dataset = paddle.vision.datasets.MNIST(mode='train',␣
,→ transform=transform)
test_dataset = paddle.vision.datasets.MNIST(mode='test',␣
,→ transform=transform)
23.3.2
#
lenet = paddle.vision.models.LeNet(num_classes=10)
model = paddle.Model(lenet)
#paddle.summary(lenet,(1, 1, 28, 28))
---------------------------------------------------------------------------
Layer (type) Input Shape Output Shape Param #
===========================================================================
Conv2D-1 [[1, 1, 28, 28]] [1, 6, 28, 28] 60
ReLU-1 [[1, 6, 28, 28]] [1, 6, 28, 28] 0
MaxPool2D-1 [[1, 6, 28, 28]] [1, 6, 14, 14] 0
Conv2D-2 [[1, 6, 14, 14]] [1, 16, 10, 10] 2,416
ReLU-2 [[1, 16, 10, 10]] [1, 16, 10, 10] 0
MaxPool2D-2 [[1, 16, 10, 10]] [1, 16, 5, 5] 0
Linear-1 [[1, 400]] [1, 120] 48,120
Linear-2 [[1, 120]] [1, 84] 10,164
Linear-3 [[1, 84]] [1, 10] 850
===========================================================================
Total params: 61,610
Trainable params: 61,610
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.11
( )
( )
Params size (MB): 0.24
Estimated Total Size (MB): 0.35
---------------------------------------------------------------------------
23.3.3
epoch
mini-batch predict_label
true_label loss optimizer
loss
#
# optimizer Loss metric
# Adam CrossEntropyLoss
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy())
#
loss
# metric
model.evaluate(test_dataset, batch_size=64, verbose=1)
23.3.4 ONNX
load
1 #
2 model.save('./output/mnist')
3 #
4 model.load('output/mnist')
5
6 #
7 img, label = test_dataset[0]
8 # shape 1*28*28 1*1*28*28 batch
9 img_batch = np.expand_dims(img.astype('float32'), axis=0)
10
11 # predict_batch list
12 out = model.predict_batch(img_batch)[0]
13 pred_label = out.argmax()
14 print('true label: {}, pred label: {}'.format(label[0], pred_label))
# onnx
save_path = 'onnx/lenet'
#
x_spec = paddle.static.InputSpec([1, 1, 28, 28], 'float32', 'x')
# ONNX
paddle.onnx.export(lenet, save_path, input_spec=[x_spec], opset_version=11)
23.3.5
1: paddlepaddle/digital_recognition.py
1 import paddle
2 import numpy as np
3 from paddle.vision.transforms import Normalize
4
10 #
11 lenet = paddle.vision.models.LeNet(num_classes=10)
12 model = paddle.Model(lenet)
13
14 #
15 model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
16 paddle.nn.CrossEntropyLoss(),
17 paddle.metric.Accuracy())
18
19 #
20 model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)
21 #
22 model.evaluate(test_dataset, batch_size=64, verbose=1)
23
24 #
25 model.save('./output/mnist')
26
27 #
( )
( )
28 # model.load('output/mnist')
29
30 #
31 img, label = test_dataset[0]
32 # shape 1*28*28 1*1*28*28 batch
33 img_batch = np.expand_dims(img.astype('float32'), axis=0)
34
35 # predict_batch list
36 out = model.predict_batch(img_batch)[0]
37 pred_label = out.argmax()
38 print('true label: {}, pred label: {}'.format(label[0], pred_label))
39
40 #
41 save_path = 'onnx/lenet'
42 #
43 x_spec = paddle.static.InputSpec([1, 1, 28, 28], 'float32', 'x')
44 # ONNX
45 paddle.onnx.export(lenet, save_path, input_spec=[x_spec], opset_version=11)
The loss value printed in the log is the current step, and the metric is␣
,→ the average value of previous steps.
Epoch 1/5
step 938/938 [==============================] - loss: 0.0117 - acc: 0.9368 -
,→ 19ms/step
Epoch 2/5
step 938/938 [==============================] - loss: 0.0072 - acc: 0.9778 -
,→ 18ms/step
Epoch 3/5
step 938/938 [==============================] - loss: 0.0521 - acc: 0.9814 -
,→ 19ms/step
( )
( )
Epoch 4/5
step 938/938 [==============================] - loss: 0.0047 - acc: 0.9848 -
,→ 19ms/step
Epoch 5/5
step 938/938 [==============================] - loss: 0.0208 - acc: 0.9857 -
,→ 19ms/step
Eval begin...
step 157/157 [==============================] - loss: 0.0011 - acc: 0.9843 -
,→ 8ms/step
Eval begin...
step 157/157 [==============================] - loss: 2.8939e-04 - acc: 0.
,→ 9825 - 8ms/step
Eval samples: 10000
true label: 7, pred label: 7
I0209 16:10:02.397411 8350 interpretercore.cc:279] New Executor is Running.
2023-02-09 16:10:02 [INFO] Static PaddlePaddle model saved in onnx/
,→ paddle_model_static_onnx_temp_dir.
[Paddle2ONNX] Start to parse PaddlePaddle model...
[Paddle2ONNX] Model file path: onnx/paddle_model_static_onnx_temp_dir/model.
,→ pdmodel
[Paddle2ONNX] Paramters file path: onnx/paddle_model_static_onnx_temp_dir/
,→ model.pdiparams
[Paddle2ONNX] Start to parsing Paddle model...
[Paddle2ONNX] Use opset_version = 11 for ONNX export.
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
2023-02-09 16:10:02 [INFO] ONNX model saved in onnx/lenet.onnx.
23.3.6 RKNN
RKNN-Toolkit2
2: paddlepaddle/rknn_transfer.py
1 INPUT_SIZE = 28
2 IMG_PATH = 'test.jpg'
3
4 # RKNN ,
5 #rknn = RKNN(verbose=True)
6 rknn = RKNN()
7
8 # NPU
9 # mean_values
10 # std_values
11 # target_platform:
12 # RKNN toolkit2
13 print('--> Config model')
14 rknn.config(mean_values=[127.5], std_values=[127.5], target_platform='rk3568
,→ ')
15
16 # ONNX
17 print('--> Loading model')
18 rknn.load_onnx(model='./lenet.onnx')
19 print('done')
20
25 # RKNN
26 print('--> Init runtime environment')
( )
( )
27 ret = rknn.init_runtime()
28 print('done')
29
30 #
31 img = cv2.imread(IMG_PATH)
32 img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
33 img = cv2.resize(img,(28,28))
34 img = np.expand_dims(img, 0)
35 img = np.expand_dims(img, 0)
36
37 #
38 print('--> Running model')
39 outputs = rknn.inference(inputs=[img], data_format='nchw')
40 print("result: ", outputs)
41 print(" :", np.argmax(outputs))
42
43 # rknn
44 print('--> Export rknn model')
45 rknn.export_rknn('./lenet.rknn')
46 print('done')
47
48 # rknn
49 rknn.release()
( )
7 --> Init runtime environment
8 W init_runtime: Target is None, use simulator!
9 done
10 --> Running model
11 Analysing : 100%|█████████████████████████████████████████████████| 11/11␣
,→ [00:00<00:00, 1931.24it/s]
12 Preparing : 100%|██████████████████████████████████████████████████| 11/11␣
,→ [00:00<00:00, 360.11it/s]
13 result: [array([[-3.5175781, -4.140625 , -1.8232422, 0.5727539, 2.
,→ 7480469,
14 -1.9824219, -7.9726562, -0.5292969, 3.46875 , 7.5039062]],
15 dtype=float32)]
16 : 9
17 --> Export rknn model
18 done
23.4
#
sudo apt update
sudo apt -y install python3-pil python3-numpy python3-pip git wget
sudo apt install python3-opencv
23.4.2
rknn
3: paddlepaddle/rknn_test.py
1 IMG_PATH = '9.jpg'
2 RKNN_MODEL = 'lenet.rknn'
3
4 # RKNN
5 rknn_lite = RKNNLite()
6
7 #
8 print('--> Load RKNN model')
9 ret = rknn_lite.load_rknn(RKNN_MODEL)
10 if ret != 0:
11 print('Load RKNN model failed')
12 exit(ret)
13 print('done')
14
23 # load image
24 img = cv2.imread(IMG_PATH)
25 img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
26 img = cv2.resize(img,(28,28))
27
( )
( )
28 # runing model
29 print('--> Running model')
30 outputs = rknn_lite.inference(inputs=[img])
31 print("result: ", outputs)
32 print(" :", np.argmax(outputs))
33
34 rknn_lite.release()
23.5
LeNet MNIST
PaddlePaddle
23.6
https://baike.baidu.com/item/%E9%A3%9E%E6%A1%A8/23472642?fromtitle=PaddlePaddle&
fromid=20110894&fr=aladdin
https://www.paddlepaddle.org.cn/documentation/docs/zh/install/pip/linux-pip.html
https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/beginner/quick_start_cn.html
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/index_cn.html
24 PaddlePaddle FastDeploy
24.1 FastDeploy
FastDeploy AI
3 AI 1 API
FastDeploy
AI
24.2
24.2.1 PC
rknn-Toolkit2 FastDeploy
1 rknn-Toolkit2
NPU
2 FastDeploy
# FastDeploy
#
pip3 install fastdeploy-python -f https://www.paddlepaddle.org.cn/whl/
,→ fastdeploy.html
# paddle2onnx
# paddlepaddle
python -m pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.
,→ cn/whl/linux/cpu-mkl/develop.html
paddlepaddle
1 RKNN
# RK
git clone https://github.com/rockchip-linux/rknpu2
( )
( )
sudo cp ./rknpu2/runtime/RK356X/Linux/librknn_api/aarch64/* /usr/lib
sudo cp ./rknpu2/runtime/RK356X/Linux/rknn_server/aarch64/usr/bin/* /usr/
,→ bin/
NPU
2 FastDeploy
#
git clone https://github.com/PaddlePaddle/FastDeploy.git
#
export ENABLE_ORT_BACKEND=ON
export ENABLE_RKNPU2_BACKEND=ON # rknpu2
export ENABLE_VISION=ON
export RKNN2_TARGET_SOC=rk356X # rk356X
cd FastDeploy/python
#
python3 setup.py build
python3 setup.py bdist_wheel
#
cd dist
pip3 install fastdeploy_python-0.0.0-cp37-cp37m-linux_aarch64.whl
# fastdeploy_python-0.0.0-cp37-cp37m-linux_
,→ aarch64.whl
: rk356X swap 4G
24.3
24.3.1 PicoDet
# Paddle FastDeploy
#
git
tar -xvf picodet_s_416_coco_lcnet.zip
# onnx
cd tools/rknpu2/picodet_s_416_coco_lcnet/
# shape
python -m paddle2onnx.optimize --input_model picodet_s_416_coco_lcnet/
,→ picodet_s_416_coco_lcnet.onnx \
( )
( )
--output_model picodet_s_416_coco_lcnet/picodet_
,→ s_416_coco_lcnet.onnx \
--input_shape_dict "{'image':[1,3,416,416]}"
2 RKNN
Paddle2ONNX ( 1.0.5)
Netron NonMaxSuppression pi-
codet_s_416_coco_lcnet.yaml outputs_nodes
1: picodet_s_416_coco_lcnet.yaml
1 mean:
2 std:
3 model_path: ./picodet_s_416_coco_lcnet1/picodet_s_416_coco_lcnet.onnx
4 target_platform: RK3568
( )
( )
5 outputs_nodes:
6 - 'p2o.Div.79'
7 - 'p2o.Concat.9'
8 do_quantization: False
9 dataset:
10 output_folder: "./picodet_s_416_coco_lcnet1"
#
cd tools/rknpu2/
python export.py --config_path config/picodet_s_416_coco_lcnet.yaml --
,→ target_platform rk3568
# rknn
cd examples/vision/detection/paddledetection/rknpu2/python
python3 infer.py --model_file picodet_s_416_coco_lcnet_rk3568_unquantized.
,→ rknn --config_file infer_cfg.yml --image 00000014439.jpg
[INFO] fastdeploy/vision/common/processors/transform.
,→ cc(159)::FuseNormalizeColorConvert BGR2RGB and Normalize are fused to␣
,→ Normalize with swap_rb=1
[INFO] fastdeploy/runtime/backends/rknpu2/rknpu2_backend.
,→ cc(57)::GetSDKAndDeviceVersion rknn_api/rknnrt version: 1.4.0␣
,→ (a10f100eb@2022-09-09T09:07:14), driver version: 0.7.2
E RKNN: [15:17:31.563] rknn_set_core_mask: No implementation found for␣
,→ current platform! ( )
( )
index=0, name=image, n_dims=4, dims=[1, 416, 416, 3], n_elems=519168,␣
,→ size=1038336, fmt=NHWC, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000,␣
,→ pass_through=0
index=1, name=scale_factor, n_dims=2, dims=[1, 2, 0, 0], n_elems=2, size=4,␣
,→ fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000, pass_
,→ through=0
index=0, name=p2o.Div.79, n_dims=4, dims=[1, 3598, 4, 1], n_elems=14392,␣
,→ size=28784, fmt=NCHW, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000,␣
,→ pass_through=0
index=1, name=p2o.Concat.9, n_dims=4, dims=[1, 80, 3598, 1], n_elems=287840,
,→ size=575680, fmt=NCHW, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000,␣
,→ pass_through=0
[INFO] fastdeploy/runtime/runtime.cc(449)::CreateRKNPU2Backend Runtime␣
,→ initialized with Backend::RKNPU2 in Device::RKNPU.
[WARNING] fastdeploy/runtime/backends/rknpu2/rknpu2_backend.cc(315)::Infer ␣
,→ The input tensor type != model's inputs type.The input_type need FP16,
,→ but inputs[0].type is FP32
[INFO] fastdeploy/runtime/backends/rknpu2/rknpu2_backend.cc(328)::Infer The␣
,→ input model is not a quantitative model. Close the normalize operation.
[WARNING] fastdeploy/runtime/backends/rknpu2/rknpu2_backend.cc(315)::Infer ␣
,→ The input tensor type != model's inputs type.The input_type need FP16,
,→ but inputs[1].type is FP32
[INFO] fastdeploy/runtime/backends/rknpu2/rknpu2_backend.cc(328)::Infer The␣
,→ input model is not a quantitative model. Close the normalize operation.
DetectionResult: [xmin, ymin, xmax, ymax, score, label_id]
268.500000,91.562500, 329.500000, 290.500000, 0.822266, 0
104.312500,84.187500, 129.500000, 171.500000, 0.810547, 0
172.375000,81.875000, 194.375000, 172.000000, 0.762207, 0
68.437500,47.156250, 82.312500, 96.187500, 0.759766, 0
380.000000,117.250000, 398.000000, 183.125000, 0.750977, 0
213.125000,41.343750, 224.125000, 82.125000, 0.666016, 0
246.125000,42.875000, 257.500000, 85.500000, 0.535645, 0
( )
( )
331.750000,119.625000, 389.750000, 283.500000, 0.502930, 0
229.000000,46.625000, 245.125000, 105.937500, 0.500000, 0
37.968750,140.625000, 72.687500, 178.875000, 0.493164, 0
122.000000,47.031250, 129.500000, 63.625000, 0.430420, 0
15.351562,119.625000, 35.531250, 156.750000, 0.406006, 0
328.000000,118.000000, 361.500000, 276.500000, 0.348145, 0
229.000000,45.750000, 239.750000, 85.875000, 0.334229, 0
234.000000,56.343750, 248.875000, 109.875000, 0.318115, 0
0.632812,154.875000, 24.468750, 177.250000, 0.354248, 24
106.250000,89.875000, 399.500000, 354.250000, 0.607422, 33
107.000000,87.250000, 206.250000, 354.000000, 0.363037, 33
24.4
https://github.com/PaddlePaddle/FastDeploy/tree/develop/docs
https://github.com/PaddlePaddle/FastDeploy/tree/develop/docs/cn/faq/rknpu2
https://github.com/rockchip-linux/rknn-toolkit2
25 TensorFlow
TensorFlow dataflow programming
machine learning DistBelief
TensorFlow tf.keras.Sequential
RKNN RK
25.1 tensorflow
#
python3 -m venv .tensorflow_venv
TensorFlow
25.2
tf.keras.Sequential
25.2.1
1: tensorflow_classification.py( )
1 #
2 import pathlib
3 dataset_url = "https://storage.googleapis.com/download.tensorflow.org/
,→ example_images/flower_photos.tgz"
4 data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url,␣
,→ untar=True)
5 data_dir = pathlib.Path(data_dir)
6
7 #
8 batch_size = 32
9 img_height = 180
10 img_width = 180
11
( )
( )
12 # tf.keras.utils
13 train_ds = tf.keras.utils.image_dataset_from_directory(
14 data_dir,
15 validation_split=0.2,
16 subset="training",
17 seed=123,
18 image_size=(img_height, img_width),
19 batch_size=batch_size)
20
21 val_ds = tf.keras.utils.image_dataset_from_directory(
22 data_dir,
23 validation_split=0.2,
24 subset="validation",
25 seed=123,
26 image_size=(img_height, img_width),
27 batch_size=batch_size)
28
29 class_names = train_ds.class_names
30 #print(class_names)
31
32 #
33 normalization_layer = layers.Rescaling(1./255)
34 train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
35 val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y))
36 num_classes = len(class_names)
25.2.2
2: tensorflow_classification.py( )
1 #
2 model = Sequential([
3 layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
4 layers.Conv2D(16, 3, padding='same', activation='relu'),
5 layers.MaxPooling2D(),
6 layers.Conv2D(32, 3, padding='same', activation='relu'),
7 layers.MaxPooling2D(),
8 layers.Conv2D(64, 3, padding='same', activation='relu'),
9 layers.MaxPooling2D(),
10 layers.Flatten(),
11 layers.Dense(128, activation='relu'),
12 layers.Dense(num_classes)
13 ])
14
15 # adam
16 model.compile(optimizer='adam',
17 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_
,→ logits=True),
18 metrics=['accuracy'])
19
20 #
21 model.summary()
22
23 #
24 epochs=10
( )
( )
25 model.fit(
26 train_ds,
27 validation_data=val_ds,
28 epochs=epochs,
29 )
3:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
rescaling (Rescaling) (None, 180, 180, 3) 0
_________________________________________________________________
conv2d (Conv2D) (None, 180, 180, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 90, 90, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 90, 90, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 45, 45, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 45, 45, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 22, 22, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 30976) 0
_________________________________________________________________
dense (Dense) (None, 128) 3965056
_________________________________________________________________
dense_1 (Dense) (None, 5) 645
=================================================================
( )
( )
Total params: 3,989,285
Trainable params: 3,989,285
Non-trainable params: 0
_________________________________________________________________
( )
Epoch 10/10
92/92 [==============================] - 51s 555ms/step - loss: 0.0194 -␣
,→ accuracy: 0.9973 - val_loss: 2.4841 - val_accuracy: 0.5886
Keras
tf.keras.layers.RandomFlip, tf.keras.layers.RandomRotation tf.keras.layers.RandomZoom
Dropout
4: tensorflow_classification.py( )
1 # data_augmentation
2 data_augmentation = keras.Sequential(
3 [
4 layers.RandomFlip("horizontal",
5 input_shape=(img_height,
6 img_width,
7 3)),
8 layers.RandomRotation(0.1),
9 layers.RandomZoom(0.1),
10 ]
11 )
12
13 model = Sequential([
14 data_augmentation, # data_augmentation
( )
( )
15 layers.Conv2D(16, 3, padding='same', activation='relu'),
16 layers.MaxPooling2D(),
17 layers.Conv2D(32, 3, padding='same', activation='relu'),
18 layers.MaxPooling2D(),
19 layers.Conv2D(64, 3, padding='same', activation='relu'),
20 layers.MaxPooling2D(),
21 layers.Dropout(0.2), # Dropout
22 layers.Flatten(),
23 layers.Dense(128, activation='relu'),
24 layers.Dense(num_classes, name="outputs")
25 ])
Epoch 1/15
2023-02-20 19:39:40.074247: I tensorflow/stream_executor/cuda/cuda_dnn.
,→ cc:369] Loaded cuDNN version 8202
92/92 [==============================] - 115s 759ms/step - loss: 1.3321 -␣
,→ accuracy: 0.4179 - val_loss: 1.0552 - val_accuracy: 0.5872
Epoch 2/15
92/92 [==============================] - 58s 635ms/step - loss: 1.0306 -␣
,→ accuracy: 0.5978 - val_loss: 0.9923 - val_accuracy: 0.6267
Epoch 3/15
92/92 [==============================] - 58s 635ms/step - loss: 0.9465 -␣
,→ accuracy: 0.6345 - val_loss: 0.9616 - val_accuracy: 0.6240
Epoch 4/15
92/92 [==============================] - 61s 661ms/step - loss: 0.8700 -␣
,→ accuracy: 0.6604 - val_loss: 0.8555 - val_accuracy: 0.6717
Epoch 5/15
92/92 [==============================] - 61s 665ms/step - loss: 0.7899 -␣
,→ accuracy: 0.6993 - val_loss: 0.8514 - val_accuracy: 0.6717
Epoch 6/15
( )
( )
92/92 [==============================] - 57s 620ms/step - loss: 0.7789 -␣
,→ accuracy: 0.6979 - val_loss: 0.7658 - val_accuracy: 0.6907
Epoch 7/15
92/92 [==============================] - 59s 640ms/step - loss: 0.7197 -␣
,→ accuracy: 0.7159 - val_loss: 0.7940 - val_accuracy: 0.6826
Epoch 8/15
92/92 [==============================] - 59s 644ms/step - loss: 0.6956 -␣
,→ accuracy: 0.7367 - val_loss: 0.7710 - val_accuracy: 0.6907
Epoch 9/15
92/92 [==============================] - 60s 657ms/step - loss: 0.6532 -␣
,→ accuracy: 0.7510 - val_loss: 0.7250 - val_accuracy: 0.7112
Epoch 10/15
92/92 [==============================] - 62s 672ms/step - loss: 0.6189 -␣
,→ accuracy: 0.7657 - val_loss: 0.7893 - val_accuracy: 0.7044
Epoch 11/15
92/92 [==============================] - 60s 652ms/step - loss: 0.6221 -␣
,→ accuracy: 0.7674 - val_loss: 0.6982 - val_accuracy: 0.7262
Epoch 12/15
92/92 [==============================] - 60s 651ms/step - loss: 0.5598 -␣
,→ accuracy: 0.7888 - val_loss: 0.6821 - val_accuracy: 0.7357
Epoch 13/15
92/92 [==============================] - 60s 649ms/step - loss: 0.5519 -␣
,→ accuracy: 0.7885 - val_loss: 0.7939 - val_accuracy: 0.7084
Epoch 14/15
92/92 [==============================] - 61s 667ms/step - loss: 0.5387 -␣
,→ accuracy: 0.7997 - val_loss: 0.8331 - val_accuracy: 0.6880
Epoch 15/15
92/92 [==============================] - 60s 653ms/step - loss: 0.5068 -␣
,→ accuracy: 0.8151 - val_loss: 0.6627 - val_accuracy: 0.7466
Keras Dropout
25.2.3
5: tensorflow_classification.py( )
1 #
2 sunflower_url = "https://storage.googleapis.com/download.tensorflow.org/
,→ example_images/592px-Red_sunflower.jpg"
3 sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_
,→ url)
4
5 img = tf.keras.utils.load_img(
6 sunflower_path, target_size=(img_height, img_width)
7 )
8 img_array = tf.keras.utils.img_to_array(img)
9 img_array = tf.expand_dims(img_array, 0)
10
11 #
12 predictions = model.predict(img_array)
13 score = tf.nn.softmax(predictions[0])
14
15 #
16 print(
17 "This image most likely belongs to {} with a {:.2f} percent confidence."
18 .format(class_names[np.argmax(score)], 100 * np.max(score))
19 )
6: tensorflow_classification.py( )
TensorFlow Lite
25.2.5
rknn-Toolkit2 RKNN
7: rknn_transfer.py( )
1 img_height = 180
2 img_width = 180
3 IMG_PATH = 'test.jpg'
4 class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
5
6 if __name__ == '__main__':
7
12 # Pre-process config
13 print('--> Config model')
14 rknn.config(mean_values=[0, 0, 0], std_values=[255, 255, 255], target_
,→ platform='rk3568') ( )
( )
15 print('done')
16
17 # Load model
18 print('--> Loading model')
19 ret = rknn.load_tflite(model='model.tflite')
20 if ret != 0:
21 print('Load model failed!')
22 exit(ret)
23 print('done')
24
25 # Build model
26 print('--> Building model')
27 ret = rknn.build(do_quantization=False)
28 #ret = rknn.build(do_quantization=True,dataset='./dataset.txt')
29 if ret != 0:
30 print('Build model failed!')
31 exit(ret)
32 print('done')
33
( )
47 # exit(ret)
48 print('done')
49
50 img = cv2.imread(IMG_PATH)
51 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
52 img = cv2.resize(img,(180,180))
53 img = np.expand_dims(img, 0)
54
65 print(
66 "This image most likely belongs to {} with a {:.2f} percent confidence."
67 .format(class_names[np.argmax(outputs)], 100 * np.max(outputs))
68 )
69 #print(" :", class_names[np.argmax(outputs)])
70 print('done')
71
72 rknn.release()
( )
--> Loading model
done
--> Building model
done
--> Export rknn model
done
--> Init runtime environment
W init_runtime: Target is None, use simulator!
done
--> Running model
Analysing : 100%|███████████████████████████████████████████████████| 18/18␣
,→ [00:00<00:00, 90.12it/s]
Preparing : 100%|███████████████████████████████████████████████████| 18/18␣
,→ [00:01<00:00, 13.98it/s]
[array([[-4.5390625 , -1.2275391 , -0.47338867, 4.75 , 0.34350586]],
dtype=float32)]
tf.Tensor([[[9.0598274e-05 2.4848275e-03 5.2822586e-03 9.8018610e-01 1.
,→ 1956180e-02]]], shape=(1, 1, 5), dtype=float32)
This image most likely belongs to sunflowers with a 98.02 percent␣
,→ confidence.
done
25.3
rknn-toolkit-lite2 RK356X
8: rknnlite_inference.py( )
1 IMG_PATH = 'test1.jpg'
2 RKNN_MODEL = 'model.rknn'
( )
( )
3 img_height = 180
4 img_width = 180
5 class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
6
26 # load image
27 img = cv2.imread(IMG_PATH)
28 img = cv2.resize(img,(180,180))
29 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
30 img = np.expand_dims(img, 0)
31
32 # runing model
33 print('--> Running model')
34 outputs = rknn_lite.inference(inputs=[img])
( )
( )
35 print("result: ", outputs)
36 print(
37 "This image most likely belongs to {}."
38 .format(class_names[np.argmax(outputs)])
39 )
40
41 rknn_lite.release()
25.4
Tensorflow RKNN
70% Tensorflow
25.5
https://tensorflow.google.cn/learn?hl=zh-cn
https://tensorflow.google.cn/tutorials/images/classification
https://tensorflow.google.cn/tutorials?hl=zh-cn
26 ResNet18 PyTorch
ResNet18 18
ResNet residual connection
26.1.1 PyTorch
PyTorch PyTorch
ubuntu20.04
PyTorch Previous
version of PyTorch
GPU GPU
CUDA cuDNN Pytorch CUDA
# python
>>> import torch
>>> torch.__version__
'1.10.1+cu102'
( )
( )
# CUDA PyTorch PyTorch CUDA
>>> torch.version.cuda
'10.2'
>>> torch.cuda.is_available()
True
# PyTorch docker
26.1.2 ResNet18
ResNet
ResNet ResNet18 ResNet34 ResNet50 ResNet101 ResNet152
18 BN ResNet18
3x3 BN ReLU
26.2 ResNet18
26.2.1
1: resnet18.py( )
1 # torchvision
2 # download=True ./data
( )
( )
9 # classes = ('plane', 'car', 'bird', 'cat','deer', 'dog', 'frog', 'horse',
,→ 'ship', 'truck')
2: resnet18.py( )
1 #
2 transform_train=torchvision.transforms.Compose([
3 torchvision.transforms.Pad(4),
4 torchvision.transforms.RandomHorizontalFlip(), #
5 torchvision.transforms.RandomCrop(32), # 32*32
6 torchvision.transforms.ToTensor(), # Tensor 0-255
0-1
7 #torchvision.transforms.Normalize((0.4914, 0.4822, 0.4465),(0.2023,␣
,→ 0.1994, 0.2010))
8 torchvision.transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5]) #
9 ])
10 transform_test=torchvision.transforms.Compose([
11 torchvision.transforms.ToTensor(),
12 #torchvision.transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023,
,→ 0.1994, 0.2010))
13 torchvision.transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])#
14 ])
26.2.2
3: resnet18.py( )
1 #
2 class ResidualBlock(nn.Module):
3 def __init__(self, in_channels, out_channels, stride=1, downsample=None):
4 super(ResidualBlock, self).__init__()
5 self.conv1 = conv3x3(in_channels, out_channels, stride)
6 self.bn1 = nn.BatchNorm2d(out_channels)
7 self.relu = nn.ReLU(inplace=True) #
8 self.conv2 = conv3x3(out_channels, out_channels)
9 self.bn2 = nn.BatchNorm2d(out_channels)
10 self.downsample = downsample # shortcut
11 def forward(self, x):
12 residual=x
13 out = self.conv1(x)
14 out = self.bn1(out)
15 out = self.relu(out)
16 out = self.conv2(out)
17 out = self.bn2(out)
18 if(self.downsample):
19 residual = self.downsample(x)
20 out += residual
21 out = self.relu(out)
22 return out
23
24 # nn.model __init__
25 # forward
26 class ResNet(torch.nn.Module):
27 def __init__(self, block, layers, num_classes):
28 super(ResNet, self).__init__()
29 self.in_channels = 16
( )
( )
30 self.conv = conv3x3(3, 16)
31 self.bn = torch.nn.BatchNorm2d(16)
32 self.relu = torch.nn.ReLU(inplace=True)
33 self.layer1 = self._make_layers(block, 16, layers[0])
34 self.layer2 = self._make_layers(block, 32, layers[1], 2)
35 self.layer3 = self._make_layers(block, 64, layers[2], 2)
36 self.layer4 = self._make_layers(block, 128, layers[3], 2)
37 self.avg_pool = torch.nn.AdaptiveAvgPool2d((1, 1))
38 self.fc = torch.nn.Linear(128, num_classes)
39
40 # _make_layers shortcut
41 def _make_layers(self, block, out_channels, blocks, stride=1):
42 downsample = None
43 if (stride != 1) or (self.in_channels != out_channels): #
1
44 downsample = torch.nn.Sequential( #␣
,→ stride==2
45 conv3x3(self.in_channels, out_channels, stride=stride),
46 torch.nn.BatchNorm2d(out_channels)
47 )
48 layers = []
49 layers.append(block(self.in_channels, out_channels, stride,␣
,→ downsample))
50 self.in_channels = out_channels
51 for i in range(1, blocks):
52 layers.append(block(out_channels, out_channels))
53 return torch.nn.Sequential(*layers)
54
( )
59 out = self.layer1(out)
60 out = self.layer2(out)
61 out = self.layer3(out)
62 out = self.layer4(out)
63 out = self.avg_pool(out)
64 out = out.view(out.size(0), -1)
65 out = self.fc(out)
66 return out
67
71 # model
72 print(f"Model structure: {model}\n\n")
73
( )
(bn1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
)
(1): ResidualBlock(
(conv1): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
)
)
(layer2): Sequential(
(0): ResidualBlock(
(conv1): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(downsample): Sequential(
( )
( )
(0): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2),␣
,→ padding=(1, 1), bias=False)
(1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True,␣
,→ track_running_stats=True)
)
)
(1): ResidualBlock(
(conv1): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
)
)
(layer3): Sequential(
(0): ResidualBlock(
(conv1): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(downsample): Sequential(
(0): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2),␣
,→ padding=(1, 1), bias=False)
( )
( )
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True,␣
,→ track_running_stats=True)
)
)
(1): ResidualBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
)
)
(layer4): Sequential(
(0): ResidualBlock(
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(downsample): Sequential(
(0): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2),␣
,→ padding=(1, 1), bias=False)
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True,␣
,→ track_running_stats=True)
( )
( )
)
)
(1): ResidualBlock(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1),␣
,→ padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_
,→ running_stats=True)
)
)
(avg_pool): AdaptiveAvgPool2d(output_size=(1, 1))
(fc): Linear(in_features=128, out_features=10, bias=True)
)
26.2.3
4: resnet18.py( )
1 if __name__ == "__main__":
2 #
3 total_step = len(train_loader)
4 for epoch in range(0,num_epoches):
5 for i, (images, labels) in enumerate(train_loader):
6 images = images.to(device=device)
7 labels = labels.to(device=device)
8 outputs = model(images)
9 loss = criterion(outputs, labels)
( )
( )
10 #
11 optimizer.zero_grad()
12 #
13 loss.backward()
14 #
15 optimizer.step()
16 if (i+1) % total_step == 0:
17 print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
18 .format(epoch+1, num_epoches, i+1, total_step, loss.
,→ item()))
19 print("Finished Tranining")
correct = 0
total = 0
images = images.to(device=device)
labels = labels.to(device=device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
( )
( )
89.8400%
26.2.4 onnx
torch.onnx.export onnx pt
5: resnet18.py( )
26.2.5 RKNN
6: resnet18.py( )
1 def show_perfs(perfs):
2 perfs = 'perfs: {}\n'.format(perfs)
3 print(perfs)
4
5 def softmax(x):
6 return np.exp(x)/sum(np.exp(x))
7
8 if __name__ == '__main__':
9
10 MODEL = './resnet18_pytorch.onnx'
11
12 # RKNN
13 # verbose=True
14 # rknn = RKNN(verbose=True)
15 rknn = RKNN()
16
17 #
18 print('--> Config model')
19 rknn.config(mean_values=[125.307, 122.961, 113.8575], std_values=[51.
,→ 5865, 50.847, 51.255], target_platform='rk3568')
( )
( )
20 print('done')
21
22 #
23 print('--> Loading model')
24 #ret = rknn.load_pytorch(model=model, input_size_list=input_size_list)
25 ret = rknn.load_onnx(model=MODEL)
26 if ret != 0:
27 print('Load model failed!')
28 exit(ret)
29 print('done')
30
31 #
32 print('--> Building model')
33 ret = rknn.build(do_quantization=False)
34 if ret != 0:
35 print('Build model failed!')
36 exit(ret)
37 print('done')
38
39 # rknn
40 print('--> Export rknn model')
41 ret = rknn.export_rknn('./resnet_18_100.rknn')
42 if ret != 0:
43 print('Export rknn model failed!')
44 exit(ret)
45 print('done')
46
47 #
48 img = cv2.imread('./0_125.jpg')
49 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
50 img = cv2.resize(img,(32,32))
51 img = np.expand_dims(img, 0)
( )
( )
52
53 #
54 print('--> Init runtime environment')
55 ret = rknn.init_runtime()
56 if ret != 0:
57 print('Init runtime environment failed!')
58 exit(ret)
59 print('done')
60
61 #
62 print('--> Running model')
63 outputs = rknn.inference(inputs=[img])
64 np.save('./pytorch_resnet18_qat_0.npy', outputs[0])
65 #show_outputs(softmax(np.array(outputs[0][0])))
66 print(outputs)
67 print('done')
68
69 rknn.release()
26.2.6
26.2.6.1
7: rknnlite_inference0.py
1 IMG_PATH = '0_125.jpg'
2 RKNN_MODEL = './resnet_18_100.rknn'
3 img_height = 32
4 img_width = 32
5 class_names = ["plane","car","bird","cat","deer","dog","frog","horse","ship
,→ ","truck"]
( )
( )
6
26 # load image
27 img = cv2.imread(IMG_PATH)
28 img = cv2.resize(img,(32,32))
29 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
30 img = np.expand_dims(img, 0)
31
32 # runing model
33 print('--> Running model')
34 outputs = rknn_lite.inference(inputs=[img])
35 print("result: ", outputs)
36 print(
37 "This image most likely belongs to {}."
( )
( )
38 .format(class_names[np.argmax(outputs)])
39 )
40 rknn_lite.release()
26.2.6.2
.jpg
8: cifar10_to_jpg.py
1 # CIFAR-10
2 base_dir = "/mnt/e/Users/Administrator/Desktop/wsl_user/pytorch/"
3 data_dir = os.path.join(base_dir, "data", "cifar-10-batches-py")
4 test_o_dir = os.path.join( base_dir, "Data", "cifar-10-png", "raw_test")
5
6 #
7 def unpickle(file):
8 with open(file, 'rb') as fo:
9 dict_ = pickle.load(fo, encoding='bytes')
10 return dict_
11
12 #
13 if __name__ == '__main__':
14 print("start...")
15 test_data_path = os.path.join(data_dir, "test_batch")
16 test_data = unpickle(test_data_path)
17 for i in range(0, 10000):
18 img = np.reshape(test_data[b'data'][i], (3, 32, 32))
19 img = img.transpose(1, 2, 0)
20
21 label_num = str(test_data[b'labels'][i])
22 o_dir = os.path.join(test_o_dir, label_num)
23 if not os.path.isdir(o_dir):
24 os.makedirs(o_dir)
25
9: rknnlite_inference1.py
1 def rknn_inference(root):
2 total=0
3 correct=0
4 for path in os.listdir(root):
5 image_filenames = os.listdir(root + '/' + path)
6 for image_filename in image_filenames:
7 img = cv2.imread(root + '/' + path + '/' + image_filename)
8 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9 outputs = rknn_lite.inference(inputs=[img])
10 total += 1
11 if np.argmax(outputs) == int(path[:1]) :
12 correct += 1
13 print("corrorect={}, total={}".format(correct,total))
14 print(' {} :{:.2f} %'.format(total,100 * correct /␣
,→ total))
( )
9 10000 :71.21 %
10 done
26.3
https://pytorch.org/vision/main/models/generated/torchvision.models.resnet18.html
https://pytorch.org/hub/pytorch_vision_resnet/
https://arxiv.org/abs/1512.03385
27 YOLOv5
Yolov5 COCO
Ultralytics AI
YOLOv5 v7.0 YOLOv5n YOLOv5s
YOLOv5m YOLOv5l YOLOv5x
YOLOv5
YOLOv5
27.1 YOLOv5
python YOLOv5
# yolov5 pytorch
git clone https://github.com/ultralytics/yolov5
cd yolov5
( )
( )
#
pip install -r requirements.txt
# python
import torch
import utils
display = utils.notebook_init()
#
Checking setup...
YOLOv5 � 2023-2-20 Python-3.8.10 torch-1.10.1+cpu CPU
Setup complete � (6 CPUs, 12.4 GB RAM, 77.3/251.0 GB disk)
27.2 YOLOv5
27.2.1
27.2.2 YOLOv5
yolov5 ./data/images/
#
# --source
# --weights
python detect.py --source ./data/images/ --weights yolov5s.pt
runs/detect/exp2
27.2.3 rknn
yolov5s.pt rknn
# onnx
pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime
# yolov5s.pt
# onnx
python export.py --weights yolov5s.pt --include onnx
# torchscript
python export.py --weights yolov5s.pt --include torchscript
(.yolov5_env) llh@YH-LONG:/mnt/e/Users/Administrator/Desktop/wsl_user/
,→ yolov5/yolov5-7.0$ python export.py --weights yolov5s.pt --include onnx
export: data=data/coco128.yaml, weights=['yolov5s.pt'], imgsz=[640, 640],␣
,→ batch_size=1, device=cpu, half=False, inplace=False, keras=False,␣
,→ optimize=False, int8=False, dynamic=False, simplify=False, opset=12,␣
,→ verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_
,→ class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['onnx']
YOLOv5 � 2023-2-28 Python-3.8.10 torch-1.10.1+cpu CPU
Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients, 16.4 GFLOPs
PyTorch: starting from yolov5s.pt with output shape (1, 25200, 85) (14.1 MB)
( )
Validate: python val.py --weights yolov5s.onnx
PyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'custom',
,→ 'yolov5s.onnx')
Visualize: https://netron.app
yolov5s.onnx Netron
yolov5
1: models/yolo.py
return z
2: export.py
3 rknn
rknn-Toolkit2
3: rknn_transfer.py( )
1 if __name__ == '__main__':
2
3 # RKNN
4 # verbose=True
5 # rknn = RKNN(verbose=True)
6 rknn = RKNN()
7
8 #
9 img = cv2.imread(IMG_PATH)
10 # img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))
11 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
12 img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
13
14 # pre-process config
15 print('--> Config model')
16 rknn.config(mean_values=[[0, 0, 0]], std_values=[[255, 255, 255]],␣
,→ target_platform="rk3568")
17 print('done')
18
19 # torchscript .pt
20 #print('--> Loading model')
21 #ret = rknn.load_pytorch(model=PYTORCH_MODEL, input_size_list=[[1,3,IMG_
,→ SIZE,IMG_SIZE]])
22 #if ret != 0:
23 # print('Load model failed!')
24 # exit(ret)
25 #print('done')
26
( )
( )
27 # onnx
28 print('--> Loading model')
29 ret = rknn.load_onnx(model=ONNX_MODEL)
30 if ret != 0:
31 print('Load model failed!')
32 exit(ret)
33 print('done')
34
35 # ,
36 print('--> Building model')
37 ret = rknn.build(do_quantization=True, dataset=DATASET)
38 #ret = rknn.build(do_quantization=False)
39 if ret != 0:
40 print('Build model failed!')
41 exit(ret)
42 print('done')
43
44 #
45 #print('--> Accuracy analysis')
46 #Ret = rknn.accuracy_analysis(inputs=[img])
47 #if ret != 0:
48 # print('Accuracy analysis failed!')
49 # exit(ret)
50 #print('done')
51
52 # RKNN
53 print('--> Export rknn model')
54 ret = rknn.export_rknn(RKNN_MODEL)
55 #if ret != 0:
56 # print('Export rknn model failed!')
57 # exit(ret)
58 print('done')
( )
( )
59
60 # device_id
61 print('--> Init runtime environment')
62 ret = rknn.init_runtime()
63 # ret = rknn.init_runtime(target='rk3568', device_id='192.168.103.
,→ 115:5555', perf_debug=True)
64 if ret != 0:
65 print('Init runtime environment failed!')
66 exit(ret)
67 print('done')
68
69 # ,
70 # rknn.eval_perf(inputs=[img], is_print=True)
71
72 rknn.release()
#
python test.py
# yolov5s.rknn
W __init__: rknn-toolkit2 version: 1.4.0-22dcfef4
--> Config model
done
--> Loading model
done
--> Building model
Analysing : 100%|███████████████████████████████████████████████| 145/145␣
,→ [00:00<00:00, 2992.56it/s]
Quantizating : 100%|█████████████████████████████████████████████| 145/145␣
,→ [00:00<00:00, 320.39it/s]
W build: The default input dtype of 'images' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
( )
( )
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
W build: The default output dtype of 'output0' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
W build: The default output dtype of '329' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
W build: The default output dtype of '331' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
done
--> Export rknn model
done
# eval_perf rknn.init_runtime
--> Init runtime environment
W init_runtime: Target is None, use simulator!
W init_runtime: Flag perf_debug has been set, it will affect the␣
,→ performance of inference!
I NPUTransfer: Starting NPU Transfer Client, Transfer version 2.1.0␣
,→ (b5861e7@2020-11-23T11:50:36)
D RKNNAPI: ==============================================
D RKNNAPI: RKNN VERSION:
D RKNNAPI: API: 1.4.0 (bb6dac9 build: 2022-08-29 16:17:01)(null)
D RKNNAPI: DRV: rknn_server: 1.3.0 (121b661 build: 2022-04-29 11:11:47)
D RKNNAPI: DRV: rknnrt: 1.4.0 (a10f100eb@2022-09-09T09:07:14)
D RKNNAPI: ==============================================
done
( )
( )
=====================================================================================
Performance
#### The performance result is just for debugging, ####
#### may worse than actual performance! ####
=====================================================================================
Total Weight Memory Size: 7312768
Total Internal Memory Size: 7782400
Predict Internal Memory RW Amount: 138880000
Predict Weight Memory RW Amount: 7312768
ID OpType DataType Target InputShape ␣
,→ OutputShape DDR Cycles NPU Cycles Total Cycles ␣
,→ Time(us) MacUsage(%) RW(KB) FullName
( )
148 OutputOperator INT8 CPU (1,255,20,20),(1,20,20,256) ␣
,→ \ 0 0 0 ␣
,→ 34 \ 220.00 OutputOperator:331
Total Operator Elapsed Time(us): 77776
=====================================================================================
rknn eval_perf
27.2.4
4: rknnlite_inference.py( )
1 if __name__ == '__main__':
2
3 # RKNNLite
4 rknn_lite = RKNNLite()
5
6 # rknn
7 print('--> Load RKNN model')
8 ret = rknn_lite.load_rknn(rknn_model)
9 if ret != 0:
10 print('Load RKNN model failed')
11 exit(ret)
12 print('done')
13
14 #
15 print('--> Init runtime environment')
16 ret = rknn_lite.init_runtime()
( )
( )
17 if ret != 0:
18 print('Init runtime environment failed!')
19 exit(ret)
20 print('done')
21
22 #
23 img = cv2.imread(IMG_PATH)
24 img, ratio, (dw, dh) = letterbox(img, new_shape=(640, 640)) #
25 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
26
27 #
28 print('--> Running model')
29 outputs = rknn_lite.inference(inputs=[img])
30 print('done')
31
32 #
33 boxes, classes, scores = yolov5_post_process(outputs)
34
39 #
40 cv2.imwrite("out.jpg", img_1)
41 # cv2.imshow("post process result", img_1)
42 # cv2.waitKey(0)
43 # cv2.destroyAllWindows()
44
45 rknn_lite.release()
out.jpg
27.3 airockchip/yolov5
airockchip/yolov5
# v6.2
git clone https://github.com/airockchip/yolov5.git
cd yolov5
# yolov5s.pt --cfg
python train.py --data coco128.yaml --weights yolov5s.pt --img 640
( )
( )
#
...
Optimizer stripped from runs/train/exp/weights/last.pt, 14.9MB
Optimizer stripped from runs/train/exp/weights/best.pt, 14.9MB
Validating runs/train/exp/weights/best.pt...
...
# runs/train/exp/
runs/train/exp/weights/ yolov5s_relu.pt
torchscript
# --weights
# --rknpu (rk_platform rk1808, rv1109, rv1126, rk3399pro,␣
,→ rk3566, rk3568, rk3588, rv1103, rv1106)
# --include onnx torchscript
#
export: data=data/coco128.yaml, weights=['yolov5s.pt'], imgsz=[640, 640],␣
,→ batch_size=1, device=cpu, half=False, inplace=False, train=False,␣
,→ keras=False, optimize=False, int8=False, dynamic=False, simplify=False,␣
,→ opset=12, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_
,→ per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=[
,→ 'torchscript'], rknpu=rk3568
YOLOv5 � 2022-10-28 Python-3.8.10 torch-1.10.1+cpu CPU
Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients, 16.4 GFLOPs
---> save anchors for RKNN
( )
( )
[[10.0, 13.0], [16.0, 30.0], [33.0, 23.0], [30.0, 61.0], [62.0, 45.0], [59.
,→ 0, 119.0], [116.0, 90.0], [156.0, 198.0], [373.0, 326.0]]
PyTorch: starting from yolov5s.pt with output shape (1, 255, 80, 80) (14.1␣
,→ MB)
# onnx yolov5s.onnx
python export.py --weights yolov5s.pt --include onnx --rknpu rk3568
rknn ,
,
# rknn
W __init__: rknn-toolkit2 version: 1.4.0-22dcfef4
( )
( )
--> Config model
done
--> Loading model
PtParse: 100%|███████████████████████████████████████████████████| 698/698␣
,→ [00:01<00:00, 602.67it/s]
done
--> Building model
Analysing : 100%|████████████████████████████████████████████████| 145/145␣
,→ [00:00<00:00, 381.87it/s]
Quantizating : 100%|█████████████████████████████████████████████| 145/145␣
,→ [00:00<00:00, 518.54it/s]
W build: The default input dtype of 'x.1' is changed from 'float32' to 'int8
,→ ' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
W build: The default output dtype of '172' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
W build: The default output dtype of '173' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
W build: The default output dtype of '174' is changed from 'float32' to
,→ 'int8' in rknn model for performance!
Please take care of this change when deploy rknn model␣
,→ with Runtime API!
done
--> Export rknn model
done
--> Init runtime environment
W init_runtime: Target is None, use simulator!
( )
( )
W init_runtime: Flag perf_debug has been set, it will affect the␣
,→ performance of inference!
I NPUTransfer: Starting NPU Transfer Client, Transfer version 2.1.0␣
,→ (b5861e7@2020-11-23T11:50:36)
D RKNNAPI: ==============================================
D RKNNAPI: RKNN VERSION:
D RKNNAPI: API: 1.4.0 (bb6dac9 build: 2022-08-29 16:17:01)(null)
D RKNNAPI: DRV: rknn_server: 1.3.0 (121b661 build: 2022-04-29 11:11:47)
D RKNNAPI: DRV: rknnrt: 1.4.0 (a10f100eb@2022-09-09T09:07:14)
D RKNNAPI: ==============================================
done
=====================================================================================
Performance
#### The performance result is just for debugging, ####
#### may worse than actual performance! ####
=====================================================================================
Total Weight Memory Size: 7312768
Total Internal Memory Size: 6144000
Predict Internal Memory RW Amount: 86931200
Predict Weight Memory RW Amount: 7312768
ID OpType DataType Target InputShape ␣
,→ OutputShape DDR Cycles NPU Cycles Total Cycles ␣
,→ Time(us) MacUsage(%) RW(KB) FullName
( )
.....
80 Conv INT8 NPU (1,512,20,20),(255,512,1,1),(255) ␣
,→ (1,255,20,20) 66931 102000 102000 ␣
,→ 180 62.96 429.50 Conv:1189_Conv
81 Sigmoid INT8 NPU (1,255,20,20) ␣
,→ (1,255,20,20) 31167 0 31167 ␣
,→ 141 \ 200.00 Sigmoid:1190_Sigmoid
82 OutputOperator INT8 CPU (1,255,80,80),(1,80,80,256) ␣
,→ \ 0 0 0 ␣
,→ 161 \ 3200.00 OutputOperator:172
83 OutputOperator INT8 CPU (1,255,40,40),(1,40,40,256) ␣
,→ \ 0 0 0 ␣
,→ 46 \ 800.00 OutputOperator:173
84 OutputOperator INT8 CPU (1,255,20,20),(1,20,20,256) ␣
,→ \ 0 0 0 ␣
,→ 24 \ 220.00 OutputOperator:174
Total Operator Elapsed Time(us): 44412
=====================================================================================
27.4
https://github.com/ultralytics/yolov5
https://github.com/airockchip/yolov5/tree/master
https://github.com/airockchip/rknn_model_zoo
28
• lubancat 2
• Debian10
• Python Python3.7
• opencv 4.7.0.68
• Flask 1.0.2
28.1
lubancat 2 Debian10
1 #
2 sudo apt update
3 sudo apt -y install git wget
4
5 # python python3
6 sudo apt -y install python3-flask python3-pil python3-numpy python3-pip
7
8 # opencv-python 4.7.0.68
9 sudo pip install opencv-contrib-python
( )
( )
10
11 # rknn-toolkit-lite2 NPU
28.2
28.2.1
Flask web
: flask Flask
1: lubancat-flask-opencv-
rknn/controller/modules/home/views.py
8 video_camera = None
9 global_frame = None
10
( )
( )
11 #
12 @home_blu.route('/')
13 def index():
14 #
15 username = session.get("username")
16 if not username:
17 return redirect(url_for("user.login"))
18 return render_template("index.html")
19
20
21 #
22 def video_stream():
23 global video_camera
24 global global_frame
25
26 if video_camera is None:
27 video_camera = VideoCamera()
28
29 while True:
30 # JPEG
31 frame = video_camera.get_frame()
32 time.sleep(0.01)
33 if frame is not None:
34 global_frame = frame
35 yield (b'--frame\r\n'
36 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
37 else:
38 yield (b'--frame\r\n'
39 b'Content-Type: image/jpeg\r\n\r\n' + global_frame + b'\r\n\
,→ r\n')
40
41 #
( )
( )
42 @home_blu.route('/video_viewer')
43 def video_viewer():
44 #
45 username = session.get("username")
46 if not username:
47 return redirect(url_for("user.login"))
48 # Response multipart/x-mixed-replace
frame
49 return Response(video_stream(),
50 mimetype='multipart/x-mixed-replace; boundary=frame')
2: lubancat-flask-opencv-
rknn/controller/templates/index.html( )
1 <body>
2 <h1 align="center" style="color: whitesmoke;">Flask+OpenCV+Rknn</h1>
3 <div class="top">
4 <div class="recorder" id="recorder" align="center">
5 <button id="record" class="btn"> </button>
6 <button id="stop" class="btn"> </button>
7 <button id="process" class="btn"> </button>
8 <button id="pause" class="btn"> </button>
9 <input type="button" class="btn" value=" "
10 onclick="javascrtpt:window.location.href='{{ url_for('user.
,→ logout') }}'">
11 <a id="download"></a>
12 <script type="text/javascript" src="{{ url_for('static', filename=
,→ 'button_process.js') }}"></script>
( )
( )
13 </div>
14 </div>
15 <img id="video" src="{{ url_for('home.video_viewer') }}">
16 </body>
28.2.2
VideoCamera
3: lubancat-flask-opencv-
rknn/controller/utils/camera.py( )
1 class VideoCamera(object):
2 def __init__(self):
3 # opencv
4 self.cap = cv2.VideoCapture(0)
5 if not self.cap.isOpened():
6 raise RuntimeError('Could not open camera.')
7
8 # RKNNLite
9 self.rknn_lite = RKNNLite()
10
11 #
12 self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
13 self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
14 # ...............
15
16 #
17 def __del__(self):
18 self.cap.release()
19 self.rknn_lite.release()
( )
( )
20
21 #
22 def get_frame(self):
23 ret, self.frame = self.cap.read()
24
25 if ret:
26 #
27 if self.is_process:
28 #self.image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
29 self.image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
30 self.outputs = self.rknn_lite.inference(inputs=[self.image])
31 self.frame = process_image(self.image, self.outputs)
32 #self.rknn_frame = process_image(self.image, self.outputs)
33 #ret, image = cv2.imencode('.jpg', self.rknn_frame)
34 #return image.tobytes()
35
28.3 NPU
4:
1 # RKNNLite
2 self.rknn_lite = RKNNLite()
3
4 #...................
5
6 def load_rknn(self):
7 # RKNN
8 print('--> Load RKNN model')
9 ret = self.rknn_lite.load_rknn(RKNN_MODEL)
10 if ret != 0:
11 print('Load RKNN model failed')
12 exit(ret)
13 # init_runtime
14 print('--> Init runtime environment')
15 ret = self.rknn_lite.init_runtime()
16 if ret != 0:
17 print('Init runtime environment failed!')
18 exit(ret)
19
20 #...................
21
22 #
23 self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
24 self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
25
26 # RGB
( )
( )
27 self.image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
28
29 # inference
30 self.outputs = self.rknn_lite.inference(inputs=[self.image])
31
32 # npu
33 self.frame = process_image(self.image, self.outputs)
34
35 #
36 def __del__(self):
37 self.cap.release()
38 # release RKNNLite
39 self.rknn_lite.release()
npu opencv
opencv
controller/utils/opencvtest.py
28.4
# , main
git clone -b main https://gitee.com/LubanCat/lubancat-flask-opencv-rknn.git
IP
# lubancat-flask-opencv-rknn
cd ./lubancat-flask-opencv-rknn
# main.py
( )
( )
vim main.py
# host="0.0.0.0" ip
app.run(threaded=True, host="0.0.0.0", port=5000)
# ip
app.run(threaded=True, host="192.168.103.121",port=5000)
# ip 192.168.103.121 5000.
# Python3
python3
# opencv
import cv2
#
cap = cv2.VideoCapture(0)
cap.isOpened()
# True
#
cap.release()
video
ls /dev/video*
#
( )
( )
# lubancat-flask-opencv-rknn opencv
cd ./controller/utils/
# camera.py VideoCamera(object)
vim camera.py
# self.cap = cv2.VideoCapture(0) 0
("/dev/video0")
self.cap = cv2.VideoCapture(0)
28.5
1.
# lubancat-flask-opencv-rknn
sudo python3 main.py
http://0.0.0.0:5000
ip CTRL+C
http://192.168.103.121:5000
2.
3.
5: controller/modules/home/views.py
1 #
2 def video_stream():
3 global video_camera
4 global global_frame
5
6 if video_camera is None:
7 video_camera = VideoCamera()
8
9 while True:
10 start_time = time.time()
11 frame = video_camera.get_frame()
12 end_time = time.time()
13 print('get_frame cost %f second' % (end_time - start_time))
14 #time.sleep(0.01)
15 if frame is not None:
16 global_frame = frame
17 yield (b'--frame\r\n'
18 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
19 else:
20 yield (b'--frame\r\n'
21 b'Content-Type: image/jpeg\r\n\r\n' + global_frame + b'\r\n\
,→ r\n')
lubancat 2 debian10
Toolkit Lite2 yolov5
npu
4.
(1)
(2)
(3)
28.6
https://github.com/miguelgrinberg/flask-video-streaming
https://gitee.com/Embedfire/flask-video-streaming-recorder
https://github.com/rockchip-linux/rknn-toolkit2
29