|
17 | 17 | add_annotation_polyline_to_json, add_annotation_template_to_json |
18 | 18 | ) |
19 | 19 | from ..api import API |
| 20 | +from ..common import process_api_response |
20 | 21 | from ..exceptions import SABaseException |
| 22 | +from ..parameter_decorators import project_metadata |
21 | 23 | from .annotation_classes import ( |
22 | 24 | fill_class_and_attribute_ids, fill_class_and_attribute_names, |
23 | 25 | get_annotation_classes_id_to_name, get_annotation_classes_name_to_id, |
24 | 26 | search_annotation_classes |
25 | 27 | ) |
26 | 28 | from .project_api import get_project_metadata_bare |
27 | | -from ..common import process_api_response |
28 | | -from ..parameter_decorators import project_metadata |
29 | 29 |
|
30 | 30 | logger = logging.getLogger("superannotate-python-sdk") |
31 | 31 |
|
@@ -632,7 +632,14 @@ def get_image_bytes(project, image_name, variant='original'): |
632 | 632 | res = response.json() |
633 | 633 | url = res[variant]["url"] |
634 | 634 | headers = res[variant]["headers"] |
| 635 | + print(params) |
| 636 | + print(url) |
| 637 | + print(headers) |
635 | 638 | response = requests.get(url=url, headers=headers) |
| 639 | + if not response.ok: |
| 640 | + raise SABaseException( |
| 641 | + response.status_code, "Couldn't download image" + response.text |
| 642 | + ) |
636 | 643 | img = io.BytesIO(response.content) |
637 | 644 | return img |
638 | 645 |
|
@@ -762,61 +769,50 @@ def get_image_annotations(project, image_name, project_type=None): |
762 | 769 | params=params |
763 | 770 | ) |
764 | 771 | if not response.ok: |
765 | | - raise SABaseException(response.status_code, response.text) |
| 772 | + raise SABaseException( |
| 773 | + response.status_code, |
| 774 | + "Couldn't get annotation download token" + response.text |
| 775 | + ) |
766 | 776 | res = response.json() |
| 777 | + # print(json.dumps(res, indent=2)) |
767 | 778 |
|
768 | 779 | annotation_classes = search_annotation_classes(project) |
769 | 780 | annotation_classes_dict = get_annotation_classes_id_to_name( |
770 | 781 | annotation_classes |
771 | 782 | ) |
772 | | - if project_type == "Vector": |
773 | | - url = res["objects"]["url"] |
774 | | - annotation_json_filename = url.rsplit('/', 1)[-1] |
775 | | - headers = res["objects"]["headers"] |
776 | | - response = requests.get(url=url, headers=headers) |
777 | | - if response.ok: |
778 | | - res_json = response.json() |
779 | | - fill_class_and_attribute_names(res_json, annotation_classes_dict) |
780 | | - return { |
781 | | - "annotation_json_filename": annotation_json_filename, |
782 | | - "annotation_json": res_json |
783 | | - } |
784 | | - if not response.ok and response.status_code == 403: |
785 | | - return {"annotation_json": None, "annotation_json_filename": None} |
786 | | - raise SABaseException(response.status_code, response.text) |
787 | | - else: # pixel |
788 | | - url = res["pixelObjects"]["url"] |
789 | | - annotation_json_filename = url.rsplit('/', 1)[-1] |
790 | | - headers = res["pixelObjects"]["headers"] |
791 | | - response = requests.get(url=url, headers=headers) |
792 | | - if not response.ok and response.status_code == 403: |
793 | | - return { |
794 | | - "annotation_json": None, |
795 | | - "annotation_json_filename": None, |
796 | | - "annotation_mask": None, |
797 | | - "annotation_mask_filename": None |
798 | | - } |
799 | | - elif not response.ok: |
800 | | - raise SABaseException(response.status_code, response.text) |
801 | | - res_json = response.json() |
802 | | - fill_class_and_attribute_names(res_json, annotation_classes_dict) |
803 | | - if len(res_json["instances"]) != 0: |
804 | | - url = res["pixelSave"]["url"] |
805 | | - annotation_mask_filename = url.rsplit('/', 1)[-1] |
806 | | - headers = res["pixelSave"]["headers"] |
807 | | - response = requests.get(url=url, headers=headers) |
808 | | - if not response.ok: |
809 | | - raise SABaseException(response.status_code, response.text) |
810 | | - mask = io.BytesIO(response.content) |
811 | | - else: |
812 | | - mask = None |
813 | | - annotation_mask_filename = None |
814 | | - return { |
815 | | - "annotation_json": res_json, |
816 | | - "annotation_json_filename": annotation_json_filename, |
817 | | - "annotation_mask": mask, |
818 | | - "annotation_mask_filename": annotation_mask_filename |
819 | | - } |
| 783 | + main_annotations = res["annotations"]["MAIN"][0] |
| 784 | + response = requests.get( |
| 785 | + url=main_annotations["annotation_json_path"]["url"], |
| 786 | + headers=main_annotations["annotation_json_path"]["headers"] |
| 787 | + ) |
| 788 | + if not response.ok: |
| 789 | + raise SABaseException( |
| 790 | + response.status_code, "Couldn't load annotations" + response.text |
| 791 | + ) |
| 792 | + res_json = response.json() |
| 793 | + fill_class_and_attribute_names(res_json, annotation_classes_dict) |
| 794 | + result = { |
| 795 | + "annotation_json": |
| 796 | + response.json(), |
| 797 | + "annotation_json_filename": |
| 798 | + common.get_annotation_json_name(image_name, project_type) |
| 799 | + } |
| 800 | + if project_type == "Pixel": |
| 801 | + response = requests.get( |
| 802 | + url=main_annotations["annotation_bluemap_path"]["url"], |
| 803 | + headers=main_annotations["annotation_bluemape_path"]["headers"] |
| 804 | + ) |
| 805 | + if not response.ok: |
| 806 | + raise SABaseException( |
| 807 | + response.status_code, |
| 808 | + "Couldn't load annotations" + response.text |
| 809 | + ) |
| 810 | + mask = io.BytesIO(response.content) |
| 811 | + result["annotation_mask"] = mask |
| 812 | + result["annotation_mask_filename"] = common.get_annotation_png_name( |
| 813 | + image_name |
| 814 | + ) |
| 815 | + return result |
820 | 816 |
|
821 | 817 |
|
822 | 818 | def download_image_annotations(project, image_name, local_dir_path): |
@@ -956,17 +952,16 @@ def upload_image_annotations( |
956 | 952 | response.status_code, "Couldn't upload annotation. " + response.text |
957 | 953 | ) |
958 | 954 | res = response.json() |
959 | | - if project_type == "Vector": |
960 | | - res = res['objects'] |
961 | | - s3_session = boto3.Session( |
962 | | - aws_access_key_id=res['accessKeyId'], |
963 | | - aws_secret_access_key=res['secretAccessKey'], |
964 | | - aws_session_token=res['sessionToken'] |
965 | | - ) |
966 | | - s3_resource = s3_session.resource('s3') |
967 | | - bucket = s3_resource.Bucket(res["bucket"]) |
968 | | - bucket.put_object(Key=res['filePath'], Body=json.dumps(annotation_json)) |
969 | | - else: # pixel |
| 955 | + res = res['annotation_json_path'] |
| 956 | + s3_session = boto3.Session( |
| 957 | + aws_access_key_id=res['accessKeyId'], |
| 958 | + aws_secret_access_key=res['secretAccessKey'], |
| 959 | + aws_session_token=res['sessionToken'] |
| 960 | + ) |
| 961 | + s3_resource = s3_session.resource('s3') |
| 962 | + bucket = s3_resource.Bucket(res["bucket"]) |
| 963 | + bucket.put_object(Key=res['filePath'], Body=json.dumps(annotation_json)) |
| 964 | + if project_type == "Pixel": |
970 | 965 | if mask is None: |
971 | 966 | raise SABaseException(0, "Pixel annotation should have mask.") |
972 | 967 | if not isinstance(mask, io.BytesIO): |
|
0 commit comments