File size: 789 Bytes
3d803a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
from tensorflow import cast
import tensorflow
from tensorflow.image import resize
import numpy as np
from tensorflow.keras.models import load_model
import os


def preprocess_img(image):
    image = cast(image, tensorflow.float32)
    image = image / 255.
    image = resize(image, [200, 200])
    return image


def predict_defect(image):
    reference = {0:"Crazing", 1:"Inclusion", 2:"Patches", 3:"Pitted", 4:"Rolled", 5:"Scratches"}
    image = preprocess_img(image)
    subdir='model_files'
    path = os.path.join(subdir,"Metal_surface_defect_detector.h5")
    model=load_model(path)
    result = np.argmax(model.predict(np.array([image])))
    # return {reference[i]: float(result[i]) for i in range(6)}
    return reference[result]