1#!/usr/bin/env python3 2# Copyright (c) 2021 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import cv2 16import sys 17import os 18from datetime import datetime 19 20DEBUG = False 21DEBUG = True 22TARGET_WIDTH = 128 23TARGET_HEIGHT = 64 24PIXEL_PER_BYTE = 8 25WIDTH_BYTES = int(TARGET_WIDTH/PIXEL_PER_BYTE) 26PIXEL_THRESHOLD = 128.0 27 28# 将多个灰度像素打包到一个整数中 29def pack_pixels(pixels, threshold): 30 value = 0 31 for gray in pixels: 32 bit = 1 if gray >= threshold else 0 # 二值化 33 value = (value << 1) + bit # 多个二值化像素值拼接为一个字节值 34 return value 35 36frameCount = 0 37def resize_and_binarize_image(frame, width, height, threshold): 38 data = [] 39 # count = 0 # for debug 40 start = datetime.now() 41 frame = cv2.resize(frame, (width, height)) # 缩放 42 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) # 转为灰度图 43 _, binary = cv2.threshold(frame, threshold, 255, cv2.THRESH_BINARY) # 二值化 44 45 for r in range(height): 46 for b in range(int(width / PIXEL_PER_BYTE)): 47 colStart = b * PIXEL_PER_BYTE 48 pixels = frame[r, colStart: colStart + PIXEL_PER_BYTE] 49 byte = pack_pixels(pixels, threshold) 50 data.append(byte) 51 if DEBUG: 52 global frameCount 53 cv2.imwrite(os.path.join('debug', str(frameCount) + '.png'), frame) 54 cv2.imwrite(os.path.join('debug', str(frameCount) + '-bin.png'), binary) 55 frameCount += 1 56 end = datetime.now() 57 print('time cost:', end - start) 58 return bytes(data) 59 60def convert_frame_to_bytes(frame): 61 return resize_and_binarize_image(frame, TARGET_WIDTH, TARGET_HEIGHT, PIXEL_THRESHOLD) 62 63def main(): 64 if len(sys.argv) < 3: 65 print("Usage: {} input outdir [width] [height] [threshod]".format(sys.argv[0])) 66 exit(-1) 67 68 imgPath = sys.argv[1] 69 outdir = sys.argv[2] 70 width = len(sys.argv) > 3 and int(sys.argv[3]) or TARGET_WIDTH 71 height = len(sys.argv) > 4 and int(sys.argv[4]) or TARGET_HEIGHT 72 threshold = len(sys.argv) > 5 and int(sys.argv[5]) or PIXEL_THRESHOLD 73 imgFile = os.path.split(imgPath)[-1] 74 imgBase = imgFile.split('.')[-2] 75 codeFile = os.path.join(outdir, imgBase + '.c') 76 if not os.path.exists(outdir): 77 os.mkdir(outdir) 78 79 frame = cv2.imread(imgPath) # 加载图片 80 bitmap = resize_and_binarize_image(frame, width, height, threshold) # 转为目标格式的数组 81 82 with open(codeFile, 'w+') as f: # 输出到.c文件 83 f.write('const unsigned char {}_size[] = {{ {}, {} }};\n'.format(imgBase, width, height)) 84 f.write('const unsigned char {}[] = {{\n'.format(imgBase)) 85 for i in range(len(bitmap)): 86 v = bitmap[i] 87 sep = '\n' if (i+1) % (TARGET_WIDTH/PIXEL_PER_BYTE) == 0 else ' ' 88 f.write('0x%02X,%s' % (v, sep)) 89 f.write('};\n') 90 print(imgFile, '=>', codeFile, 'done!') 91 92if __name__ == "__main__": 93 main() 94