File size: 1,251 Bytes
7de9b44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4b1f21
7de9b44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2860169
 
7de9b44
2860169
7de9b44
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()