HiSilicon development: python down-conversion image to bgr_planner format

foreword

After the model conversion, the performance will be more or less lost. In order to evaluate, it is necessary to run a dataset on HiSilicon, but the image input format of HiSilicon is bgr_planner format, so a format conversion is required for the image. For the master, it is directly done in c/c++, but this recipe can only choose this two-stage method.

the code

Not much nonsense, the code has been tested and is sure to work. In order to rest assured, I also did a special experiment. The same picture was converted to bgr_planner format with python, and converted to bgr_planner format with c++, and then fed to the face recognition model successively. The difference in the final output feature value was about 0.05.

import cv2
import os
import numpy as np
import struct

# 图片转为bgr_planner文件并保存
def mat_to_bgr_planner(img_path, bgr_path, new_size, bgr_planner=1):

    img = cv2.imread(img_path)

    img = cv2.resize(img, new_size)

    height = img.shape[0]
    width = img.shape[1]
    channel = img.shape[2]

    with open(bgr_path, "wb") as bgr:
        for chn in range(channel):
            if bgr_planner: # 输出 bgr_planner
                img_split = img[:, :, chn]
            else:           # 输出 rgb_planner
                img_split = img[:, :, 2 - chn]

            for row in range(height):
                for col in range(width):

                    element = int(img_split[row, col])
                    element = struct.pack('B', element)
                    bgr.write(element)

    print("mat_to_bgr_planner done !")
img_path = r"F:\demo\lfw_nnie_test\bgr_test_id10.jpg"
#img_path = r"F:\HiSVP_PC_V1.2.2.2\tools\nnie\windows\RuyiStudio-2.0.41_\workspace\nniefacelib\data\nnie_image\rgb_planar\id10.jpg"
bgr_path = r"F:\demo\lfw_nnie_test\bgr_test_id10.bgr"
new_size = (112, 112)
#mat_to_bgr_planner(img_path, bgr_path, new_size)

# bgr_planner文件转为图片并保存
def bgr_planner_to_mat(bgr_path, img_path, shape, bgr_planner=1):

    height, width, channel = shape
    img = np.zeros(shape)

    with open(bgr_path, "rb") as bgr:
        for chn in range(channel):
            if bgr_planner:  # 输入 bgr_planner
                img_split = img[:, :, chn]
            else:            # 输入 rgb_planner
                img_split = img[:, :, 2 - chn]

            for row in range(height):
                for col in range(width):
                    element = bgr.read(1)
                    element = struct.unpack('B', element)
                    img_split[row, col] = element[0]

    cv2.imwrite(img_path, img)
    print("bgr_planner_to_mat done !")

bgr_path = r"F:\demo\lfw_nnie_test\bgr_image\Aaron_Eckhart_0001.bgr"
img_path = r"F:\demo\lfw_nnie_test\Aaron_Eckhart_0001.jpg"
shape = (112, 112, 3)
#bgr_planner_to_mat(bgr_path, img_path, shape)

Guess you like

Origin blog.csdn.net/tangshopping/article/details/109117448