-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
396529c
commit ce3a000
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "ZELjx8qt4t3u" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#PALING BENER\n", | ||
"\n", | ||
"import os\n", | ||
"import numpy as np\n", | ||
"import tensorflow as tf\n", | ||
"from tensorflow.keras.models import load_model\n", | ||
"from tensorflow.keras.utils import load_img, img_to_array\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from google.colab import files\n", | ||
"\n", | ||
"# Load model gabungan\n", | ||
"combined_model = load_model('/content/combined_model.h5')\n", | ||
"\n", | ||
"# Daftar class labels untuk model pertama\n", | ||
"class_labels = ['Apel', 'Pisang', 'Paprika', 'Jeruk', 'Wortel', 'Timun']\n", | ||
"\n", | ||
"# Fungsi untuk prediksi jenis buah\n", | ||
"def predict_jenis_buah(model, class_labels, file_name, target_size=(224, 224), confidence_threshold=0.6):\n", | ||
" img = load_img(file_name, target_size=target_size)\n", | ||
" img_array = img_to_array(img)\n", | ||
" img_array = np.expand_dims(img_array, axis=0) / 255.0\n", | ||
" prediction = model.predict([img_array, np.zeros((1, 150, 150, 3))])[0]\n", | ||
" confidence = np.max(prediction)\n", | ||
" print(f\"Predictions: {prediction}, Confidence: {confidence}\") # Debugging output\n", | ||
" predicted_class_index = np.argmax(prediction)\n", | ||
" if predicted_class_index >= len(class_labels) or confidence < confidence_threshold:\n", | ||
" return \"Cannot be predicted\", confidence\n", | ||
" predicted_label = class_labels[predicted_class_index]\n", | ||
" return predicted_label, confidence\n", | ||
"\n", | ||
"# Fungsi untuk prediksi kesegaran buah\n", | ||
"def predict_kesegaran(model, file_name, target_size=(150, 150)):\n", | ||
" img = load_img(file_name, target_size=target_size)\n", | ||
" img_array = img_to_array(img)\n", | ||
" img_array = np.expand_dims(img_array, axis=0) / 255.0\n", | ||
" prediction = model.predict([np.zeros((1, 224, 224, 3)), img_array])[1]\n", | ||
" print(f\"Kesegaran prediction: {prediction}\") # Debugging output\n", | ||
" predicted_label = 'Segar' if prediction[0] > 0.01 else 'Tidak Segar'\n", | ||
" return predicted_label\n", | ||
"\n", | ||
"# Mengunggah dan memprediksi gambar baru\n", | ||
"uploaded = files.upload()\n", | ||
"for fn in uploaded.keys():\n", | ||
" file_path = fn\n", | ||
" jenis_buah, confidence = predict_jenis_buah(combined_model, class_labels, file_path)\n", | ||
" if jenis_buah == \"Cannot be predicted\":\n", | ||
" kesegaran_buah = \"Cannot be predicted\"\n", | ||
" else:\n", | ||
" kesegaran_buah = predict_kesegaran(combined_model, file_path)\n", | ||
"\n", | ||
" # Menampilkan hasil prediksi\n", | ||
" img = load_img(file_path)\n", | ||
" plt.imshow(img)\n", | ||
" plt.title(f'Predicted: {jenis_buah} (Confidence: {confidence:.2f}), Kesegaran: {kesegaran_buah}')\n", | ||
" plt.axis('off')\n", | ||
" plt.show()\n", | ||
"\n", | ||
" print(f'Gambar {fn} adalah {jenis_buah}, dan buah tersebut {kesegaran_buah}')\n", | ||
"\n" | ||
] | ||
} | ||
] | ||
} |