MIF_E31230393/python/predict.py

42 lines
905 B
Python

import cv2
import numpy as np
import joblib
import os
import sys
image_path = sys.argv[1]
if not os.path.exists(image_path):
print("ERROR: file tidak ditemukan")
sys.exit()
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
model = joblib.load(os.path.join(BASE_DIR, 'model_svm_kopi.pkl'))
scaler = joblib.load(os.path.join(BASE_DIR, 'scaler_kopi.pkl'))
def extract_features(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224))
mean = np.mean(img, axis=(0,1))
std = np.std(img, axis=(0,1))
return list(mean) + list(std)
img = cv2.imread(image_path)
if img is None:
print("ERROR")
sys.exit()
fitur = extract_features(img)
if fitur is None:
print("ERROR")
sys.exit()
fitur_scaled = scaler.transform([fitur])
pred = model.predict(fitur_scaled)[0]
prob = model.predict_proba(fitur_scaled).max()
print(f"{pred}|{prob}")