Koleshjr commited on
Commit
831e0c1
1 Parent(s): a051841

Upload fruit_classifier_app.py

Browse files
Files changed (1) hide show
  1. fruit_classifier_app.py +90 -0
fruit_classifier_app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Fruit_Classifier_app.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1wFmOPbrpLNAJxJsRfdiNoE_r1f8brR6M
8
+
9
+ ## FRUIT CLASSIFICATION APP
10
+ """
11
+
12
+ !pip install gradio
13
+ !pip install -U albumentations
14
+ !pip install -U albumentations
15
+ !pip install opencv-python==4.5.4.60
16
+ !pip install timm==0.6.2.dev0
17
+
18
+ #Start by connecting gdrive into the google colab
19
+
20
+ from google.colab import drive
21
+
22
+ drive.mount('/content/gdrive')
23
+ path = '/content/gdrive/MyDrive/Fruit_Project/'
24
+
25
+ import gradio as gr
26
+ from fastai.vision.all import *
27
+ import skimage
28
+ import pathlib
29
+ from PIL import Image
30
+ import albumentations
31
+ from albumentations.pytorch import ToTensorV2
32
+ import timm
33
+
34
+ plt = platform.system()
35
+ if plt == 'Linux':
36
+ pathlib.WindowsPath = pathlib.PosixPath
37
+
38
+
39
+
40
+ # !unzip -o -q /content/gdrive/MyDrive/sign_prediction/ModImages -d Images/
41
+
42
+ class AlbumentationsTransform (RandTransform):
43
+ split_idx,order=None,2
44
+ def __init__(self, train_aug, valid_aug): store_attr()
45
+
46
+ def before_call(self, b, split_idx):
47
+ self.idx = split_idx
48
+
49
+ def encodes(self, img: PILImage):
50
+ if self.idx == 0:
51
+ aug_img = self.train_aug(image=np.array(img))['image']
52
+ else:
53
+ aug_img = self.valid_aug(image=np.array(img))['image']
54
+ return PILImage.create(aug_img)
55
+
56
+ def get_valid_aug(): return albumentations.Compose([
57
+ albumentations.Resize(224, 224),
58
+ ], p=1.0)
59
+
60
+ learn = load_learner(path + 'fruit_model_v2.pkl')
61
+
62
+ labels = learn.dls.vocab
63
+
64
+ def predict(img):
65
+
66
+ pred,pred_idx,probs = learn.predict(img)
67
+
68
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
69
+
70
+ # predict('/content/gdrive/MyDrive/Fruit_Project/Onion.jpg')
71
+
72
+ title = "Fruit and Vegetation Classifier"
73
+ description = '''A simple app to classify various fruits and vegetables '''
74
+
75
+ examples = [[path + 'Onion.jpg'],
76
+ [path + 'orange.jpg'],
77
+ [path + 'plum.jpg'],
78
+ [path + 'tomato.jpg'],
79
+ [path + 'banana.jpg']]
80
+ enable_queue = True
81
+
82
+ gr.Interface (fn= predict,
83
+ inputs=gr.inputs.Image(shape = (224,224)),
84
+ outputs= gr.outputs.Label(num_top_classes =3),
85
+ title = title,
86
+ description = description,
87
+ examples = examples,
88
+ flagging_options=["Incorrect Prediction"],
89
+ enable_queue = enable_queue).launch(debug = True, share=True)
90
+