File size: 1,513 Bytes
247bf63
690fe91
daeae8e
c06002b
690fe91
 
247bf63
 
690fe91
daeae8e
 
690fe91
daeae8e
690fe91
 
 
 
02be56f
 
690fe91
 
 
 
 
 
 
 
 
 
 
 
02be56f
 
690fe91
 
 
02be56f
247bf63
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
import gradio as gr
import pandas as pd
# import pathlib
from fastbook import *
from fastai.vision.widgets import *
from fastai.vision.all import *


# Load the model. "PosixPath" is something with windows/linux, I dont know really.
# temp = pathlib.PosixPath
# pathlib.PosixPath = pathlib.WindowsPath
learn = load_learner('model.pkl') # Load the model itself
# pathlib.PosixPath = temp
categories = learn.dls.vocab # Get the list of labels from the model

# Load the list of trash-sorting, use the items as index.
df_sort = pd.read_csv('Lista.csv',sep =";").set_index('Avfall')

def classify_image(img):
    # Make the prediction
    trash,idx,probs = learn.predict(PILImage.create(img)) # Make prediction
    df = pd.DataFrame()    # Create dataframe
    df['categories'] = categories # Add categories to dataframe
    df['probabilities'] = probs.numpy() # Add probabilities to dataframe
    sorted_df = df.sort_values(by=['probabilities'], ascending=False).head() # Sort by probability, highest first, take the top 5
    predictions = dict(zip(sorted_df['categories'].tolist(),map(float,sorted_df['probabilities'].tolist()))) # Now convert to a dictionary that we return later
    
    
    # Create sorting statement
    sort_text = "Sorteras som " + df_sort.loc[trash].tolist()[0]
    return "Det där är...", predictions, sort_text # Return the dictionary


image = gr.Image(type='pil')
label = ["text",gr.Label(),"text"]

iface = gr.Interface(fn=classify_image, inputs=image, outputs=label)
iface.launch()