File size: 1,487 Bytes
2c8c2f8
65a728c
 
 
 
 
 
 
 
 
 
 
 
 
 
2c8c2f8
65a728c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c8c2f8
65a728c
 
ec46c47
65a728c
 
 
 
 
 
 
 
 
 
2c8c2f8
65a728c
2c8c2f8
65a728c
 
 
2c8c2f8
 
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
#import gradio as gr
import fastbook
fastbook.setup_book()
from fastbook import *

"""
Get the prediction labels and their accuracies, then return the results as a dictionary.

[obj] - tensor matrix containing the predicted accuracy given from the model
[learn] - fastai learner needed to get the labels
[thresh] - minimum accuracy threshold to returning results
"""
def get_pred_classes(obj, learn, thresh):
    labels = []
    # get list of classes from csv--replace
    with open('classes.txt', 'r') as f:
      for line in f:
        labels.append(line.strip('\n'))

    predictions = {}
    x=0
    for item in obj:
        acc= round(item.item(), 3)
        if acc > thresh:
            predictions[labels[x]] = round(acc, 3)
        x+=1

    predictions =sorted(predictions.items(), key=lambda x: x[1], reverse=True)

    return predictions

def get_x(r): return 'images'/r['img_name']
def get_y(r): return [t for t in r['tags'].split(' ') if t in pop_tags]

learn = load_learner('model-large-basic-10e.pkl')

def predict_single_img(imf, thresh=0.2, learn=learn):
  
  img = PILImage.create(imf)

  #img.show() #show image
  _, _, pred_pct = learn.predict(img) #predict while ignoring first 2 array inputs
  img.show() #show image
  return str(get_pred_classes(pred_pct, learn, thresh))

predict_single_img('test/mask.jpeg')

"""
iface = gr.Interface(fn=predict_single_img, 
                     inputs=["image","number"], 
                     outputs="text")
iface.launch()
"""