Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1cZj5_KDg88LfgRs3U7RTXz6MiGy_485i
|
| 8 |
+
|
| 9 |
+
## FRUIT CLASSIFICATION APP
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from fastai.vision.all import *
|
| 14 |
+
import skimage
|
| 15 |
+
import pathlib
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import albumentations
|
| 18 |
+
from albumentations.pytorch import ToTensorV2
|
| 19 |
+
import timm
|
| 20 |
+
|
| 21 |
+
class AlbumentationsTransform (RandTransform):
|
| 22 |
+
split_idx,order=None,2
|
| 23 |
+
def __init__(self, train_aug, valid_aug): store_attr()
|
| 24 |
+
|
| 25 |
+
def before_call(self, b, split_idx):
|
| 26 |
+
self.idx = split_idx
|
| 27 |
+
|
| 28 |
+
def encodes(self, img: PILImage):
|
| 29 |
+
if self.idx == 0:
|
| 30 |
+
aug_img = self.train_aug(image=np.array(img))['image']
|
| 31 |
+
else:
|
| 32 |
+
aug_img = self.valid_aug(image=np.array(img))['image']
|
| 33 |
+
return PILImage.create(aug_img)
|
| 34 |
+
|
| 35 |
+
def get_valid_aug(): return albumentations.Compose([
|
| 36 |
+
albumentations.Resize(224, 224),
|
| 37 |
+
], p=1.0)
|
| 38 |
+
|
| 39 |
+
learn = load_learner(path + 'fruit_model_v2.pkl')
|
| 40 |
+
|
| 41 |
+
labels = learn.dls.vocab
|
| 42 |
+
|
| 43 |
+
def predict(img):
|
| 44 |
+
|
| 45 |
+
pred,pred_idx,probs = learn.predict(img)
|
| 46 |
+
|
| 47 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 48 |
+
|
| 49 |
+
title = "Fruit and Vegetation Classifier"
|
| 50 |
+
description = '''A simple app to classify various fruits and vegetables '''
|
| 51 |
+
|
| 52 |
+
examples = [[path + 'Onion.jpg'],
|
| 53 |
+
[path + 'orange.jpg'],
|
| 54 |
+
[path + 'plum.jpg'],
|
| 55 |
+
[path + 'tomato.jpg'],
|
| 56 |
+
[path + 'banana.jpg']]
|
| 57 |
+
enable_queue = True
|
| 58 |
+
|
| 59 |
+
gr.Interface (fn= predict,
|
| 60 |
+
inputs=gr.inputs.Image(shape = (224,224)),
|
| 61 |
+
outputs= gr.outputs.Label(num_top_classes =3),
|
| 62 |
+
title = title,
|
| 63 |
+
description = description,
|
| 64 |
+
examples = examples,
|
| 65 |
+
flagging_options=["Incorrect Prediction"],
|
| 66 |
+
enable_queue = enable_queue).launch(debug = True, share=True)
|
| 67 |
+
|