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 = [['0229_01.jpg'], ['0385_01.jpg'], ['0067_01.jpg']] URL = 'https://drive.google.com/file/d/1TpQmvq2R8nHy9CQaVb0jtMMuRQA0Cw0L/view?usp=sharing' output_path = 'label.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 = 4000 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) model = load_model("final_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.Image(type='pil'), outputs='label', examples=path,).launch()