forked from tzutalin/dlib-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·217 lines (181 loc) · 7.56 KB
/
build.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
#!/usr/bin/env python
# Copyright (c) 2017 Tzutalin
# Create by TzuTaLin <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
import sys
import argparse
import shutil
import subprocess
from subprocess import Popen, PIPE
DEVICE_ABI = 'armeabi-v7a'
class PrintColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Build dlib-android')
parser.add_argument('--jobs', dest='jobs', help='Jobs to compile',
default=4, type=int)
parser.add_argument('--android_project', dest='android_project',
help='Android JNI folder path',
default=os.path.join('androidstudio-examples', 'dlib-android-app', 'dlib', 'src', 'main', 'jniLibs'), type=str)
parser.add_argument('--clean', action='store_true',
help='clean obj and binaries')
parser.add_argument('--test', action='store_true',
help='Push executable file to data/local/tmp/, and run them')
parser.add_argument('--debug', action='store_true',
help='Enable debug build')
args = parser.parse_args()
return args
def ndk_build(args):
jobs = str(args.jobs)
isDebug = '1' if args.debug else '0'
build_cmd = ['ndk-build',
'-j' + jobs,
'NDK_LOG=1',
'NDK_DEBUG=' + isDebug,
'V=0']
# Print the build command
print PrintColors.UNDERLINE + 'ndk build arguments:' + str(build_cmd) + PrintColors.ENDC
ret = subprocess.call(build_cmd)
if ret is not 0:
print PrintColors.FAIL + 'Build Error' + PrintColors.ENDC
os.sys.exit(1)
else:
print PrintColors.OKBLUE + 'Build Pass' + PrintColors.ENDC
def ndk_clean():
subprocess.call(['ndk-build', 'clean'])
def setDeviceABI():
global DEVICE_ABI
p = subprocess.Popen(
['adb', 'shell', 'getprop', 'ro.product.cpu.abi'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(
b"input data that is passed to subprocess' stdin")
rc = p.returncode
if rc == 0:
if "x86" in output:
DEVICE_ABI = 'x86'
print PrintColors.OKBLUE + 'We will use ABI:' + DEVICE_ABI + ' binaries to test ' + PrintColors.ENDC
def test():
global DEVICE_ABI
# Test max_cost_assignment_ex daemon example
print '----max_cost_assignment_ex daemon test'
print '----Push svm_ex to phone device'
srcFolder = os.path.join('libs', DEVICE_ABI, 'max_cost_assignment_ex')
subprocess.call(['adb', 'push', srcFolder, '/data/local/tmp'])
print '----Execute /data/local/tmp/max_cost_assignment_ex'
subprocess.call(['adb', 'shell', './data/local/tmp/max_cost_assignment_ex'])
print '\n\n'
print 'Test dlib selective search'
print '----selective search algorithm'
srcFolder = os.path.join('data', 'lena.jpg')
print '----Push test image to data/local/tmp'
subprocess.call(['adb', 'push', srcFolder, '/data/local/tmp/lena.jpg'])
print '----Push daemon to /data/local/tmp'
srcFolder = os.path.join('libs', DEVICE_ABI, 'TestSelectiveSearch')
subprocess.call(['adb', 'push', srcFolder, '/data/local/tmp'])
print '----Execute /data/local/tmp/TestSelectiveSearch'
subprocess.call(
['adb', 'shell', './data/local/tmp/TestSelectiveSearch', '/data/local/tmp/lena.jpg'])
print '\n\n'
print 'Test face landmark'
srcFolder = os.path.join('data', 'lena.bmp')
subprocess.call(['adb', 'push', srcFolder, '/data/local/tmp'])
srcFolder = os.path.join('data', 'shape_predictor_68_face_landmarks.dat')
subprocess.call(['adb', 'push', srcFolder, '/data/local/tmp'])
srcFolder = os.path.join('libs', DEVICE_ABI, 'face_landmark')
subprocess.call(['adb', 'push', srcFolder, '/data/local/tmp'])
print '----Execute /data/local/tmp/face_lanmark'
subprocess.call(['adb', 'shell', './data/local/tmp/face_landmark',
'/data/local/tmp/shape_predictor_68_face_landmarks.dat', '/data/local/tmp/lena.bmp'])
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
shutil.copy2(s, d)
def copySoToAS(src, dst):
for root, subdirs, files in os.walk(src):
for f in files:
if f.endswith('.so'):
path = os.path.join(root, f)
abis = os.path.basename(os.path.dirname(path))
shutil.copy(path, os.path.join(dst, abis))
# TODO : Refactor
def test_cmake():
curr = os.getcwd()
buildFolder = 'build'
if os.path.exists(buildFolder):
shutil.rmtree(buildFolder)
os.mkdir(buildFolder)
os.chdir(buildFolder)
cmake_cmd = ['cmake']
build_cmd = ['make']
ABI = 'arm64-v8a'
# ABI = 'x86'
# TODO
NDK_PATH = '/home/darrenl/tools/android-ndk-r10e'
cmake_cmd = cmake_cmd + ['-DCMAKE_SYSTEM_NAME=Android', '-DCMAKE_SYSTEM_VERSION=21',
'-DCMAKE_ANDROID_ARCH_ABI=' + ABI, '-DCMAKE_ANDROID_STL_TYPE=gnustl_static',
'-DCMAKE_ANDROID_NDK=' + NDK_PATH]
cmake_cmd = cmake_cmd + ['-D', 'CMAKE_INSTALL_PREFIX=.']
cmake_cmd = cmake_cmd + ['..']
# Print the build command
print PrintColors.UNDERLINE + 'cmake build arguments:' + str(cmake_cmd) + PrintColors.ENDC
ret = subprocess.call(cmake_cmd)
ret = subprocess.call(build_cmd)
if ret is not 0:
print PrintColors.FAIL + 'Build Error' + PrintColors.ENDC
os.sys.exit(1)
else:
print PrintColors.OKBLUE + 'Build Pass' + PrintColors.ENDC
if __name__ == '__main__':
# Move to top-level
ROOT = os.path.dirname(os.path.abspath(__file__))
os.chdir(ROOT)
libs_dir = os.path.join(ROOT, 'libs')
obj_dir = os.path.join(ROOT, 'obj')
setDeviceABI()
args = parse_args()
if args.test:
os.environ['Test'] = "true"
if args.clean:
ndk_clean()
else:
ndk_build(args)
if args.android_project and os.path.exists(args.android_project):
srcFolder = os.path.join('libs')
copySoToAS(srcFolder, args.android_project)
if args.test:
test()
os.sys.exit(0)