Skip to content

Instantly share code, notes, and snippets.

@narphorium
Created September 10, 2022 06:03
Show Gist options
  • Save narphorium/8838e53bd0fa4a84b3d7307768de4b06 to your computer and use it in GitHub Desktop.
Save narphorium/8838e53bd0fa4a84b3d7307768de4b06 to your computer and use it in GitHub Desktop.
Eagle AutoTagger
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Eagle AutoTagger\n",
"\n",
"**Note:** Eagle must be running for this to work."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Look up all the tags currently used in Eagle. You can add more tags in Eagle and then re-run this script."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"API_BASE = 'http://localhost:41595/api/'\n",
"\n",
"items = requests.get(API_BASE + 'item/list', {'limit': 1000000}).json()['data']\n",
"\n",
"lexicon = set()\n",
"for item in items:\n",
" lexicon = lexicon.union(set(item['tags']))\n",
"\n",
"for tag in sorted(lexicon):\n",
" print(tag)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Automatically tag all items in the currently open library by matching the tags to the description."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for item in items:\n",
"\n",
" # If the image doesn't have a description, use the filename as the description\n",
" if not item['annotation']:\n",
" item['annotation'] = item['name'].replace('__', ', ').replace('_', ' ')\n",
"\n",
" # Loop through tags and look for matches in the image description\n",
" existing_tags = set(item['tags'])\n",
" new_tags = set()\n",
" for tag in lexicon:\n",
" if tag.lower() in item['annotation'].lower() and not tag in existing_tags:\n",
" new_tags.add(tag)\n",
" if new_tags:\n",
" item['tags'].extend(list(new_tags))\n",
" print(f'Added tags to {item[\"name\"]}: {list(new_tags)}')\n",
"\n",
" # Update the image metadata in Eagle\n",
" requests.post(API_BASE + 'item/update', json=item)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('.venv': venv)",
"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.10.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "038360db81a4bb1efb8d9660744b0f996012034286ddd48d3bb13ae1ceda25d6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment