osbornep8's picture
Upload app.py
cc47c88 verified
import streamlit as st
from fastai.vision.all import *
from fastcore.all import *
from fastbook import *
from PIL import Image
import numpy as np
@st.cache_resource
def load_model():
return load_learner('model.pkl')
@st.cache_data
def classify(_img):
model = load_model()
name, pred_idx, preds = model.predict(_img)
return name
@st.cache_data
def show_output(button, _output):
with st.container():
if (button) and (_output is not None):
with st.spinner(text='Loading...'):
out_array = np.asarray(_output)
name = classify(out_array)
url = search_images_ddg((f'{name} soccer player'), max_images=1)
img = download_url(url[0])
im = Image.open(img)
st.success(f'Congratulations! You Look Like: {name}!!')
st.divider()
st.image(im.resize((480,376)))
st.balloons()
if (show_output):
st.cache_data.clear()
st.cache_resource()
def button_predict():
st.subheader("That's a lovely picture!")
button = st.button('Click here to see who you look like!')
return button
title = ':red[Upload Your Image And See which 2022 FIFA WC Football Player You Look Like]! ⚽ 🧔🏽'
st.title(title)
st.divider()
with st.container():
st.header('You can upload your image below using one of the following two options:')
col1, col2 = st.columns(2)
with col1:
st.subheader('1. Upload an Image:')
upload_image = st.file_uploader(label='Local', type=['png', 'jpg', 'jpeg'])
with col2:
st.subheader('2. Take a Picture: ')
webcam_image = st.camera_input(label='Webcam', )
st.divider()
with st.container():
if (upload_image):
output = Image.open(upload_image)
st.image(upload_image, caption="Uploaded Image")
button = button_predict()
show_output(button, output)
elif (webcam_image):
output = Image.open(webcam_image)
st.image(webcam_image, caption="Webcam Image")
button = button_predict()
show_output(button, output)
else:
st.header('Please upload an image to proceed...')
st.divider()