File size: 1,854 Bytes
2dd1f74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfec278
2dd1f74
 
 
 
 
 
 
 
 
 
 
 
dfec278
 
 
 
 
2dd1f74
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1cZj5_KDg88LfgRs3U7RTXz6MiGy_485i

## FRUIT CLASSIFICATION APP
"""

import gradio as gr
from fastai.vision.all import *
import skimage
import pathlib
from PIL import Image
import albumentations
from albumentations.pytorch import ToTensorV2
import timm

class AlbumentationsTransform (RandTransform):
    split_idx,order=None,2
    def __init__(self, train_aug, valid_aug): store_attr()
    
    def before_call(self, b, split_idx):
        self.idx = split_idx
    
    def encodes(self, img: PILImage):
        if self.idx == 0:
            aug_img = self.train_aug(image=np.array(img))['image']
        else:
            aug_img = self.valid_aug(image=np.array(img))['image']
        return PILImage.create(aug_img)

def get_valid_aug(): return albumentations.Compose([
            albumentations.Resize(224, 224),
            ], p=1.0)

learn = load_learner('fruit_model_v2.pkl')

labels = learn.dls.vocab

def predict(img):

  pred,pred_idx,probs = learn.predict(img)

  return {labels[i]: float(probs[i]) for i in range(len(labels))}

title = "Fruit and Vegetation Classifier"
description = '''A simple app to classify various fruits and vegetables '''

examples = [['Onion.jpg'],
            ['orange.jpg'],
            ['plum.jpg'],
            ['tomato.jpg'],
            ['banana.jpg']]
enable_queue = True

gr.Interface (fn= predict,
              inputs=gr.inputs.Image(shape = (224,224)), 
              outputs= gr.outputs.Label(num_top_classes =3),
              title = title,
              description = description,
              examples = examples,
              flagging_options=["Incorrect Prediction"],
              enable_queue = enable_queue).launch(debug = True, share=True)