使用python对文件中的内容进行修改,替换掉.cfg文件、.weight文件和生成的.rknn文件的名称。
from PIL import Image
import numpy as np
import re
import math
import random
from rknn.api import RKNN
if __name__ == '__main__':
# Create RKNN object
rknn = RKNN()
# Load tensorflow model
print('--> Loading model')
rknn.load_darknet(model='/home/toybrick/yolov3_demo/ADAS4/123.cfg', weight="/home/toybrick/yolov3_demo/ADAS4/1234.weights")`
print('done')
rknn.config(channel_mean_value='0 0 0 255', reorder_channel='0 1 2')
# Build model
print('--> Building model')
rknn.build(do_quantization=True, dataset='./dataset_416x416.txt')
#rknn.build(do_quantization=True)
print('done')
rknn.export_rknn('/home/toybrick/yolov3_demo/ADAS4/12345.rknn')
exit(0)
代码如下:
# encoding: utf-8
import sys
import os
import shutil
import re
__all__ = ['DirPath']
'''
在指定路径下递归查找包含指定字符串的文件
可以指定查找的文件类型category-默认为'.py'
可以指定查找的字符串str-默认为'python'
'''
class DirPath(object):
# 初始化参数查找路径-path
def __init__(self, path):
#self.show = self.show()
self.path = path
self.value = []
def getResult(self):
return self.value
# 开启func协程的装饰器
def on(func):
def wrapper(*args):
res = func(*args)
next(res)
return res
return wrapper
@on
# 搜索path路径下的python文件
def search(self, target, category):
while True:
path = yield
for entry in os.scandir(path):
if entry.is_file():
if entry.name.endswith(category):
target.send(entry.path)
if entry.is_dir():
self.search(target, category).send(entry.path)
@on
# 找到f文件中包含str的行信息并发送给target
def find_str(self, str):
while True:
path = yield
with open(path, "r", encoding='utf-8') as f:
for (name, value) in enumerate(f):
if str in value:
#target.send(f"[{path}][{name+1}]:{value}")
self.value.append(f"[{path}][{name+1}]:{value}")
# 默认在'.py'类型文件中查找字符串-可以指定文件类型category
# 默认查找字符串'python'-可以指定查找字符串str
def code_search(self, category=".py", str="python"):
self.search(self.find_str(str), category).send(self.path)
def rknn_transform_tiny_alter(folder_name,cfg_name,model_name,rknn_name):
path = folder_name
Dir = DirPath(path)
Dir.code_search(str=".cfg", category=".py")
Dir.code_search(str=".rknn", category=".py")
search_result=Dir.getResult()
#print(search_result)
nameRegex=re.compile(r'''/ADAS4/(.*).cfg(.*)/ADAS4/(.*).weights(.*)''')
mo = nameRegex.search(search_result[0])
search_result[0]=search_result[0].replace(mo.group(3),model_name)
search_result[0] = search_result[0].replace(mo.group(1), cfg_name)
nameRegex = re.compile(r'''/ADAS4/(.*).rknn(.*)''')
mo = nameRegex.search(search_result[1])
search_result[1] = search_result[1].replace(mo.group(1), rknn_name)
#print(search_result[1])
nameRegex2 = re.compile(r'\[(.*)\]\[(.*)\]:(.*)',re.DOTALL)
mo2=nameRegex2.search(search_result[0])
print(mo2.group(0))
nameRegex3 = re.compile(r'\[(.*)\]\[(.*)\]:(.*)', re.DOTALL)
mo3 = nameRegex2.search(search_result[1])
print(mo3.group(0))
file_selinux = mo2.group(1)
backup_file_selinux = file_selinux + '.bak'
temp_file_selinux = file_selinux + '.temp'
if not os.path.exists(backup_file_selinux):
shutil.copy2(file_selinux, backup_file_selinux)
with open(file_selinux,mode='r') as fr,open(temp_file_selinux, mode='w') as fw:
lines = fr.readlines()
#del lines[(int(mo2.group(2))-1)]#删除原来的行
lines[(int(mo2.group(2))-1)] = mo2.group(3)#添加的新的行
lines[(int(mo3.group(2))- 1)] = mo3.group(3) # 添加的新的行
for line in lines:
fw.writelines(line)
os.remove(file_selinux)
os.rename(temp_file_selinux, file_selinux)
print('OK')
if __name__ == '__main__':
rknn_transform_tiny_alter('./test','cfg','model','rknn')