{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e0bddbd4", "metadata": {}, "outputs": [], "source": [ "! pip install -Uqq fastbook\n", "import fastbook" ] }, { "cell_type": "code", "execution_count": 2, "id": "1b46b67e", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "from fastai.vision.widgets import *" ] }, { "cell_type": "code", "execution_count": 3, "id": "4a089247", "metadata": {}, "outputs": [], "source": [ "import pathlib\n", "temp = pathlib.PosixPath\n", "pathlib.PosixPath = pathlib.WindowsPath" ] }, { "cell_type": "code", "execution_count": 4, "id": "1f07c129", "metadata": {}, "outputs": [], "source": [ "path = Path()\n", "learn_inf = load_learner(path/'export.pkl', cpu=True)\n", "btn_upload = widgets.FileUpload()\n", "out_pl = widgets.Output()\n", "lbl_pred = widgets.Label()\n", "btn_run = widgets.Button(description='Classify')" ] }, { "cell_type": "code", "execution_count": 5, "id": "b319ee30", "metadata": {}, "outputs": [], "source": [ "def on_click_classify(change):\n", " img = PILImage.create(btn_upload.data[-1])\n", " out_pl.clear_output()\n", " with out_pl: display(img.to_thumb(128,128))\n", " pred, pred_idx, probs = learn_inf.predict(img)\n", " lbl_pred.value = f'Prediction: {str(pred)[10:]}; Probability: {probs[pred_idx]:.04f}'\n", " \n", "btn_run.on_click(on_click_classify)" ] }, { "cell_type": "code", "execution_count": 6, "id": "114b5a71", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "476e154bfede489a9560404bca8fff16", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(Label(value='Select your images'), FileUpload(value={}, description='Upload'), Button(descripti…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "VBox([widgets.Label('Select your images'),\n", " btn_upload, btn_run, out_pl, lbl_pred])" ] }, { "cell_type": "code", "execution_count": null, "id": "d640937e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "d925690840644bcd766647b280b19c370132b1426b7601e4e2f280a77c18a034" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }