Skip to content

Commit

Permalink
add labelmaps to save annotation calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylampada committed Nov 24, 2023
1 parent af12610 commit c3fe42e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
.gptexecthread

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
7 changes: 4 additions & 3 deletions roboflow/adapters/rfapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import urllib

import json
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

Expand Down Expand Up @@ -83,6 +83,7 @@ def save_annotation(
annotation_string: str,
image_id: str,
is_prediction: bool = False,
annotation_labelmap=None,
):
"""
Upload an annotation to a specific project.
Expand All @@ -98,8 +99,8 @@ def save_annotation(

response = requests.post(
upload_url,
data=annotation_string,
headers={"Content-Type": "text/plain"},
data=json.dumps({"data": annotation_string, "labelmap": annotation_labelmap}),
headers={"Content-Type": "application/json"},
)
responsejson = None
try:
Expand Down
2 changes: 2 additions & 0 deletions roboflow/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ def single_upload(
self,
image_path=None,
annotation_path=None,
annotation_labelmap=None,
hosted_image=False,
image_id=None,
split="train",
Expand Down Expand Up @@ -506,6 +507,7 @@ def single_upload(
annotation_str,
image_id,
is_prediction=is_prediction,
annotation_labelmap=annotation_labelmap,
)
except BaseException as e:
uploaded_annotation = {"error": e}
Expand Down
6 changes: 5 additions & 1 deletion roboflow/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,13 @@ def _upload_image(imagedesc):
annotationdesc = imagedesc.get("annotationfile")
if annotationdesc:
annotation_path = f"{location}{annotationdesc['file']}"
labelmap = annotationdesc.get("labelmap")
try:
uploadres = project.single_upload(
image_path=image_path, annotation_path=annotation_path, split=split
image_path=image_path,
annotation_path=annotation_path,
annotation_labelmap=labelmap,
split=split,
)
_log_img_upload(image_path, uploadres)
except Exception as e:
Expand Down
19 changes: 18 additions & 1 deletion roboflow/util/folderparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def parsefolder(folder):
_decide_split(images)
annotations = [f for f in files if f["extension"] in ANNOTATION_EXTENSIONS]
labelmaps = [f for f in files if f["extension"] in LABELMAPS_EXTENSIONS]
labelmaps = _load_labelmaps(folder, labelmaps)
_map_labelmaps_to_annotations(annotations, labelmaps)
_map_annotations_to_images(images, annotations)
return {
Expand Down Expand Up @@ -72,7 +73,23 @@ def _map_labelmaps_to_annotations(annotations, labelmaps):
for ann in annotations:
labelmap = labelmapmap.get(ann["dirname"])
if labelmap:
ann["labelmapfile"] = labelmap
ann["labelmap"] = labelmap["labelmap"]


def _load_labelmaps(folder, labelmaps):
for labelmap in labelmaps:
try:
labelmap["labelmap"] = _load_labelmap(f"{folder}/{labelmap['file']}")
except:
# raise
pass
return [lm for lm in labelmaps if lm.get("labelmap")]


def _load_labelmap(f):
with open(f, "r") as file:
lines = [line for line in file.readlines() if line.strip()]
return {i: l.strip() for i, l in enumerate(lines)}


def _decide_split(images):
Expand Down
6 changes: 5 additions & 1 deletion tests/manual/debugme.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import sys
import os

# import requests

# requests.urllib3.disable_warnings()

thisdir = os.path.dirname(os.path.abspath(__file__))
rootdir = os.path.abspath(f"{thisdir}/../..")
os.environ["ROBOFLOW_CONFIG_DIR"] = f"{thisdir}/data/.config"
Expand All @@ -14,7 +18,7 @@
# args = parser.parse_args(f"upload {thisdir}/../datasets/chess -w wolfodorpythontests -p chess -f auto".split())
args = parser.parse_args(
# f"upload {thisdir}/data/cultura-pepino-voc -w wolfodorpythontests -p cultura-pepino-voc -f auto -c 50".split()
f"upload {thisdir}/data/cultura-pepino-darknet -w wolfodorpythontests -p cultura-pepino-darknet -f auto -c 50".split()
f"upload {thisdir}/data/cultura-pepino-darknet -w wolfodorpythontests -p cultura-pepino-darknet -f auto -c 100".split()
# f"upload {thisdir}/data/0311fisheye -w wolfodorpythontests -p 0311fisheye -f auto -c 50".split()
)
args.func(args)
1 change: 1 addition & 0 deletions tests/manual/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export ROBOFLOW_CONFIG_DIR=./data/.config
# python ../../roboflowpy.py upload ./data/cultura-pepino-yolov8 -w wolfodorpythontests -p cultura-pepino-upload-test-yolov8 -f yolov8
python ../../roboflowpy.py upload ./data/yellow -w wolfodorpythontests -p yellow-auto -f darknet
# python ../../roboflowpy.py upload ./data/cultura-pepino-yolov5pytorch -w wolfodorpythontests -p cultura-pepino-upload-test-yolov5 -f yolov5
# python ../../roboflowpy.py upload ./data/0311fisheye -w wolfodorpythontests -p 0311fisheye -f auto
3 changes: 3 additions & 0 deletions tests/manual/uselocal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
export API_URL=https://localhost.roboflow.one
export APP_URL=https://localhost.roboflow.one

0 comments on commit c3fe42e

Please sign in to comment.