Created
January 2, 2024 21:34
-
-
Save ch-hristov/db6cde1547f918571cbd907d0146f06a to your computer and use it in GitHub Desktop.
d8n-public.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyN8CVlpnGo6bPuO5/zzo4wF", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ch-hristov/db6cde1547f918571cbd907d0146f06a/d8n-public.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## API key\n", | |
"\n", | |
"\n", | |
"If you reached this notebook, it is likely that I also gave you an API key.\n", | |
"In order to use it you must input it in the secrets tab on the left side of the screen (look for the key icon).\n", | |
"\n", | |
"" | |
], | |
"metadata": { | |
"id": "hBIhTFNkFIGL" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"First we install the d8n package from pypi. This includes a library to communicate with the API." | |
], | |
"metadata": { | |
"id": "y6HPRzLIFvp8" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [], | |
"metadata": { | |
"id": "WOdeS8Wdxnk8" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install d8n\n", | |
"\n", | |
"import requests\n", | |
"from google.colab import userdata\n", | |
"from d8n.d8nClient import d8nClient\n", | |
"\n", | |
"api_key = userdata.get('api_key')\n", | |
"client = d8nClient(api_key)\n", | |
"\n", | |
"url = \"https://media.licdn.com/dms/image/D5612AQHlEzNOh4bIbg/article-cover_image-shrink_720_1280/0/1673730667692?e=2147483647&v=beta&t=dUWIM4P6t15lohxqYepJe3-wfFH31PuIkznVUo36Y8U\"\n", | |
"img_data = requests.get(url).content\n", | |
"\n", | |
"with open('image_name.jpg', 'wb') as handler:\n", | |
" handler.write(img_data)" | |
], | |
"metadata": { | |
"id": "1xz9_lKFTYu1", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "dc8d9dec-f180-414f-9f48-6244557bad0e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Collecting d8n\n", | |
" Downloading d8n-0.2.6-py3-none-any.whl (2.2 kB)\n", | |
"Installing collected packages: d8n\n", | |
"Successfully installed d8n-0.2.6\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Code above will fetch a random image from the internet.\n", | |
"\n", | |
"You can see how the image looks like by opening the file image_name.jpg" | |
], | |
"metadata": { | |
"id": "zHkQUlyeF8pF" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Upload the image to the API and run analysis on it\n", | |
"jsonid = client.from_local_file('/content/image_name.jpg')\n", | |
"id = jsonid['id']\n", | |
"# id is now a reference to the ID of your request\n", | |
"display(id)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 37 | |
}, | |
"id": "9pBEDR9HOucD", | |
"outputId": "e928cb9c-28be-43b3-fe8e-8d287ebb788c" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"'ca8fa272-408a-4d61-99d0-dc13b2f6a845'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"status = client.fetch_status(id)\n", | |
"\n", | |
"print(\"Current status:\")\n", | |
"display(status[\"status\"])\n", | |
"\n", | |
"print(\"Finished tasks:\")\n", | |
"display(status[\"finished_tasks\"])" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 92 | |
}, | |
"id": "BMoeXBJxPWaZ", | |
"outputId": "5725e545-f59a-42ad-83a4-5f979f0e7c22" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Current status:\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"'Status.Line_Detection'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Finished tasks:\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"'Status(Started|Symbol_Detection|Text_Extraction)'" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"**Note**: wait until 'finished_tasks' is Completed. (shouln't take more than a minute)\n", | |
"\n", | |
"\n", | |
"This will download the collected info into YOLO format which you can input into d8n sandbox to view.\n" | |
], | |
"metadata": { | |
"id": "52HhJWu_S3iF" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Download a zip file containing\n", | |
"# 1. The image you uploaded\n", | |
"# 2. The text file of labels of the identified objects\n", | |
"# 3. A text file containing all the identified objects with their identified coordinates (normalized)\n", | |
"# and the class predicted\n", | |
"import zipfile\n", | |
"\n", | |
"zip_file = client.download_entry(id)\n", | |
"save_file = './file.zip'\n", | |
"\n", | |
"with zipfile.ZipFile(save_file, 'w') as zip:\n", | |
" zip.writestr('out.zip',zip_file)\n", | |
"\n", | |
"from google.colab import files\n", | |
"files.download(\"/content/file.zip\")" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 17 | |
}, | |
"id": "j2o6MLUcXGns", | |
"outputId": "153b5b70-12d9-4758-fbd0-6be242f604c0" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"\n", | |
" async function download(id, filename, size) {\n", | |
" if (!google.colab.kernel.accessAllowed) {\n", | |
" return;\n", | |
" }\n", | |
" const div = document.createElement('div');\n", | |
" const label = document.createElement('label');\n", | |
" label.textContent = `Downloading \"${filename}\": `;\n", | |
" div.appendChild(label);\n", | |
" const progress = document.createElement('progress');\n", | |
" progress.max = size;\n", | |
" div.appendChild(progress);\n", | |
" document.body.appendChild(div);\n", | |
"\n", | |
" const buffers = [];\n", | |
" let downloaded = 0;\n", | |
"\n", | |
" const channel = await google.colab.kernel.comms.open(id);\n", | |
" // Send a message to notify the kernel that we're ready.\n", | |
" channel.send({})\n", | |
"\n", | |
" for await (const message of channel.messages) {\n", | |
" // Send a message to notify the kernel that we're ready.\n", | |
" channel.send({})\n", | |
" if (message.buffers) {\n", | |
" for (const buffer of message.buffers) {\n", | |
" buffers.push(buffer);\n", | |
" downloaded += buffer.byteLength;\n", | |
" progress.value = downloaded;\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
" const blob = new Blob(buffers, {type: 'application/binary'});\n", | |
" const a = document.createElement('a');\n", | |
" a.href = window.URL.createObjectURL(blob);\n", | |
" a.download = filename;\n", | |
" div.appendChild(a);\n", | |
" a.click();\n", | |
" div.remove();\n", | |
" }\n", | |
" " | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"download(\"download_712c5c96-76f6-49a3-890c-c013292e1629\", \"file.zip\", 390738)" | |
] | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"\n", | |
"To do this unzip the downloaded file and then input the image into\n", | |
"\n", | |
"Sandbox url: https://sandbox.d8n.host/\n", | |
"\n", | |
"After that click on\n", | |
"1. Object Detection\n", | |
"2. Multiple- files in YOLO format.\n", | |
"3. Select both the .txt file and the labels.txt file in the extracted folder" | |
], | |
"metadata": { | |
"id": "VItWn3o1TyFT" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment