File size: 2,429 Bytes
831e0c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b6238a
831e0c1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""Fruit_Classifier_app.ipynb

Automatically generated by Colaboratory.

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

## FRUIT CLASSIFICATION APP
"""

!pip install gradio
!pip install -U albumentations
!pip install -U albumentations
!pip install opencv-python==4.5.4.60
!pip install timm==0.6.2.dev0

#Start by connecting gdrive into the google colab

from google.colab import drive

drive.mount('/content/gdrive')
path = '/content/gdrive/MyDrive/Fruit_Project/'

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

plt = platform.system()
if plt == 'Linux':
  pathlib.WindowsPath = pathlib.PosixPath



# !unzip -o -q /content/gdrive/MyDrive/sign_prediction/ModImages -d Images/

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(path + '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))}

# predict('/content/gdrive/MyDrive/Fruit_Project/Onion.jpg')

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

examples = [[path + 'Onion.jpg'],
            [path + 'orange.jpg'],
            [path + 'plum.jpg'],
            [path + 'tomato.jpg'],
            [path + '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()