Spaces:
Runtime error
Runtime error
ashishabraham22
commited on
Commit
·
3d803a7
1
Parent(s):
7cdcdb6
Upload ml_predict.py
Browse files- ml_predict.py +26 -0
ml_predict.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from tensorflow import cast
|
3 |
+
import tensorflow
|
4 |
+
from tensorflow.image import resize
|
5 |
+
import numpy as np
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
def preprocess_img(image):
|
11 |
+
image = cast(image, tensorflow.float32)
|
12 |
+
image = image / 255.
|
13 |
+
image = resize(image, [200, 200])
|
14 |
+
return image
|
15 |
+
|
16 |
+
|
17 |
+
def predict_defect(image):
|
18 |
+
reference = {0:"Crazing", 1:"Inclusion", 2:"Patches", 3:"Pitted", 4:"Rolled", 5:"Scratches"}
|
19 |
+
image = preprocess_img(image)
|
20 |
+
subdir='model_files'
|
21 |
+
path = os.path.join(subdir,"Metal_surface_defect_detector.h5")
|
22 |
+
model=load_model(path)
|
23 |
+
result = np.argmax(model.predict(np.array([image])))
|
24 |
+
# return {reference[i]: float(result[i]) for i in range(6)}
|
25 |
+
return reference[result]
|
26 |
+
|