{ "cells": [ { "cell_type": "markdown", "id": "66f72396-0cf1-41cf-a486-2930d2ad1652", "metadata": {}, "source": [ "# area_map\n", "Give a string with the name of the image-file that was annotated with the Annotation Compass; Select a specific area of the image, return a list of all labels in that specific area." ] }, { "cell_type": "code", "execution_count": 1, "id": "1eb5f9a2-a86a-45f2-aa7b-9b32653f9fed", "metadata": {}, "outputs": [], "source": [ "from urllib.request import urlopen\n", "import json" ] }, { "cell_type": "code", "execution_count": 2, "id": "5fb4d7db-9d58-4921-81e9-9e5fd00c4d46", "metadata": {}, "outputs": [], "source": [ "def area_map(image: str, left: int, right: int, top: int, bottom: int ) -> list:\n", " \n", " \"\"\"Give a string with the name of the image-file that was annotated with the Annotation Compass; Select a specific area of the image, return a list of all labels in that specific area.\"\"\" \n", "\n", " url = f\"https://hub.xpub.nl/soupboat/generic-labels/get-labels/?image={image}\"\n", " response = urlopen(url)\n", " data_json = json.loads(response.read()) \n", " \n", " filtered_map = []\n", " for label in data_json['labels']:\n", " if left <= (label['position']['x']) <= right and top <= (label['position']['y']) <= bottom:\n", " filtered_map.append(label)\n", " \n", " return filtered_map" ] }, { "cell_type": "markdown", "id": "1094ed8e-1387-481e-b612-9a8cc81d5c18", "metadata": {}, "source": [ "This function was built for a project where individuals are invited to add their annotations on a map using the Annotation Compass. Each annotation-label is stored in a json-file and includes the annotation-text itself, but also the name of the image-file as well as the position, size, index, timestamp and userID of the annotation.\n", "\n", " Example for a label:\n", "\n", " {'image': 'map.jpg',\n", " 'position': {'x': 12, 'y': 97},\n", " 'size': {'width': 43, 'height': 18},\n", " 'text': 'This is a text! Is this a text?',\n", " 'timestamp': 'Wed, 01 Dec 2021 14:04:00 GMT',\n", " 'userID': 5766039063}\n", "\n", "\n", "If interested in the annotations in a specific area of the map, area_map() can help. The function needs a string with the name of the of the image-file that was annotated with the annotation compass tool as well as four specific values to define the left, right, top and bottom outlines of the selected area. The output is a list of all labels in the defined area of interest.\n", "\n", "How to get a json-file with annotation-labels?\n", "\n", " https://hub.xpub.nl/soupboat/generic-labels/\n", "The Annotation Compass allows people to uplaod an image and ask others to annotate it. A json-file of the annotations is provided." ] }, { "cell_type": "markdown", "id": "2c3e992a-317b-4433-8c3b-9efcedea575d", "metadata": { "tags": [] }, "source": [ "## Examples\n", "\n", "In this example, the function returns all annotation-labels in an area of interest that reaches from x = 70% to x = 90% and from y = 10% to y = 100%" ] }, { "cell_type": "code", "execution_count": 3, "id": "e3d7e97b-0089-4c29-a2c1-9ef823ff2e37", "metadata": { "scrolled": true, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[{'image': 'rejection_map.jpg',\n", " 'position': {'x': 76.25, 'y': 69.5246},\n", " 'size': {'height': 12.3939, 'width': 15.625},\n", " 'text': \"I once went to view a house here but it didn't have the floor. crap.\",\n", " 'timestamp': 'Wed, 15 Dec 2021 11:36:51 GMT',\n", " 'userID': '6286616941'},\n", " {'image': 'rejection_map.jpg',\n", " 'position': {'x': 77.0, 'y': 63.7521},\n", " 'size': {'height': 8.65874, 'width': 10.25},\n", " 'text': 'other housing rejection situations here.\\n',\n", " 'timestamp': 'Wed, 15 Dec 2021 11:37:25 GMT',\n", " 'userID': '6286616941'},\n", " {'image': 'rejection_map.jpg',\n", " 'position': {'x': 75.285, 'y': 29.2254},\n", " 'size': {'height': 19.0141, 'width': 17.513},\n", " 'text': '어딘지 잘은 모르겠지만, Kralingen 쪽이었던 것 같아. bsn 거주등록을 위해 학교에 갔는데, 시청에서 나온 사람들이 나의 룸메이트 ID card가 필요하다며 거절했지. 나의 아침을 날렸어. 나의 룸메가 나에게 ID card 사진을 보내줬지만, 그들은 서명이 같지 않다면서 다시 거절했지. ',\n", " 'timestamp': 'Wed, 15 Dec 2021 11:39:21 GMT',\n", " 'userID': '8633793842'},\n", " {'image': 'rejection_map.jpg',\n", " 'position': {'x': 73.703, 'y': 49.514},\n", " 'size': {'height': 5.22479, 'width': 5.09839},\n", " 'text': \"i got rejected from a skateboard here. it's normal though because it's always trying to reject you and you love it anyway\",\n", " 'timestamp': 'Wed, 15 Dec 2021 11:44:35 GMT',\n", " 'userID': '8918562766'}]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "area_map('rejection_map.jpg', 70, 90, 10, 100)" ] }, { "cell_type": "code", "execution_count": null, "id": "926c7209-4a91-4401-a1a5-15066b0331bc", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }