File size: 2,083 Bytes
642a5f0
 
 
 
 
22535ad
a6e6021
 
642a5f0
 
 
 
 
 
 
 
 
 
 
 
22535ad
642a5f0
 
22535ad
642a5f0
 
 
 
 
 
 
 
 
22535ad
642a5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import requests
import numpy as np
import streamlit as st
import tensorflow as tf
from huggingface_hub import from_pretrained_keras
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from PIL import Image
from io import BytesIO


def make_prediction(url, model, race_names):
    response = requests.get(url)
    img = Image.open(BytesIO(response.content)).resize((180, 180))
    img_array = img_to_array(img)
    img_array = tf.expand_dims(img_array, 0)  # Create batch axis

    predictions = model.predict(img_array)

    top5 = predictions.argsort()[0, -1:-6:-1]

    breakdown = []
    for race, acc in zip(np.array(race_names)[top5], predictions[0, top5]):
        breakdown.append(f'{race} at {acc:.2%}')
    return breakdown


def main():

    race_file = open('race_names.json', 'r')
    race_names = json.load(race_file)
    # Load model
    model = from_pretrained_keras('JbIPS/DogRace')

    st.set_page_config("Who let's the dogs out")
    st.title('Quelle est ta race de chien totem ?')

    st.text('''
    Pour découvrir ta race de chien, colle l'adresse d'une photo.
    ''')
    url = st.text_input('URL de la photo')
    predict_btn = st.button('Prédire')
    if predict_btn:
        pred = make_prediction(url, model, race_names)
        main_race = pred[0].split(' at')[0].lower()
        main_race = '/'.join(reversed(main_race.replace('-', '').split(' ')))
        if main_race.startswith('husky'):
            main_race = main_race.split('/')[0]
        col1, col2 = st.columns(2)
        with col1:
            st.image(url)
        with col2:
            response = requests.get(url=f'https://dog.ceo/api/breed/{main_race}/images/random').json()
            if response['status'] == 'success':
                st.image(response['message'])
            else:
                st.text(main_race)
                st.text(response)
        st.write('Les races qui te correspondent le plus sont :')
        for race in pred:
            st.write(race)


if __name__ == '__main__':
    main()