|
import base64 |
|
import numpy as np |
|
import cv2 |
|
import tensorflow as tf |
|
from tensorflow.keras.utils import CustomObjectScope |
|
|
|
def iou(y_true, y_pred): |
|
def f(y_true, y_pred): |
|
intersection = (y_true * y_pred).sum() |
|
union = y_true.sum() + y_pred.sum() - intersection |
|
x = (intersection + 1e-15) / (union + 1e-15) |
|
x = x.astype(np.float32) |
|
return x |
|
return tf.numpy_function(f, [y_true, y_pred], tf.float32) |
|
|
|
smooth = 1e-15 |
|
def dice_coef(y_true, y_pred): |
|
y_true = tf.keras.layers.Flatten()(y_true) |
|
y_pred = tf.keras.layers.Flatten()(y_pred) |
|
intersection = tf.reduce_sum(y_true * y_pred) |
|
return (2. * intersection + smooth) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth) |
|
|
|
def dice_loss(y_true, y_pred): |
|
return 1.0 - dice_coef(y_true, y_pred) |
|
|
|
def predict(): |
|
|
|
interpreter = tf.lite.Interpreter(model_path='model/segmentation_model.tflite') |
|
interpreter.allocate_tensors() |
|
|
|
|
|
input_details = interpreter.get_input_details() |
|
output_details = interpreter.get_output_details() |
|
|
|
|
|
H = 512 |
|
W = 384 |
|
|
|
|
|
image = cv2.imread('model/test.jpg', cv2.IMREAD_COLOR) |
|
|
|
resized_image = cv2.resize(image, (W, H)) |
|
x = resized_image/255.0 |
|
x = x.astype(np.float32) |
|
x = np.expand_dims(x,0) |
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], x) |
|
interpreter.invoke() |
|
|
|
|
|
prediction = interpreter.get_tensor(output_details[0]['index'])[0] |
|
|
|
prediction = cv2.resize(prediction, (W, H)) |
|
prediction = np.expand_dims(prediction, axis=-1) |
|
prediction = prediction > 0.5 |
|
|
|
mask = np.uint8(prediction) * 255 |
|
prediction = cv2.merge([mask, mask, mask]) |
|
|
|
mask_inv = cv2.cvtColor(prediction, cv2.COLOR_BGR2GRAY) |
|
|
|
output_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2BGRA) |
|
output_image[..., 3] = mask_inv |
|
|
|
|
|
|
|
cv2.imwrite("model/output.png", output_image) |
|
|
|
return "done" |
|
|
|
def predict_h5(file): |
|
|
|
with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}): |
|
model = tf.keras.models.load_model("model/model.h5") |
|
|
|
|
|
H = 512 |
|
W = 384 |
|
|
|
|
|
|
|
file_bytes = np.fromstring(file, np.uint8) |
|
|
|
image = cv2.imdecode(file_bytes, cv2.IMREAD_UNCHANGED) |
|
|
|
resized_image = cv2.resize(image, (W, H)) |
|
x = resized_image/255.0 |
|
x = x.astype(np.float32) |
|
x = np.expand_dims(x,0) |
|
|
|
|
|
|
|
|
|
prediction = model.predict(x)[0] |
|
|
|
prediction = cv2.resize(prediction, (W, H)) |
|
prediction = np.expand_dims(prediction, axis=-1) |
|
prediction = prediction > 0.5 |
|
|
|
mask = np.uint8(prediction) * 255 |
|
prediction = cv2.merge([mask, mask, mask]) |
|
|
|
mask_inv = cv2.cvtColor(prediction, cv2.COLOR_BGR2GRAY) |
|
|
|
output_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2BGRA) |
|
output_image[..., 3] = mask_inv |
|
|
|
retval, buffer = cv2.imencode('.png', output_image) |
|
encoded_image = base64.b64encode(buffer).decode('utf-8') |
|
|
|
return encoded_image |