| import gradio as gr |
| import cv2 |
| import requests |
| import gdown |
| import tensorflow as tf |
| from tensorflow import keras |
| |
| import numpy as np |
| |
| |
|
|
| from keras.models import load_model |
|
|
|
|
|
|
| |
|
|
| path = [['car_bike.jpg'], ['human.jpg'], ['chair.jpg']] |
|
|
| |
| url = 'https://drive.google.com/file/d/1PCb6MTqelw7Tk0iCxo5Ef70zYwEpsI4Y/view?usp=sharing' |
| output_path = 'classlabel.txt' |
| gdown.download(url, output_path, quiet=False,fuzzy=True) |
|
|
| with open(output_path,'r') as file: |
| LABELS = [x.strip() for x in file.readlines()] |
|
|
|
|
| num_classes = 12 |
| IMG_SIZE = 124 |
|
|
| def _normalize_img(img): |
| img = tf.cast(img, tf.float32)/255. |
| img = tf.image.resize(img, (IMG_SIZE, IMG_SIZE), method= 'bilinear') |
| return (img) |
|
|
|
|
|
|
| model = load_model("model.h5") |
|
|
|
|
| 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)} |
| |
|
|
|
|
| gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label', examples=path,).launch() |
|
|