| | import gradio as gr |
| | from tensorflow.keras.models import load_model |
| | import numpy as np |
| | from PIL import Image |
| |
|
| | |
| | autoencoder = load_model('autoencoder_denoise.h5') |
| | classifier = load_model('tomato_cnn_model.h5') |
| |
|
| | |
| | label_mapping = {0: 'Reject', 1: 'Ripe', 2: 'Unripe'} |
| |
|
| | def process_image(image): |
| | |
| | image = image.convert("RGB").resize((128, 128)) |
| | img_array = np.array(image) / 255.0 |
| | img_array = np.expand_dims(img_array, axis=0) |
| |
|
| | |
| | denoised_img = autoencoder.predict(img_array) |
| |
|
| | |
| | prediction = classifier.predict(denoised_img)[0] |
| |
|
| | |
| | denoised_img_disp = np.squeeze(denoised_img) * 255 |
| | denoised_img_disp = Image.fromarray(denoised_img_disp.astype('uint8')) |
| |
|
| | |
| | confidences = {label_mapping[i]: float(pred) for i, pred in enumerate(prediction)} |
| |
|
| | return denoised_img_disp, confidences |
| |
|
| | |
| | interface = gr.Interface( |
| | fn=process_image, |
| | inputs=gr.Image(type="pil"), |
| | outputs=[ |
| | gr.Image(type="pil", label="Denoised Image"), |
| | gr.Label(label="Predicted Class & Confidence") |
| | ], |
| | title="Denoising Autoencoder + Image Classifier", |
| | description="Upload gambar noise, hasilnya gambar bersih & hasil klasifikasinya." |
| | ) |
| |
|
| | |
| | if __name__ == "__main__": |
| | interface.launch() |
| |
|