tomweks/fontsrecognizer
2
1{2 "nbformat": 4,3 "nbformat_minor": 0,4 "metadata": {5 "colab": {6 "provenance": []7 },8 "kernelspec": {9 "name": "python3",10 "display_name": "Python 3"11 },12 "language_info": {13 "name": "python"14 }15 },16 "cells": [17 {18 "cell_type": "code",19 "source": [20 "!pip install -Uqq fastai gradio nbdev"21 ],22 "metadata": {23 "id": "LyRA7KSuOt6f"24 },25 "execution_count": null,26 "outputs": []27 },28 {29 "cell_type": "code",30 "execution_count": null,31 "metadata": {32 "id": "P9petL8cIBXd"33 },34 "outputs": [],35 "source": [36 "#|default_exp app"37 ]38 },39 {40 "cell_type": "code",41 "source": [42 "#|export\n",43 "from fastai.vision.all import *\n",44 "import gradio as gr"45 ],46 "metadata": {47 "id": "6olANYYvILjT"48 },49 "execution_count": null,50 "outputs": []51 },52 {53 "cell_type": "code",54 "source": [55 "im = PILImage.create('garamond.png')\n",56 "im.thumbnail((192, 192))\n",57 "im"58 ],59 "metadata": {60 "id": "JNb8CfVCRM3C"61 },62 "execution_count": null,63 "outputs": []64 },65 {66 "cell_type": "code",67 "source": [68 "#|export\n",69 "learn = load_learner('model.pkl')"70 ],71 "metadata": {72 "id": "FFxZjXWrP-Fg"73 },74 "execution_count": null,75 "outputs": []76 },77 {78 "cell_type": "code",79 "source": [80 "learn.predict(im)"81 ],82 "metadata": {83 "id": "gA5vqoTISPVL"84 },85 "execution_count": null,86 "outputs": []87 },88 {89 "cell_type": "code",90 "source": [91 "#|export\n",92 "categories = ('Garamond', 'Helvetica')\n",93 "\n",94 "def classify_image(img):\n",95 " pred, idx, probs = learn.predict(img)\n",96 " return dict(zip(categories, map(float, probs)))"97 ],98 "metadata": {99 "id": "bScV4g0eQUgh"100 },101 "execution_count": null,102 "outputs": []103 },104 {105 "cell_type": "code",106 "source": [107 "classify_image(im)"108 ],109 "metadata": {110 "id": "_yMhyT_hSXt0"111 },112 "execution_count": null,113 "outputs": []114 },115 {116 "cell_type": "code",117 "source": [118 "#|export\n",119 "image = gr.inputs.Image(shape=(192, 192))\n",120 "label = gr.outputs.Label()\n",121 "examples = ['garamond.png', 'helvetica.png']\n",122 "\n",123 "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",124 "intf.launch(inline=False)"125 ],126 "metadata": {127 "id": "BTRZvR-3SdRp"128 },129 "execution_count": null,130 "outputs": []131 },132 {133 "cell_type": "markdown",134 "source": [135 "# Export"136 ],137 "metadata": {138 "id": "BtgbVjRtTTqB"139 }140 },141 {142 "cell_type": "code",143 "source": [144 "import nbdev"145 ],146 "metadata": {147 "id": "_wNIvDXSTUwC"148 },149 "execution_count": null,150 "outputs": []151 },152 {153 "cell_type": "code",154 "source": [155 "nbdev.export.nb_export('app.ipynb', 'app')"156 ],157 "metadata": {158 "id": "CMlwAQz0Taj0"159 },160 "execution_count": null,161 "outputs": []162 }163 ]164}