File size: 1,913 Bytes
0777b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4f4aa4
afa9738
 
0777b3a
 
 
 
 
afa9738
c6bb0b9
 
0777b3a
afa9738
 
 
 
 
 
 
 
 
0777b3a
 
 
 
 
afa9738
 
0777b3a
 
 
 
 
 
 
 
afa9738
0777b3a
 
 
 
4119868
0777b3a
 
 
 
afa9738
 
 
 
 
c70a3f9
afa9738
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from statistics import mode

import streamlit as st
from fastai.vision.all import *
from PIL import Image

from Processor import Processor


@st.experimental_singleton
def initialize_app():
    return Processor(load_learner('model.pkl'))


def process_images(images, processor: Processor):
    filtered_images = []
    result = []
    class_names = list(
        map(lambda name: {name: 0}, processor.inference.dls.vocab))

    for image in images:
        image = Image.open(image)
        if processor.filter_image(image):
            filtered_images.append(np.asarray(image))

    for img in filtered_images:
        result.append(processor.classify_image(img)[0])

    if len(result) == 0:
        return None

    for res_name in result:
        for idx, class_name in enumerate(class_names):
            for key, value in class_name.items():
                if res_name == key:
                    class_names[idx][key] = value + 1

    outfit = mode(result)

    with open(f'./texts/{outfit}.txt') as text:
        personality = text.read()

    return {'outfit': outfit.title(), 'personality': personality,
            'chart': class_names}


# Streamlit UI

processor = initialize_app()

st.title('Instagram Clothes Psychology (Photos)')
uploaded_photos = st.file_uploader(label="Upload photos", type=[
    'jpg', 'jpeg'], accept_multiple_files=True)

photos_empty = True if len(uploaded_photos) == 0 else False

is_clicked = st.button(label='Predict Personality',
                       disabled=photos_empty)

if is_clicked:
    with st.spinner('Please wait...'):
        result = process_images(uploaded_photos, processor)
        if result is None:
            st.write('Tidak ditemukan gambar yang valid')
        else:
            st.header('Your personality is..')
            st.subheader(result['outfit'])
            st.markdown(result['personality'])
            st.bar_chart(result['chart'])