File size: 1,103 Bytes
8cd0e8b
f59dfb2
 
0aa6c36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f59dfb2
87bb2e6
 
 
 
0aa6c36
 
 
 
 
 
 
 
 
 
 
 
 
 
76a54a5
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
from fastai.vision.all import load_learner
import gradio as gr

cap_labels = cap_labels = {
    'baseball cap',
    'beanie cap',
    'fedora cap',
    'cowboy hat',
    'kepi cap',
    'flat cap',
    'trucker cap',
    # 'newsboy cap'
    'pork pie hat',
    'bowler hat',
    'top hat',
    'sun hat',
    'boater hat',
    # 'ivy cap',
    'bucket hat',
    'balaclava cap',
    'turban cap',
    'taqiyah cap',
    'rasta cap',
    'visor cap'
}

version = 1
model_path = f"cap-recognizer-v{version}.pkl"
model = load_learner(model_path)

def recognize_image(image):
    pred, idx, probs = model.predict(image)
    return dict(zip(sorted(cap_labels), map(float, probs)))

image = gr.inputs.Image(shape=(192, 192))
label = gr.outputs.Label()
examples = [
            'test_images/test_0.jpg',
            'test_images/test_1.jpg',
            'test_images/test_2.jpg', 
            'test_images/test_3.jpg',
            'test_images/test_4.jpg',
            'test_images/test_5.jpg']
iface = gr.Interface(fn=recognize_image, inputs=image, outputs=label, examples=examples)
iface.launch(inline=False)