Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """app.ipynb | |
| Automatically generated by Colaboratory. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1Mv-09djxvNrRlLGT7tkVUiy9YGyOLW5G | |
| """ | |
| import gradio as gr | |
| import cv2 | |
| import requests | |
| import gdown | |
| import tensorflow as tf | |
| from tensorflow import keras | |
| #from custom_model import ImageClassifier | |
| import numpy as np | |
| #from tensorflow.keras import models | |
| #from keras.layers import Dense, Activation, Flatten,Dropout, Conv2D, BatchNormalization, MaxPooling2D | |
| from keras.models import load_model | |
| # Add any necessary imports | |
| import tensorflow as tf | |
| import gradio as gr | |
| from tensorflow.keras.models import load_model | |
| import numpy as np | |
| import tensorflow_hub as hub | |
| #path = [['car_bike.jpg'], ['human.jpg'], ['chair.jpg']] | |
| # Define your class labels or categories for predictions | |
| train_info = [] # Replace with your actual class labels | |
| # Read image names from the text file | |
| with open('label.txt', 'r') as file: | |
| train_info = [line.strip() for line in file.read().splitlines()] | |
| output_path = 'label.txt' | |
| with open(output_path, 'r') as file: | |
| LABELS = [x.strip() for x in file.readlines()] | |
| num_classes = 12 | |
| IMG_SIZE = 224 | |
| def _normalize_img(img): | |
| img = tf.cast(img, tf.float32) / 255. # All images will be rescaled by 1./255 | |
| img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE), method='bilinear') | |
| return img | |
| # If your model requires custom objects, provide them here | |
| model = load_model('model.h5', custom_objects={'KerasLayer': hub.KerasLayer}) | |
| def predict_fn(img): | |
| img = img.convert('RGB') | |
| img_data = _normalize_img(img) | |
| x = np.array(img_data) | |
| x = np.expand_dims(x, axis=0) | |
| temp = model.predict(x) | |
| idx = np.argsort(np.squeeze(temp))[::-1] | |
| top3_value = np.asarray([temp[0][i] for i in idx[0:3]]) | |
| top3_idx = idx[0:3] | |
| return {LABELS[i]: str(v) for i, v in zip(top3_idx, top3_value)} | |
| # Launch the Gradio interface | |
| gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label').launch() |