-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfeatureextractor.py
59 lines (45 loc) · 1.5 KB
/
testfeatureextractor.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import tflite_runtime.interpreter as tflite
import numpy as np
import pandas as pd
import os
l = []
CSV = 'newface4.csv'
folder = os.listdir('testdata/')
def get_mobilenet_input(f, out_size=(92, 112), is_quant=True):
img = np.array(Image.open("testdata/" + f).resize(out_size))
if not (is_quant):
img = img.astype(np.float32) / 128 - 1
return np.array([img])
model_file = 'mobnetfacefeatureExtractor.tflite'
interpreter = tflite.Interpreter(model_path=model_file)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
##Converting image into tensor
import cv2
from PIL import Image
import numpy as np
#import tensorflow as tf
for f in folder:
print(f)
image = cv2.resize(cv2.imread('testdata/' + f).astype(np.float32), (224, 224))
img = get_mobilenet_input(f)
##Test model
interpreter.set_tensor(input_details[0]['index'], img.astype(np.float32))
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
features = np.array(output_data)
features = np.ravel(features)
l.append(features)
print(features.shape)
import csv
l = zip(*l)
with open(CSV, 'w') as f:
# create the csv writer
writer = csv.writer(f)
# write a row to the csv file
writer.writerow(folder)
for row in l:
writer.writerow(row)