forked from SystemErrorWang/CartoonGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartoonize.py
32 lines (23 loc) · 915 Bytes
/
cartoonize.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
import cv2
import numpy as np
import tensorflow as tf
def cartoonize(image_path):
interpreter = tf.lite.Interpreter(model_path="cartoon_gan.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
image = cv2.imread(image_path)
image = cv2.resize(image, (256, 256),
interpolation=cv2.INTER_CUBIC)
image = image.astype(np.float32)/127.5 - 1
image = image.reshape(input_shape)
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])
output = (np.squeeze(output)+1)*127.5
output = np.clip(output, 0, 255)
cv2.imwrite('cartoon_image.jpg', output)
if __name__ == '__main__':
image_path = 'test_image.jpg'
cartoonize(image_path)