Spaces:
Build error
Build error
am4nsolanki
commited on
Commit
•
6b083e3
1
Parent(s):
af4ccd5
Upload utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from progressbar import ProgressBar
|
4 |
+
import matplotlib.image as mpimg
|
5 |
+
import tensorflow as tf
|
6 |
+
from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
7 |
+
|
8 |
+
|
9 |
+
def get_image_arrays(image_column, image_path):
|
10 |
+
progressBar = ProgressBar()
|
11 |
+
X = []
|
12 |
+
|
13 |
+
for image_id in progressBar(image_column.values):
|
14 |
+
image = load_img(image_path + image_id, target_size=(224, 224))
|
15 |
+
image_array = img_to_array(image)
|
16 |
+
|
17 |
+
X.append(image_array)
|
18 |
+
|
19 |
+
X_array = np.asarray(X, dtype='float32')
|
20 |
+
X_array /= 255.
|
21 |
+
|
22 |
+
return X_array
|
23 |
+
|
24 |
+
|
25 |
+
def get_image_predictions(image_array, model_path):
|
26 |
+
# Load the TFLite model and allocate tensors.
|
27 |
+
interpreter = tf.lite.Interpreter(model_path=model_path)
|
28 |
+
interpreter.allocate_tensors()
|
29 |
+
|
30 |
+
# Get input and output tensors.
|
31 |
+
input_details = interpreter.get_input_details()
|
32 |
+
output_details = interpreter.get_output_details()
|
33 |
+
|
34 |
+
# Test the model on random input data.
|
35 |
+
input_shape = input_details[0]['shape']
|
36 |
+
input_data = image_array
|
37 |
+
interpreter.set_tensor(input_details[0]['index'], input_data)
|
38 |
+
|
39 |
+
interpreter.invoke()
|
40 |
+
|
41 |
+
# The function `get_tensor()` returns a copy of the tensor data.
|
42 |
+
# Use `tensor()` in order to get a pointer to the tensor.
|
43 |
+
output_data = interpreter.get_tensor(output_details[0]['index'])
|
44 |
+
|
45 |
+
return output_data
|
46 |
+
|
47 |
+
|
48 |
+
def show_image(image_id, image_path):
|
49 |
+
image_id_dict = dict(image_id).values()
|
50 |
+
image_id_string = list(image_id_dict)[0]
|
51 |
+
img = mpimg.imread(image_path + image_id_string)
|
52 |
+
plt.imshow(img, interpolation='nearest', aspect='auto')
|
53 |
+
plt.show()
|