File size: 4,503 Bytes
1987bed 7ce9ab5 71fac2a a2c132a 1987bed 7ce9ab5 0f4b23e 78a12d4 0f4b23e 78a12d4 5b311b1 78a12d4 91cc7ac 78a12d4 0917cd6 71fac2a 78a12d4 71fac2a 5cd635a dfd85a3 5cd635a 7da8f80 5cd635a 78a12d4 f16cc96 6f50425 6814cdf 5b311b1 6814cdf 6f50425 6814cdf 1987bed 78a12d4 0728d62 6f50425 eb84283 5cd635a 0728d62 5cd635a f16cc96 6f50425 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
__all__ = ['install', 'learn', 'categories', 'classify_image', 'classify_image_url', 'image', 'label', 'examples', 'intf', 'intf_text_url']
# Kept getting "No module named 'fastai'" from huggingface..workaround:
# https://stackoverflow.com/a/50255019
import subprocess
import sys
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
install("fastai")
from fastai.vision.all import *
#from fast.ai.vision.all import *
import gradio as gr
# def greet(name):
# return "Hello " + name + "!!"
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
# iface.launch()
# from:
# https://course.fast.ai/Lessons/lesson2.html
# https://github.com/fastai/fastbook/blob/master/02_production.ipynb
#learn_inf = load_learner(path/'export.pkl')
#learn_inf.predict('images/grizzly.jpg')
#learn_inf.dls.vocab
# model created from: https://www.kaggle.com/code/zachwormgoor/stock-photo-recognizer
learn = load_learner('model - 712 images 3 epochs.pkl')
categories = ('amateur', 'stock')
#may have categories swapped?
#add probs to UI somewhere
def classify_image(img):
tens = tensor(img) #fix apparently needed after fastai 2.7.11 released
pred,idx,probs = learn.predict(tens)
return dict(zip(categories, map(float,probs)))
from fastdownload import download_url
import os
def classify_image_url(url_text):
try:
dest = 'temp.jpg'
download_url(url_text, dest, show_progress=False)
im = Image.open(dest)
img = im.to_thumb(256,256)
#resize_images(dest, max_size=400, dest=dest)
os.remove(dest)
return classify_image(img)
except:
# in case there is any error, invalid URL or invalid image, etc., not sure how Gradio will handle a runtime exception so catching it to be safe
return { categories[0]: 0.0, categories[1]: 0.0 }
def classify_image_url_debug(url_text):
try:
dest = 'temp.jpg'
download_url(url_text, dest, show_progress=False)
im = Image.open(dest)
img = im.to_thumb(256,256)
#resize_images(dest, max_size=400, dest=dest)
os.remove(dest)
temp = classify_image(img)
return "Success: " + str(temp)
except Exception as ex:
# in case there is any error, invalid URL or invalid image, etc., not sure how Gradio will handle a runtime exception so catching it to be safe
#return { categories[0]: 0.0, categories[1]: 0.0 }
error = f"{type(ex).__name__} was raised: {ex}"
return error;
image = gr.inputs.Image(shape=(192, 192))
label = gr.outputs.Label()
examples = ['stock.jpg', 'stock2.jpg', 'stock-easy.jpg', 'stock-easy2.jpg', 'combine-stock.jpg', 'washing-machine-stock.jpg', 'glasses-amateur.jpg', 'amateur.jpg', 'amateur2.jpg', 'amateur-easy.jpg', 'amateur-easy2.jpg', 'unsure.jpg']
# stock: https://cdn.pixabay.com/photo/2022/02/11/12/12/pforphoto-7007231_1280.jpg
# stock2: https://cdn.pixabay.com/photo/2019/09/25/13/57/business-4503747_960_720.jpg
# stock-easy: https://scienceline.org/wp-content/uploads/2015/10/apple-926456_1280.jpg
# stock-easy2: https://ph-files.imgix.net/6b5cb0af-8bef-4700-b35f-8576e0ec58d8.jpeg?auto=format&fit=crop&frame=1&h=512&w=1024
# washing-machine-stock: https://live.staticflickr.com/7313/12775521983_e0a67aeb86_n.jpg
# combine-stock: https://live.staticflickr.com/5765/22059912468_ec5c315be4_b.jpg
# glasses-amateur: http://2.bp.blogspot.com/-dpygWXrSzRo/VfiIfzsWuxI/AAAAAAAAGvc/SNLnjV-IzO4/s1600/Green%2BTea%2Band%2BOrange%2BCollard%2BRecipe.jpg
# amateur: https://cimg3.ibsrv.net/cimg/www.honda-tech.com/1024x768_85/479/AAApril-2-Control-Arms-01-58479.jpg
# amateur2: https://www.iflash.xyz/images/diyMod/img7.jpg
# unsure: https://en.wikipedia.org/wiki/Joshua_Bean
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
# intf.launch(inline=False)
intf_text_url = gr.Interface(fn=classify_image_url, inputs="text", outputs=gr.outputs.Label())#, examples=example_urls)
intf_text_url_debug = gr.Interface(fn=classify_image_url_debug, inputs="text", outputs="text")
# intf_text_url.launch(inline=False)
# https://github.com/gradio-app/gradio/issues/450
gr.TabbedInterface( [intf, intf_text_url, intf_text_url_debug], ["Image file upload", "Image URL", "Image URL debug" ] ).launch()
|