update py and md

This commit is contained in:
chen
2020-06-28 12:18:05 +08:00
parent 76da83d752
commit 299a761988
2 changed files with 63 additions and 95 deletions
+7 -40
View File
@@ -3,43 +3,11 @@
img2bin能够生成模型推理所需的输入数据,以.bin格式保存。 img2bin能够生成模型推理所需的输入数据,以.bin格式保存。
有两类数据,一类是图片数据,另一类是模型需要的第二个输入数据,如fasterrcnn的第二个输入是图片的shape信息。 有两类数据,一类是图片数据,另一类是模型需要的第二个输入数据,如fasterrcnn的第二个输入是图片的shape信息。
## 环境准备 ## 前提条件
执行此脚本需要安装python3的opencv,**如已安装可跳过环境准备**。 脚本可在Centos和Ubuntu环境下使用,需提前安装python3和pip3。
1. 在root用户下更换源 如未安装opencv-python,脚本会自动安装
```
vim /etc/apt/sources.list
```
把原有的源更换为国内可用的源,arm源可参考 https://bbs.huaweicloud.com/forum/thread-61366-1-1.html 。
源更新后,执行以下命令更新软件列表。
```
apt-get update
```
2. 安装python3的依赖。
```
apt-get install python3-setuptools python3-dev build-essential python3-pip
```
```
pip3 install enum34==1.1.6 future==0.17.1 funcsigs==1.0.2 unique protobuf numpy
```
>**说明:**
>
> pip3 install安装有报错“SSLError”时,请使用:pip3 install --trusted-host pypi.org --trusted-host files.pythonhosted.org numpy==1.11.2 enum34==1.1.6 future==0.17.1 funcsigs==1.0.2 unique protobuf 安装依赖,表示可信赖的主机解决问题。
3. 安装opencv。
```
pip3 install opencv_python
```
## 获取脚本 ## 获取脚本
@@ -58,11 +26,10 @@ img2bin能够生成模型推理所需的输入数据,以.bin格式保存。
## 使用方法 ## 使用方法
进入脚本所在目录。 进入脚本所在目录。
``` ```
cd $HOME/AscendProjects/tools/img2bin cd $HOME/AscendProjects/img2bin
``` ```
### 第一类图片: ### 第一类图片:
- -i后跟**目录**表示转换图片。
- 脚本会将 -i 后指定的图片目录下的所有图片按参数设置做相应的预处理,并以"文件名.bin"命名保存在-o指定的输出目录下。 - 脚本会将 -i 后指定的图片目录下的所有图片按参数设置做相应的预处理,并以"文件名.bin"命名保存在-o指定的输出目录下。
``` ```
@@ -70,10 +37,10 @@ python3 img2bin.py -i ./images -w 416 -h 416 -f BGR -a NHWC -t uint8 -m [104,117
``` ```
### 第二类: ### 第二类:
- -i后跟**文件路径**表示转换第二类数据。
- 第二类数据,需要新建一个文件,文件模板为test.txt"input_node"为数据,"shape"为数据的shape信息。 - 第二类数据,需要新建一个文件,文件模板为test.txt"input_node"为数据,"shape"为数据的shape信息。
- 文件模板的后缀必须是“.txt”。
- 第二类数据只需 -i、-t、-o三个参数。 - 第二类数据只需 -i、-t、-o三个参数。
- 参数 -i 需要指定文件的路径,-t 需要指定数据类型,-o指定输出目录。 - 参数 -i 需要指定文件的目录或路径,-t 需要指定数据类型,-o指定输出目录。
``` ```
python3 img2bin.py -i ./test.txt -t uint8 -o ./out python3 img2bin.py -i ./test.txt -t uint8 -o ./out
@@ -83,7 +50,7 @@ python3 img2bin.py -i ./test.txt -t uint8 -o ./out
| 参数名 | 说明 | | 参数名 | 说明 |
| - | - | | - | - |
| -i | 图片的输入目录或第二个输入的文件路径 | | -i | 输入目录或路径 <br>**在目录下,不能同时有图片和txt,一次只能转一种数据** |
| -w | 输出图片宽 | | -w | 输出图片宽 |
| -h | 输出图片高 | | -h | 输出图片高 |
| -f | 输出图片色彩格式,支持(BGR/RGB/YUV/GRAY | | -f | 输出图片色彩格式,支持(BGR/RGB/YUV/GRAY |
+54 -53
View File
@@ -31,23 +31,28 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
# ======================================================================= # =======================================================================
#
import argparse import argparse
import configparser import configparser
import cv2 as cv
import numpy as np
import json import json
import os import os
import re
import sys import sys
try:
import cv2 as cv
except:
os.system('pip3 install --trusted-host pypi.org --trusted-host files.pythonhosted.org opencv-python')
import cv2 as cv
try:
import numpy as np
except:
os.system('pip3 install --trusted-host pypi.org --trusted-host files.pythonhosted.org numpy')
import numpy as np
def get_args(): def get_args():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
conflict_handler='resolve', conflict_handler='resolve',
description='''eg1: python3 imgtobin.py description='''eg1: python3 imgtobin.py
-i ./images -w 416 -h 416 -f BGR -a NCHW -m [104,117,123] -o ./out -i ./images -w 416 -h 416 -f BGR -a NHWC -t uint8 -m [104,117,123] -c [1,1,1] -o ./out
eg2: python3 imgtobin.py -i ./test.txt -t uint8''') eg2: python3 imgtobin.py -i ./test.txt -t uint8 -o ./out''')
parser.add_argument('-i', '--input', required=True, type=str, \ parser.add_argument('-i', '--input', required=True, type=str, \
help='folder of input image or file of other input.') help='folder of input image or file of other input.')
parser.add_argument('-w', '--width', type=int, \ parser.add_argument('-w', '--width', type=int, \
@@ -82,32 +87,21 @@ def check_args(args):
check_flag = True check_flag = True
is_dir = True is_dir = True
if os.path.isdir(args.input): if os.path.isdir(args.input):
#print(args)
if not os.listdir(args.input): if not os.listdir(args.input):
eprint('[ERROR] input image path=%r is empty.' % path) eprint('[ERROR] input image path=%r is empty.' % args.input)
check_flag = False check_flag = False
elif os.path.isfile(args.input): elif os.path.isfile(args.input):
is_dir = False is_dir = False
else: else:
eprint('[ERROR] input path=%r does not exist.' % path) eprint('[ERROR] input path=%r does not exist.' % args.input)
check_flag = False check_flag = False
if args.output_image_format not in ('BGR','RGB', 'YUV', 'GRAY'): if args.output_image_format not in ('BGR','RGB', 'YUV', 'GRAY'):
eprint("ERROR:Convert to %d is not support"%(args.output_image_format)) eprint("ERROR:Convert to %d is not support"%(args.output_image_format))
check_flag = False check_flag = False
# if os.path.isfile(args.output_path):
# eprint('[ERROR] argument output_path should be a folder.')
# elif not os.path.exists(args.output_path):
# os.makedirs(args.output_path)
# if not 16 <= args.model_width <= 4096:
# eprint('[ERROR] resized image width should between 16 and 4096.')
# check_flag = False
# if not 16 <= args.model_height <= 4096:
# eprint('[ERROR] resized image height should between 16 andd 4096.')
# check_flag = False
return check_flag, is_dir return check_flag, is_dir
def convert_img(args, input_img): def convert_img(args, input_img):
if args.output_image_format == 'BGR': if args.output_image_format == 'BGR':
converted_input_img = input_img converted_input_img = input_img
@@ -116,11 +110,11 @@ def convert_img(args, input_img):
elif args.output_image_format == 'YUV': elif args.output_image_format == 'YUV':
if input_img.shape[0] % 2 == 1: if input_img.shape[0] % 2 == 1:
if input_img.shape[1] % 2 == 1: if input_img.shape[1] % 2 == 1:
input_img = cv.resize(input_img, ((input_img.shape[0] + 1), (input_img.shape[1] + 1))) input_img = cv.resize(input_img, ((input_img.shape[0] + 1), (input_img.shape[1] + 1)))
else: else:
input_img = cv.resize(input_img, ((input_img.shape[0] + 1), input_img.shape[1])) input_img = cv.resize(input_img, ((input_img.shape[0] + 1), input_img.shape[1]))
elif input_img.shape[1] % 2 == 1: elif input_img.shape[1] % 2 == 1:
input_img = cv.resize(input_img, (input_img.shape[0], input_img.shape[1] + 1)) input_img = cv.resize(input_img, (input_img.shape[0], input_img.shape[1] + 1))
converted_input_img = cv.cvtColor(input_img, cv.COLOR_BGR2YUV_I420) converted_input_img = cv.cvtColor(input_img, cv.COLOR_BGR2YUV_I420)
elif args.output_image_format == 'GRAY': elif args.output_image_format == 'GRAY':
converted_input_img = cv.cvtColor(input_img, cv.COLOR_BGR2GRAY) converted_input_img = cv.cvtColor(input_img, cv.COLOR_BGR2GRAY)
@@ -192,42 +186,49 @@ def mkdir_output(args):
return return
def process(args, file_path):
if file_path.endswith(".txt"):
config = configparser.ConfigParser()
config.read(file_path)
input_node = json.loads(config['baseconf']['input_node'])
shape = json.loads(config['baseconf']['shape'])
input_node_np = np.array(input_node)
change_type_img_info = change_type(args, input_node_np)
img_info = np.reshape(change_type_img_info, shape)
out_path = os.path.join(args.output, os.path.splitext(os.path.split(file_path)[1])[0] + ".bin")
mkdir_output(args)
img_info.tofile(out_path)
else:
input_img = cv.imread(file_path)
if args.output_image_format == 'YUV':
resized_img1 = resize_img(args, input_img)
converted_img = convert_img(args, resized_img1)
mean_img = mean(args, converted_img)
else:
converted_img = convert_img(args, input_img)
resized_img = resize_img(args, converted_img)
mean_img = mean(args, resized_img)
coefficient_img = coefficient(args, mean_img)
change_type_img = change_type(args, coefficient_img)
change_format_img = change_format(args, change_type_img)
out_path = os.path.join(args.output, os.path.splitext(os.path.split(file_path)[1])[0] + ".bin")
mkdir_output(args)
change_format_img.tofile(out_path)
def main(): def main():
"""main function to receive params them change data to bin. """main function to receive params them change data to bin.
""" """
args = get_args() args = get_args()
ret,is_dir = check_args(args) ret, is_dir = check_args(args)
if ret: if ret:
if is_dir: if is_dir:
img_names = os.listdir(args.input) files_name = os.listdir(args.input)
for img_name in img_names: for file_name in files_name:
img_path = os.path.join(args.input, img_name) file_path = os.path.join(args.input, file_name)
input_img = cv.imread(img_path) process(args, file_path)
if args.output_image_format == 'YUV':
resized_img1 = resize_img(args, input_img)
converted_img = convert_img(args, resized_img1)
mean_img = mean(args, converted_img)
else:
converted_img = convert_img(args, input_img)
resized_img = resize_img(args, converted_img)
mean_img = mean(args, resized_img)
coefficient_img = coefficient(args, mean_img)
change_type_img = change_type(args, coefficient_img)
change_format_img = change_format(args, change_type_img)
out_path = os.path.join(args.output, os.path.splitext(img_name)[0] + ".bin")
mkdir_output(args)
change_format_img.tofile(out_path)
else: else:
config = configparser.ConfigParser() process(args, args.input)
config.read(args.input)
input_node = json.loads(config['baseconf']['input_node'])
shape = json.loads(config['baseconf']['shape'])
input_node_np = np.array(input_node)
change_type_img_info = change_type(args, input_node_np)
img_info = np.reshape(change_type_img_info, shape)
out_path = os.path.join(args.output, os.path.splitext(args.input)[0] + ".bin")
mkdir_output(args)
img_info.tofile(out_path)
if __name__ == '__main__': if __name__ == '__main__':