forked from afif2100/image-clasifier-tf-keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
49 lines (38 loc) · 1.18 KB
/
predict.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Defiine params and lib
import requests
from io import BytesIO
from PIL import Image
import numpy as np
# Parameters
input_size = (150,150)
#define input shape
channel = (3,)
input_shape = input_size + channel
#define labels
labels = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']
# # Define preprocess function
def preprocess(img,input_size):
nimg = img.convert('RGB').resize(input_size, resample= 0)
img_arr = (np.array(nimg))/255
return img_arr
def reshape(imgs_arr):
return np.stack(imgs_arr, axis=0)
# # Load models
from tensorflow.keras.models import load_model
# ada 2 cara load model, jika cara pertama berhasil maka bisa lasngusng di lanjutkan ke fungsi prediksi
MODEL_PATH = 'model/medium_project/model.h5'
model = load_model(MODEL_PATH,compile=False)
# # Predict the image
# read image
im = Image.open('contoh_prediksi.jpg')
X = preprocess(im,input_size)
X = reshape([X])
y = model.predict(X)
print( labels[np.argmax(y)], np.max(y) )
print( labels[np.argmax(y)], np.max(y) )
# read image
im = Image.open('dataset/train/dandelion/2522454811_f87af57d8b.jpg')
X = preprocess(im,input_size)
X = reshape([X])
y = model.predict(X)
print( labels[np.argmax(y)], np.max(y) )