Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

inference.py 1.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. import tensorflow as tf
  2. import numpy as np
  3. from PIL import Image
  4. class FEPredictor:
  5. def __init__(self, model_path):
  6. self.model_path = model_path
  7. self.model = tf.keras.models.load_model(self.model_path)
  8. self.image_size = self.model.input_shape[1]
  9. # def preprocess(self, image):
  10. # image = tf.image.resize(image, (self.image_size, self.image_size))
  11. # image = np.expand_dims(np.expand_dims(image, -1), 0)
  12. # return tf.cast(image, tf.float32) / 255.0
  13. def infer(self, image):
  14. tensor_image = tf.convert_to_tensor(np.array(Image.open(image)), dtype=tf.float32)[np.newaxis, :]
  15. # #tensor_image = self.preprocess(tensor_image)
  16. # shape= tensor_image.shape
  17. # tensor_image = tf.reshape(tensor_image,[1, shape[0],shape[1], shape[2]])
  18. # print(tensor_image.shape)
  19. pred = self.model.predict(tensor_image)
  20. pred = int(np.argmax(pred))
  21. return {'output':pred}
  22. if __name__ == "__main__":
  23. image = "./data/test/angry/PrivateTest_88305.jpg"
  24. predictor = FEPredictor("./models/1/emotionModel.hdf5")
  25. print(predictor.infer(image))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...