本文主要记录在做 ProjectA 时用到的 Python 脚本,因为用得频繁,所以做成了脚本,避免重复输命令。
项目树状结构图如下:
1 2 3 4 5 6
| . ├── CommonTips.py ├── DumpPhotos.py ├── FallWithMask.py ├── OfflineSoRunner.py └── main.py
|
它主要的功能是:
- 从 Android 手机拉取照片(需要手机连接到电脑并且安装好 adb 命令)
- 在 Android 手机上以 shell 的形式跑 so 库算法,一般是在 apk 上运行
- 这个带 mask 分析已经看不懂是什么意思了
入口:main.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
| import CommonTips import DumpPhotos import OfflineSoRunner import FallWithMask sub_model_name = 'Main Menu' op_info = '''\n--------------Main Menu-------------- | 【0】:Dump照片 | 【1】:离线跑算法库 | 【2】:带mask分析跌倒视频''' + CommonTips.tip_ops+'-------------------------------------\n' print(op_info)
while True: cmd = input('('+sub_model_name+')'+CommonTips.tip_input_cmd)
if cmd.isdigit(): cmd = int(cmd) if cmd == 0: DumpPhotos.main() elif cmd == 1: OfflineSoRunner.main() elif cmd == 2: FallWithMask.main() else: print(CommonTips.tip_arg_error) else: if 'h' == cmd.lower(): print(op_info) elif 'q' == cmd.lower(): print(CommonTips.tip_quit) break else: print(CommonTips.tip_arg_error)
|
提示输出:CommonTips.py
1 2 3 4 5 6 7 8 9
| tip_arg_error = '''-------------------------- | X 参数错误,请重新输入! | --------------------------\n''' tip_quit = "已退出子模块" tip_input_cmd = "请输入命令:" tip_ops = '''\n|************************************ | 【h】:打印本帮助 | 【q】:退出本模块 '''
|
拉取照片:DumpPhotos.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
| import os
import CommonTips
sub_model_name = 'Dump照片'
save_path = "C:\\Users\\D22433\\Desktop\\DumpPhotos\\" remote_path = "/sdcard/AlgoTest/"
pull_photo_2_local = "adb pull "+remote_path+" "+save_path del_local_photos = "del /Q "+save_path+"\\*" del_remote_photos = 'adb shell "rm -rf '+remote_path+'/*"'
op_info = '''--------------Dump照片-------------- | 【0】:从安卓端拉取dump照片至本地 | 【1】:删除本地dump照片 | 【2】:删除安卓dump照片 | 【3】:删除本地&安卓dump照片''' + CommonTips.tip_ops+'-----------------------------------'
def main(): print(op_info) while True: cmd = input('('+sub_model_name+')'+CommonTips.tip_input_cmd)
if cmd.isdigit(): cmd = int(cmd) if cmd == 0: os.system(pull_photo_2_local) elif cmd == 1: os.system(del_local_photos) elif cmd == 2: os.system(del_remote_photos) elif cmd == 3: os.system(del_local_photos) os.system(del_remote_photos) else: print(CommonTips.tip_arg_error) else: if 'h' == cmd.lower(): print(op_info) elif 'q' == cmd.lower(): print(CommonTips.tip_quit) break else: print(CommonTips.tip_arg_error)
if __name__ == '__main__': main()
|
离线跑 so 库:OfflineSoRunner.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
| import os import CommonTips
sub_model_name = '跑离线算法库'
model_path_local = 'E:\\ProjectA\\latestModel' model_path_remote = '/sdcard/apache/model/'
exe_path_local = 'E:\\projects\\Android\\projectA_app\\app\\build\\intermediates\\cmake\\debug\\obj\\arm64-v8a\\executor' exe_path_remote = '/system/app/'
so_path_local = 'E:\\ProjectA\\latestLib' so_path_remote = '/system/lib64/'
cmd_push_model = 'adb push ' + model_path_local + ' ' + model_path_remote cmd_push_exe = 'adb push ' + exe_path_local + ' ' + exe_path_remote cmd_grant_permission = 'adb shell chmod 777 /system/app/executor' cmd_push_so = 'adb push ' + so_path_local + ' ' + so_path_remote
op_info = '''--------------跑离线算法库-------------- | 【0】:推送算法库所需要的数据文件 | 【1】:推送算法库so文件 | 【2】:推送可执行文件并授予运行权限 | 【3】:执行可执行文件 | 1 for testing checkFall | 2 for testing detectFaceNumber | 3 for testing extractFaceFeature | others for quit.''' + CommonTips.tip_ops + '----------------------------------------'
def main(): print(op_info) while True: cmd = input('('+sub_model_name+')'+CommonTips.tip_input_cmd) if cmd.isdigit(): cmd = int(cmd) if cmd == 0: os.system(cmd_push_model) elif cmd == 1: os.system(cmd_push_so) elif cmd == 2: os.system(cmd_push_exe) os.system(cmd_grant_permission) elif cmd == 3: while True: choice = input("测试选项:") if choice == '1' or choice == '2' or choice == '3': command = 'adb shell "/system/app/executor ' + choice + ' `ls /sdcard/TestData' + choice + '`"' print(command) os.system(command) else: break else: print(CommonTips.tip_arg_error) else: if 'h' == cmd.lower(): print(op_info) elif 'q' == cmd.lower(): print(CommonTips.tip_quit) break else: print(CommonTips.tip_arg_error)
if __name__ == '__main__': main()
|
带 mask 分析:FallWithMask.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
| import os
import CommonTips
sub_model_name = '带mask跌倒检测'
exe_path = 'E:\\ProjectA\\TestVideoWithMask\\TestVideoWithMask.exe' video_path = 'E:\\ProjectA\\videos' remote_path = '/sdcard/LuPingDaShi/Rec/'
op_info = '--------------带mask跌倒检测--------------'
def main(): pull = input("是否要从Android拉取视频(y/N):") if pull == 'y': os.system('adb pull '+remote_path+' '+video_path+' ') os.system('adb shell rm '+remote_path+'* ')
files = os.listdir(video_path) print("可用来测试的视频 :") for f in range(0, len(files)): print('【'+str(f)+'】 : '+files[f])
while True: idx = input('(' + sub_model_name + ')' + "请选择序号:")
if idx.isdigit(): idx = int(idx) if idx >= len(files) or idx < 0: print(CommonTips.tip_arg_error+"---数组越界") else: os.system(exe_path + ' ' + video_path + '\\' + files[idx]+' >> log.txt') else: if 'h' == idx.lower(): print(op_info) elif 'q' == idx.lower(): print(CommonTips.tip_quit) break elif 'r' == idx.lower(): files = os.listdir(video_path) print("可用来测试的视频 :") for f in range(0, len(files)): print('【' + str(f) + '】 : ' + files[f]) else: print(CommonTips.tip_arg_error)
if __name__ == '__main__': main()
|