-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
36 lines (27 loc) · 1019 Bytes
/
utils.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
import os, sys, glob
from PIL import Image
def img2gray(img_path):
img = Image.open(img_path)
img = img.convert('L')
img.save(img_path)
# images to one header file
def img2array(img_paths, h_path):
for img_path in img_paths:
os.system('xxd -i %s >> %s' % (img_path, h_path))
def tflite2array(tflite_path, h_path):
result = os.popen('cat %s | xxd -i' % tflite_path).read()
with open(h_path, 'w+', encoding='utf-8') as f:
f.write("""#ifndef _TFLITE_MODEL_H_\n""")
f.write("""#define _TFLITE_MODEL_H_\n""")
f.write("""alignas(8) const unsigned char tflite_model[] = {\n""")
f.write(result)
f.write("""};\n""")
f.write("""#endif""")
if __name__ == '__main__':
img_dir = 'lpr/data'
h_path = 'lpr/images.h'
img_paths = glob.glob(os.path.join(img_dir, '*.jpg'))
# img2array(img_paths, h_path)
tflite_path = 'lpr/data/model.tflite'
save_path = 'lpr/src/tflite_model.h'
tflite2array(tflite_path, save_path)