halo ini project fruitszone
This commit is contained in:
commit
072b8da3bf
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
Binary file not shown.
After Width: | Height: | Size: 71 KiB |
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
Binary file not shown.
After Width: | Height: | Size: 56 KiB |
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,240 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Buah Alpukat',\n",
|
||||
" 'Buah Apel',\n",
|
||||
" 'Buah Belimbing',\n",
|
||||
" 'Buah Naga',\n",
|
||||
" 'Buah Lemon',\n",
|
||||
" 'Buah Nanas',\n",
|
||||
" 'Buah Pir',\n",
|
||||
" 'Buah Pisang',\n",
|
||||
" 'Buah Salak',\n",
|
||||
" 'Buah Stroberi']"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from fastai.vision.all import *\n",
|
||||
"\n",
|
||||
"learn = load_learner('model.pkl')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def format_name(name):\n",
|
||||
" if name != 'buah naga':\n",
|
||||
" return f'Buah {name.title()}'\n",
|
||||
" \n",
|
||||
" return name.title()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"names = list(map(format_name, learn.dls.vocab))\n",
|
||||
"names\n",
|
||||
"\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import firebase_admin\n",
|
||||
"from firebase_admin import credentials\n",
|
||||
"from firebase_admin import firestore\n",
|
||||
"\n",
|
||||
"cred = credentials.Certificate('firebase_key.json')\n",
|
||||
"app = firebase_admin.initialize_app(cred)\n",
|
||||
"db = firestore.client()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"doc_ref = db.collection(u'preds').add({\n",
|
||||
" u'image': u'Lovelace',\n",
|
||||
" u'prediction': 1815,\n",
|
||||
" u'time_added': firestore.SERVER_TIMESTAMP\n",
|
||||
"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"preds_ref = db.collection(u'preds')\n",
|
||||
"docs = preds_ref.stream()\n",
|
||||
"\n",
|
||||
"for doc in docs:\n",
|
||||
" data = base64.b64decode(doc.to_dict()['image'])\n",
|
||||
" buff = BytesIO(data)\n",
|
||||
" img = Image.open(buff)\n",
|
||||
" img.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Running on local URL: http://127.0.0.1:7860\n",
|
||||
"\n",
|
||||
"To create a public link, set `share=True` in `launch()`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(<gradio.routes.App at 0x7f3279fb6af0>, 'http://127.0.0.1:7860/', None)"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"\n",
|
||||
"<style>\n",
|
||||
" /* Turns off some styling */\n",
|
||||
" progress {\n",
|
||||
" /* gets rid of default border in Firefox and Opera. */\n",
|
||||
" border: none;\n",
|
||||
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
|
||||
" background-size: auto;\n",
|
||||
" }\n",
|
||||
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
|
||||
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
|
||||
" }\n",
|
||||
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
|
||||
" background: #F44336;\n",
|
||||
" }\n",
|
||||
"</style>\n"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import gradio as gr\n",
|
||||
"from fastai.vision.all import *\n",
|
||||
"import firebase_admin\n",
|
||||
"from firebase_admin import credentials\n",
|
||||
"from firebase_admin import firestore\n",
|
||||
"import base64\n",
|
||||
"from io import BytesIO\n",
|
||||
"\n",
|
||||
"cred = credentials.Certificate('firebase_key.json')\n",
|
||||
"app = firebase_admin.initialize_app(cred)\n",
|
||||
"db = firestore.client()\n",
|
||||
"\n",
|
||||
"learn = load_learner('model.pkl')\n",
|
||||
"names = list(map(lambda name: name.title(), learn.dls.vocab))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def classify(image):\n",
|
||||
" buffered = BytesIO()\n",
|
||||
" image.save(buffered, format=\"JPEG\")\n",
|
||||
" img_str = base64.b64encode(buffered.getvalue())\n",
|
||||
"\n",
|
||||
" pred, idx, probs = learn.predict(np.asarray(image))\n",
|
||||
"\n",
|
||||
" db.collection(u'preds').add({\n",
|
||||
" u'image': img_str,\n",
|
||||
" u'prediction': pred.title(),\n",
|
||||
" u'time_added': firestore.SERVER_TIMESTAMP\n",
|
||||
" })\n",
|
||||
"\n",
|
||||
" return dict(zip(names, map(float, probs)))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"with gr.Blocks() as demo:\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" image_input = gr.Image(label=\"Gambar\", shape=(200, 200), type='pil')\n",
|
||||
" predict_btn = gr.Button(\"Klasifikasi\", variant='primary')\n",
|
||||
" with gr.Column():\n",
|
||||
" chart = gr.Label(label=\"Hasil\")\n",
|
||||
"\n",
|
||||
" predict_btn.click(fn=classify, inputs=image_input,\n",
|
||||
" outputs=chart, api_name='classify_image')\n",
|
||||
"\n",
|
||||
"demo.launch()\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.9.12 ('base')",
|
||||
"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.12"
|
||||
},
|
||||
"orig_nbformat": 4,
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "fdff80770fe959a274e11f3f42c90636d375da6f3b1d0ebe5804fc741631cbf3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
import firebase_admin
|
||||
import gradio as gr
|
||||
import numpy as np
|
||||
from fastai.vision.all import *
|
||||
import json
|
||||
from firebase_admin import credentials, firestore
|
||||
import pathlib
|
||||
|
||||
temp = pathlib.PosixPath
|
||||
pathlib.PosixPath = pathlib.WindowsPath
|
||||
|
||||
cred = credentials.Certificate("firebase_key.json")
|
||||
app = firebase_admin.initialize_app(cred)
|
||||
db = firestore.client()
|
||||
|
||||
learn = load_learner("model.pkl")
|
||||
names = json.load(open("./translations.json"))
|
||||
|
||||
|
||||
def classify(image):
|
||||
buffered = BytesIO()
|
||||
image.save(buffered, format="JPEG")
|
||||
img_str = base64.b64encode(buffered.getvalue())
|
||||
|
||||
pred, idx, probs = learn.predict(np.asarray(image))
|
||||
|
||||
db.collection("preds").add( # inilo db
|
||||
{
|
||||
"image": img_str,
|
||||
"prediction": pred.title(),
|
||||
"time_added": firestore.SERVER_TIMESTAMP,
|
||||
}
|
||||
)
|
||||
|
||||
return [
|
||||
names[pred]["id"],
|
||||
names[pred]["en"],
|
||||
f"./audios/en/{pred}.mp3",
|
||||
f"./audios/id/{pred}.mp3",
|
||||
]
|
||||
|
||||
|
||||
with gr.Blocks(
|
||||
css=".gradio-container {background-image: url('file=Fruitzone.jpg')}"
|
||||
) as demo:
|
||||
with gr.Row():
|
||||
with gr.Column():
|
||||
image_input = gr.Webcam(label="Gambar", shape=(200, 200), type="pil")
|
||||
predict_btn = gr.Button("Cek Nama Buah Yuk", variant="primary")
|
||||
|
||||
with gr.Column():
|
||||
id_fruit_name = gr.Label(label="Bahasa indonesia")
|
||||
en_fruit_name = gr.Label(label="Bahasa inggris")
|
||||
en_audio = gr.Audio(label="Audio inggris")
|
||||
id_audio = gr.Audio(label="Audio indonesia")
|
||||
|
||||
predict_btn.click(
|
||||
fn=classify,
|
||||
inputs=image_input,
|
||||
outputs=[id_fruit_name, en_fruit_name, id_audio, en_audio],
|
||||
api_name="classify_image",
|
||||
)
|
||||
|
||||
demo.launch()
|
|
@ -0,0 +1,76 @@
|
|||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
import firebase_admin
|
||||
import gradio as gr
|
||||
import numpy as np
|
||||
from fastai.vision.all import *
|
||||
import json
|
||||
from firebase_admin import credentials, firestore
|
||||
import pathlib
|
||||
|
||||
temp = pathlib.PosixPath
|
||||
pathlib.PosixPath = pathlib.WindowsPath
|
||||
|
||||
cred = credentials.Certificate("firebase_key.json")
|
||||
app = firebase_admin.initialize_app(cred)
|
||||
db = firestore.client()
|
||||
|
||||
learn = load_learner("model.pkl")
|
||||
names = json.load(open("./translations.json"))
|
||||
|
||||
|
||||
def classify(image):
|
||||
buffered = BytesIO()
|
||||
image.save(buffered, format="JPEG")
|
||||
img_str = base64.b64encode(buffered.getvalue())
|
||||
|
||||
pred, idx, probs = learn.predict(np.asarray(image))
|
||||
|
||||
db.collection("preds").add(
|
||||
{
|
||||
"image": img_str,
|
||||
"prediction": pred.title(),
|
||||
"time_added": firestore.SERVER_TIMESTAMP,
|
||||
}
|
||||
)
|
||||
|
||||
return [
|
||||
names[pred]["id"],
|
||||
names[pred]["en"],
|
||||
f"./audios/en/{pred}.mp3",
|
||||
f"./audios/id/{pred}.mp3",
|
||||
]
|
||||
|
||||
|
||||
with gr.Blocks(
|
||||
css=".gradio-container {background-image: url('file=Background\fix.png');background-size: cover; background-size: 100% 100%;}"
|
||||
) as demo:
|
||||
with gr.Row():
|
||||
with gr.Column():
|
||||
image_input = gr.Webcam(label="Gambar", shape=(200, 200), type="pil")
|
||||
predict_id_btn = gr.Button("Prediksi Bahasa Indonesia", variant="primary")
|
||||
predict_en_btn = gr.Button("Predict English", variant="primary")
|
||||
|
||||
with gr.Column():
|
||||
id_fruit_name = gr.Label(label="Bahasa Indonesia")
|
||||
id_audio = gr.Audio(label="Audio Indonesia")
|
||||
with gr.Column():
|
||||
en_fruit_name = gr.Label(label="English")
|
||||
en_audio = gr.Audio(label="English Audio")
|
||||
|
||||
predict_id_btn.click(
|
||||
fn=classify,
|
||||
inputs=image_input,
|
||||
outputs=[id_fruit_name, id_audio],
|
||||
api_name="classify_image",
|
||||
)
|
||||
|
||||
predict_en_btn.click(
|
||||
fn=classify,
|
||||
inputs=image_input,
|
||||
outputs=[en_fruit_name, en_audio],
|
||||
api_name="classify_image",
|
||||
)
|
||||
|
||||
demo.launch()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,8 @@
|
|||
import os
|
||||
|
||||
file_path = "audios/en/Srikaya.mp3" # Ganti dengan jalur file yang ingin Anda periksa
|
||||
|
||||
if os.path.exists(file_path):
|
||||
print("File ada.")
|
||||
else:
|
||||
print("File tidak ada atau jalur file salah.")
|
|
@ -0,0 +1,209 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from fastai.vision.all import *\n",
|
||||
"\n",
|
||||
"learn = load_learner('model.pkl')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import firebase_admin\n",
|
||||
"from firebase_admin import credentials\n",
|
||||
"from firebase_admin import firestore\n",
|
||||
"import base64\n",
|
||||
"from io import BytesIO\n",
|
||||
"\n",
|
||||
"cred = credentials.Certificate('firebase_key.json')\n",
|
||||
"app = firebase_admin.initialize_app(cred)\n",
|
||||
"db = firestore.client()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"doc_ref = db.collection(u'preds').add({\n",
|
||||
" u'image': u'Lovelace',\n",
|
||||
" u'prediction': 1815,\n",
|
||||
" u'time_added': firestore.SERVER_TIMESTAMP\n",
|
||||
"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"preds_ref = db.collection(u'preds')\n",
|
||||
"docs = preds_ref.stream()\n",
|
||||
"\n",
|
||||
"for doc in docs:\n",
|
||||
" data = base64.b64decode(doc.to_dict()['image'])\n",
|
||||
" buff = BytesIO(data)\n",
|
||||
" img = Image.open(buff)\n",
|
||||
" img.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Running on local URL: http://127.0.0.1:7860\n",
|
||||
"\n",
|
||||
"To create a public link, set `share=True` in `launch()`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(<gradio.routes.App at 0x7f3279fb6af0>, 'http://127.0.0.1:7860/', None)"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"\n",
|
||||
"<style>\n",
|
||||
" /* Turns off some styling */\n",
|
||||
" progress {\n",
|
||||
" /* gets rid of default border in Firefox and Opera. */\n",
|
||||
" border: none;\n",
|
||||
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
|
||||
" background-size: auto;\n",
|
||||
" }\n",
|
||||
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
|
||||
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
|
||||
" }\n",
|
||||
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
|
||||
" background: #F44336;\n",
|
||||
" }\n",
|
||||
"</style>\n"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import gradio as gr\n",
|
||||
"from fastai.vision.all import *\n",
|
||||
"import firebase_admin\n",
|
||||
"from firebase_admin import credentials\n",
|
||||
"from firebase_admin import firestore\n",
|
||||
"import base64\n",
|
||||
"from io import BytesIO\n",
|
||||
"\n",
|
||||
"cred = credentials.Certificate('firebase_key.json')\n",
|
||||
"app = firebase_admin.initialize_app(cred)\n",
|
||||
"db = firestore.client()\n",
|
||||
"\n",
|
||||
"learn = load_learner('model.pkl')\n",
|
||||
"names = list(map(lambda name: name.title(), learn.dls.vocab))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def classify(image):\n",
|
||||
" buffered = BytesIO()\n",
|
||||
" image.save(buffered, format=\"JPEG\")\n",
|
||||
" img_str = base64.b64encode(buffered.getvalue())\n",
|
||||
"\n",
|
||||
" pred, idx, probs = learn.predict(np.asarray(image))\n",
|
||||
"\n",
|
||||
" db.collection(u'preds').add({\n",
|
||||
" u'image': img_str,\n",
|
||||
" u'prediction': pred.title(),\n",
|
||||
" u'time_added': firestore.SERVER_TIMESTAMP\n",
|
||||
" })\n",
|
||||
"\n",
|
||||
" return dict(zip(names, map(float, probs)))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"with gr.Blocks() as demo:\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" image_input = gr.Image(label=\"Gambar\", shape=(200, 200), type='pil')\n",
|
||||
" predict_btn = gr.Button(\"Klasifikasi\", variant='primary')\n",
|
||||
" with gr.Column():\n",
|
||||
" chart = gr.Label(label=\"Hasil\")\n",
|
||||
"\n",
|
||||
" predict_btn.click(fn=classify, inputs=image_input,\n",
|
||||
" outputs=chart, api_name='classify_image')\n",
|
||||
"\n",
|
||||
"demo.launch()\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.9.13 64-bit",
|
||||
"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.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]"
|
||||
},
|
||||
"orig_nbformat": 4,
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "b671c20432fcd147198c92e7f072af9e705f087eb990bee22b07f08caab9f630"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fastai
|
||||
firebase_admin
|
|
@ -0,0 +1,103 @@
|
|||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
import firebase_admin
|
||||
import gradio as gr
|
||||
import numpy as np
|
||||
from fastai.vision.all import *
|
||||
import json
|
||||
from firebase_admin import credentials, firestore
|
||||
import pathlib
|
||||
|
||||
temp = pathlib.PosixPath
|
||||
pathlib.PosixPath = pathlib.WindowsPath
|
||||
|
||||
cred = credentials.Certificate("firebase_key.json")
|
||||
app = firebase_admin.initialize_app(cred)
|
||||
db = firestore.client()
|
||||
|
||||
learn = load_learner("model.pkl")
|
||||
names = json.load(open("./translations.json"))
|
||||
|
||||
with gr.Blocks(
|
||||
css=".gradio-container {background-image: url('file=Background/4.jpeg');background-size: cover; background-size: 100% 100%;}" # .block.svelte-kz0ejz{background-color: rgba(0,0,0,0);} kalo butuh transparan
|
||||
) as demo:
|
||||
gr.Markdown(" ")
|
||||
gr.Markdown(" ")
|
||||
with gr.Row():
|
||||
with gr.Column():
|
||||
image_input = gr.Webcam(label="Gambar", shape=(200, 200), type="pil")
|
||||
with gr.Row():
|
||||
predict_id_btn = gr.Button("Bahasa Indonesia", variant="primary")
|
||||
predict_en_btn = gr.Button("Bahasa Inggris", variant="secondary")
|
||||
|
||||
with gr.Column():
|
||||
with gr.Column(visible=False) as id_fruit_name_col:
|
||||
id_fruit_name = gr.Label(label="Bahasa indonesia")
|
||||
id_audio = gr.Audio(label="Audio indonesia")
|
||||
with gr.Column(visible=False) as en_fruit_name_col:
|
||||
en_fruit_name = gr.Label(label="Bahasa inggris")
|
||||
en_audio = gr.Audio(label="Audio inggris")
|
||||
|
||||
def classify_id(image):
|
||||
buffered = BytesIO()
|
||||
image.save(buffered, format="JPEG")
|
||||
img_str = base64.b64encode(buffered.getvalue())
|
||||
|
||||
pred, idx, probs = learn.predict(np.asarray(image))
|
||||
|
||||
db.collection("preds").add( # inilo db
|
||||
{
|
||||
"image": img_str,
|
||||
"prediction": pred.title(),
|
||||
"time_added": firestore.SERVER_TIMESTAMP,
|
||||
}
|
||||
)
|
||||
id_fruit_name_col = gr.update(visible=True)
|
||||
en_fruit_name_col = gr.update(visible=False)
|
||||
return [
|
||||
names[pred]["id"],
|
||||
f"./audios/id/" + names[pred]["id"] + ".mp3",
|
||||
id_fruit_name_col,
|
||||
en_fruit_name_col,
|
||||
]
|
||||
|
||||
def classify_en(image):
|
||||
buffered = BytesIO()
|
||||
image.save(buffered, format="JPEG")
|
||||
img_str = base64.b64encode(buffered.getvalue())
|
||||
|
||||
pred, idx, probs = learn.predict(np.asarray(image))
|
||||
|
||||
db.collection("preds").add( # inilo db
|
||||
{
|
||||
"image": img_str,
|
||||
"prediction": pred.title(),
|
||||
"time_added": firestore.SERVER_TIMESTAMP,
|
||||
}
|
||||
)
|
||||
en_fruit_name_col = gr.update(visible=True)
|
||||
id_fruit_name_col = gr.update(visible=False)
|
||||
return [
|
||||
names[pred]["en"],
|
||||
f"./audios/en/" + names[pred]["id"] + ".mp3",
|
||||
id_fruit_name_col,
|
||||
en_fruit_name_col,
|
||||
]
|
||||
|
||||
predict_id_btn.click(
|
||||
fn=classify_id,
|
||||
inputs=image_input,
|
||||
outputs=[id_fruit_name, id_audio, id_fruit_name_col, en_fruit_name_col],
|
||||
api_name="classify_image",
|
||||
)
|
||||
predict_en_btn.click(
|
||||
fn=classify_en,
|
||||
inputs=image_input,
|
||||
outputs=[en_fruit_name, en_audio, id_fruit_name_col, en_fruit_name_col],
|
||||
api_name="classify_image",
|
||||
)
|
||||
|
||||
|
||||
# demo.launch(share=True)
|
||||
demo.launch()
|
|
@ -0,0 +1,95 @@
|
|||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
import firebase_admin
|
||||
import gradio as gr
|
||||
import numpy as np
|
||||
from fastai.vision.all import *
|
||||
import json
|
||||
from firebase_admin import credentials, firestore
|
||||
import pathlib
|
||||
|
||||
temp = pathlib.PosixPath
|
||||
pathlib.PosixPath = pathlib.WindowsPath
|
||||
|
||||
cred = credentials.Certificate("firebase_key.json")
|
||||
app = firebase_admin.initialize_app(cred)
|
||||
db = firestore.client()
|
||||
|
||||
learn = load_learner("model.pkl")
|
||||
names = json.load(open("./translations.json"))
|
||||
|
||||
|
||||
def classify_id(image):
|
||||
buffered = BytesIO()
|
||||
image.save(buffered, format="JPEG")
|
||||
img_str = base64.b64encode(buffered.getvalue())
|
||||
|
||||
pred, idx, probs = learn.predict(np.asarray(image))
|
||||
|
||||
db.collection("preds").add( # inilo db
|
||||
{
|
||||
"image": img_str,
|
||||
"prediction": pred.title(),
|
||||
"time_added": firestore.SERVER_TIMESTAMP,
|
||||
}
|
||||
)
|
||||
|
||||
return [
|
||||
names[pred]["id"],
|
||||
f"./audios/id/" + names[pred]["id"] + ".mp3",
|
||||
]
|
||||
|
||||
|
||||
def classify_en(image):
|
||||
buffered = BytesIO()
|
||||
image.save(buffered, format="JPEG")
|
||||
img_str = base64.b64encode(buffered.getvalue())
|
||||
|
||||
pred, idx, probs = learn.predict(np.asarray(image))
|
||||
|
||||
db.collection("preds").add( # inilo db
|
||||
{
|
||||
"image": img_str,
|
||||
"prediction": pred.title(),
|
||||
"time_added": firestore.SERVER_TIMESTAMP,
|
||||
}
|
||||
)
|
||||
|
||||
return [
|
||||
# names[pred]["id"],
|
||||
names[pred]["en"],
|
||||
f"./audios/en/" + names[pred]["id"] + ".mp3",
|
||||
]
|
||||
|
||||
|
||||
with gr.Blocks(
|
||||
css=".gradio-container {background-image: url('file=Background/Fruitzone.jpg');background-size: cover; background-size: 100% 100%;}.block.svelte-kz0ejz{background-color: rgba(0,0,0,0);}"
|
||||
) as demo:
|
||||
with gr.Row():
|
||||
with gr.Column():
|
||||
image_input = gr.Webcam(label="Gambar", shape=(200, 200), type="pil")
|
||||
predict_id_btn = gr.Button("Bahasa Indonesia", variant="primary")
|
||||
predict_en_btn = gr.Button("Bahasa Inggris", variant="secondary")
|
||||
|
||||
with gr.Column():
|
||||
id_fruit_name = gr.Label(label="Bahasa indonesia", visible=False)
|
||||
id_audio = gr.Audio(label="Audio indonesia", visible=False)
|
||||
en_fruit_name = gr.Label(label="Bahasa inggris", visible=False)
|
||||
en_audio = gr.Audio(label="Audio inggris", visible=False)
|
||||
|
||||
predict_id_btn.click(
|
||||
fn=classify_id,
|
||||
inputs=image_input,
|
||||
outputs=[id_fruit_name, id_audio],
|
||||
api_name="classify_image",
|
||||
)
|
||||
predict_en_btn.click(
|
||||
fn=classify_en,
|
||||
inputs=image_input,
|
||||
outputs=[en_fruit_name, en_audio],
|
||||
api_name="classify_image",
|
||||
)
|
||||
|
||||
|
||||
demo.launch()
|
|
@ -0,0 +1,86 @@
|
|||
{
|
||||
"apel": {
|
||||
"id": "Apel",
|
||||
"en": "Apple"
|
||||
},
|
||||
"anggur": {
|
||||
"id": "anggur",
|
||||
"en": "grape"
|
||||
},
|
||||
"alpukat": {
|
||||
"id": "Alpukat",
|
||||
"en": "Avocado"
|
||||
},
|
||||
"belimbing": {
|
||||
"id": "Belimbing",
|
||||
"en": "Starfruit"
|
||||
},
|
||||
"buah naga": {
|
||||
"id": "Buah Naga",
|
||||
"en": "Dragonfruit"
|
||||
},
|
||||
"delima": {
|
||||
"id": "Delima",
|
||||
"en": "Pomegranate"
|
||||
},
|
||||
"durian": {
|
||||
"id": "Durian",
|
||||
"en": "Durian"
|
||||
},
|
||||
"jeruk": {
|
||||
"id": "Jeruk",
|
||||
"en": "Orange"
|
||||
},
|
||||
"kiwi": {
|
||||
"id": "Kiwi",
|
||||
"en": "Kiwi"
|
||||
},
|
||||
"lemon": {
|
||||
"id": "Lemon",
|
||||
"en": "Lemon"
|
||||
},
|
||||
"mangga": {
|
||||
"id": "Mangga",
|
||||
"en": "Mango"
|
||||
},
|
||||
"manggis": {
|
||||
"id": "Manggis",
|
||||
"en": "mangosteen"
|
||||
},
|
||||
"melon": {
|
||||
"id": "Melon",
|
||||
"en": "Melon"
|
||||
},
|
||||
"nanas": {
|
||||
"id": "Nanas",
|
||||
"en": "Pineapple"
|
||||
},
|
||||
"pir": {
|
||||
"id": "Pir",
|
||||
"en": "Pear"
|
||||
},
|
||||
"pisang": {
|
||||
"id": "Pisang",
|
||||
"en": "Banana"
|
||||
},
|
||||
"rambutan": {
|
||||
"id": "Rambutan",
|
||||
"en": "Rambutan"
|
||||
},
|
||||
"salak": {
|
||||
"id": "Salak",
|
||||
"en": "Snakefruit"
|
||||
},
|
||||
"semangka": {
|
||||
"id": "Semangka",
|
||||
"en": "Watermelon"
|
||||
},
|
||||
"srikaya": {
|
||||
"id": "Srikaya",
|
||||
"en": "Srikaya"
|
||||
},
|
||||
"stroberi": {
|
||||
"id": "Stroberi",
|
||||
"en": "Strawberry"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue