-
Notifications
You must be signed in to change notification settings - Fork 18
/
install.py
293 lines (263 loc) · 8.94 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
from distutils.log import warn
import os, sys
import time
import threading
# version
# =================================================================
sys.path.append('./vilib')
user_name = os.getlogin()
from version import __version__
print("Start installing vilib %s for user %s"%(__version__ ,user_name))
# define color print
# =================================================================
def warn(msg, end='\n', file=sys.stdout, flush=False):
print(f'\033[0;33m{msg}\033[0m', end=end, file=file, flush=flush)
def error(msg, end='\n', file=sys.stdout, flush=False):
print(f'\033[0;31m{msg}\033[0m', end=end, file=file, flush=flush)
# check if run as root
# =================================================================
if os.geteuid() != 0:
warn("Script must be run as root. Try \"sudo python3 install.py\".")
sys.exit(1)
# global variables defined
# =================================================================
errors = []
avaiable_options = ['-h', '--help', '--no-dep']
usage = '''
Usage:
sudo python3 install.py [option]
Options:
--no-dep Do not download dependencies
-h --help Show this help text and exit
'''
# utils
# =================================================================
def run_command(cmd=""):
import subprocess
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
result = p.stdout.read().decode('utf-8')
status = p.poll()
return status, result
at_work_tip_sw = False
def working_tip():
char = ['/', '-', '\\', '|']
i = 0
global at_work_tip_sw
while at_work_tip_sw:
i = (i+1)%4
sys.stdout.write('\033[?25l') # cursor invisible
sys.stdout.write('%s\033[1D'%char[i])
sys.stdout.flush()
time.sleep(0.5)
sys.stdout.write(' \033[1D')
sys.stdout.write('\033[?25h') # cursor visible
sys.stdout.flush()
def do(msg="", cmd=""):
print(" - %s ... " % (msg), end='', flush=True)
# at_work_tip start
global at_work_tip_sw
at_work_tip_sw = True
_thread = threading.Thread(target=working_tip)
_thread.daemon = True
_thread.start()
# process run
status, result = run_command(cmd)
# print(status, result)
# at_work_tip stop
at_work_tip_sw = False
while _thread.is_alive():
time.sleep(0.01)
# status
if status == 0 or status == None or result == "":
print('Done')
else:
print('\033[1;35mError\033[0m')
errors.append("%s error:\n Status:%s\n Error:%s" %
(msg, status, result))
def check_rpi_model():
_, result = run_command("cat /proc/device-tree/model |awk '{print $3}'")
result = result.strip()
if result == '3':
return int(3)
elif result == '4':
return int(4)
else:
return None
def check_raspbain_version():
_, result = run_command("cat /etc/debian_version|awk -F. '{print $1}'")
return int(result.strip())
def check_python_version():
import sys
major = int(sys.version_info.major)
minor = int(sys.version_info.minor)
micro = int(sys.version_info.micro)
return major, minor, micro
def check_os_bit():
'''
# import platform
# machine_type = platform.machine()
latest bullseye uses a 64-bit kernel
This method is no longer applicable, the latest raspbian will uses 64-bit kernel
(kernel 6.1.x) by default, "uname -m" shows "aarch64",
but the system is still 32-bit.
'''
_ , os_bit = run_command("getconf LONG_BIT")
return int(os_bit)
# print system and hardware information
# =================================================================
rpi_model = check_rpi_model()
python_version = check_python_version()
raspbain_version = check_raspbain_version()
os_bit = check_os_bit()
print(f"Python version: {python_version[0]}.{python_version[1]}.{python_version[2]}")
print(f"Raspbian version: {raspbain_version} ({os_bit}bit)")
print("")
# check system
# =================================================================
if raspbain_version <= 10:
warn('System not be supported.Requires system in bullseye(11) or newer.')
print('Please use newer system or use "legacy" branch.')
sys.exit(1)
# Dependencies list installed with apt
# =================================================================
APT_INSTALL_LIST = [
"python3-libcamera",
# install python3-picamera2: https://datasheets.raspberrypi.com/camera/picamera2-manual.pdf
"python3-picamera2",
"python3-pyqt5",
"python3-opengl",
# install python3-opencv: # https://qengineering.eu/install-opencv-4.5-on-raspberry-64-os.html
"python3-opencv",
"opencv-data",
# install ffmpeg
"ffmpeg",
# install mediapipe dependencies
"libgtk-3-0",
"libxcb-shm0",
"libcdio-paranoia-dev",
"libsdl2-2.0-0",
"libxv1",
"libtheora0",
"libva-drm2",
"libva-x11-2",
"libvdpau1",
"libharfbuzz0b",
"libbluray2",
"libatlas-base-dev",
"libhdf5-103",
# "libopenexr25",
"libzbar0",
"libopenblas-dev",
]
# Dependencies list installed with pip3
# =================================================================
PIP_INSTALL_LIST = [
"tflite-runtime",
"Flask",
"imutils",
"qrcode",
"pyzbar", # pyzbar:one-dimensional barcodes and QR codes
"pyzbar[scripts]",
"readchar", # will update setuptools to the latest version
'protobuf>=3.20.0', # mediapipe need
]
# check whether mediapipe is supported
is_mediapipe_supported = False
if os_bit == 64 and raspbain_version >= 11:
is_mediapipe_supported = True
PIP_INSTALL_LIST.append("mediapipe")
else:
is_mediapipe_supported = False
warn("mediapipe is only supported on 64bit system.")
if raspbain_version > 11:
PIP_INSTALL_LIST.append("numpy")
else:
PIP_INSTALL_LIST.append("numpy==1.26.4")
# main function
# =================================================================
def install():
options = []
if len(sys.argv) > 1:
options = sys.argv[1:]
for opt in options:
if opt not in avaiable_options:
print("Option {} is not found.".format(opt))
print(usage)
sys.exit(0)
if "-h" in options or "--help" in options:
print(usage)
sys.exit(0)
if "--no-dep" not in options:
# install dependencies with apt
# ===================================
print("apt install dependency:")
do(msg="dpkg configure",
cmd='dpkg --configure -a')
do(msg="update apt-get",
cmd='apt-get update -y')
for dep in APT_INSTALL_LIST:
do(msg=f"install {dep}",
cmd=f'apt-get install {dep} -y')
# install dependencies with pip
# ===================================
print("pip3 install dependency:")
# check whether pip has the option "--break-system-packages"
_is_bsps = ''
status, _ = run_command("pip3 help install|grep break-system-packages")
if status == 0: # if true
_is_bsps = "--break-system-packages"
print("\033[38;5;8m pip3 install with --break-system-packages\033[0m")
# update pip
do(msg="update pip3",
cmd=f'python3 -m pip install --upgrade pip {_is_bsps}'
)
for dep in PIP_INSTALL_LIST:
if dep.endswith('.whl'):
dep_name = dep.split("/")[-1]
else:
dep_name = dep
do(msg=f"install {dep_name}",
cmd=f'pip3 install {dep} {_is_bsps}')
#
if is_mediapipe_supported == False:
print('\033[38;5;8m mediapipe is not supported on this platform... Skip \033[0m')
print("Create workspace")
# ===================================
if not os.path.exists('/opt'):
os.mkdir('/opt')
run_command('chmod 774 /opt')
run_command(f'chown -R {user_name}:{user_name} /opt')
do(msg="create dir",
cmd='mkdir -p /opt/vilib'
+ ' && chmod 774 /opt/vilib'
+ f' && chown -R {user_name}:{user_name} /opt/vilib'
)
do(msg="copy workspace",
cmd='cp -r ./workspace/* /opt/vilib/'
+ ' && chmod 774 /opt/vilib/*'
+ f' && chown -R {user_name}:{user_name} /opt/vilib/*'
)
print("Install vilib python package")
do(msg="run setup file",
cmd='python3 setup.py install')
do(msg="cleanup",
cmd='rm -rf vilib.egg-info')
# check errors
if len(errors) == 0:
print("Finished")
else:
print("\n\nError happened in install process:")
for error in errors:
print(error)
print("Try to fix it yourself, or contact service@sunfounder.com with this message")
if __name__ == "__main__":
try:
install()
except KeyboardInterrupt:
print("\n\nCanceled.")
finally:
sys.stdout.write(' \033[1D')
sys.stdout.write('\033[?25h') # cursor visible
sys.stdout.flush()