22import json
33import time
44from pathlib import Path
5- from typing import Optional
65
76import requests
87
98from darwin .utils import is_image_extension_allowed
109
1110
1211def download_all_images_from_annotations (
12+ api_key : str ,
1313 api_url : str ,
1414 annotations_path : Path ,
1515 images_path : Path ,
@@ -21,6 +21,8 @@ def download_all_images_from_annotations(
2121
2222 Parameters
2323 ----------
24+ api_key : str
25+ API Key of the current team
2426 api_url : str
2527 Url of the darwin API (e.g. 'https://darwin.v7labs.com/api/')
2628 annotations_path : Path
@@ -73,17 +75,23 @@ def download_all_images_from_annotations(
7375 # Create the generator with the partial functions
7476 count = len (annotations_to_download_path )
7577 generator = lambda : (
76- functools .partial (download_image_from_annotation , api_url , annotation_path , images_path , annotation_format )
78+ functools .partial (
79+ download_image_from_annotation , api_key , api_url , annotation_path , images_path , annotation_format
80+ )
7781 for annotation_path in annotations_to_download_path
7882 )
7983 return generator , count
8084
8185
82- def download_image_from_annotation (api_url : str , annotation_path : Path , images_path : str , annotation_format : str ):
86+ def download_image_from_annotation (
87+ api_key : str , api_url : str , annotation_path : Path , images_path : str , annotation_format : str
88+ ):
8389 """Helper function: dispatcher of functions to download an image given an annotation
8490
8591 Parameters
8692 ----------
93+ api_key : str
94+ API Key of the current team
8795 api_url : str
8896 Url of the darwin API (e.g. 'https://darwin.v7labs.com/api/')
8997 annotation_path : Path
@@ -94,20 +102,22 @@ def download_image_from_annotation(api_url: str, annotation_path: Path, images_p
94102 Format of the annotations. Currently only JSON is supported
95103 """
96104 if annotation_format == "json" :
97- download_image_from_json_annotation (api_url , annotation_path , images_path )
105+ download_image_from_json_annotation (api_key , api_url , annotation_path , images_path )
98106 elif annotation_format == "xml" :
99107 print ("sorry can't let you do that dave" )
100108 raise NotImplementedError
101109 # download_image_from_xml_annotation(annotation_path, images_path)
102110
103111
104- def download_image_from_json_annotation (api_url : str , annotation_path : Path , image_path : str ):
112+ def download_image_from_json_annotation (api_key : str , api_url : str , annotation_path : Path , image_path : str ):
105113 """
106114 Helper function: downloads an image given a .json annotation path
107115 and renames the json after the image filename
108116
109117 Parameters
110118 ----------
119+ api_key : str
120+ API Key of the current team
111121 api_url : str
112122 Url of the darwin API (e.g. 'https://darwin.v7labs.com/api/')
113123 annotation_path : Path
@@ -122,10 +132,10 @@ def download_image_from_json_annotation(api_url: str, annotation_path: Path, ima
122132 original_filename_suffix = Path (annotation ["image" ]["original_filename" ]).suffix
123133 path = Path (image_path ) / (annotation_path .stem + original_filename_suffix )
124134
125- download_image (annotation ["image" ]["url" ], path )
135+ download_image (annotation ["image" ]["url" ], path , api_key )
126136
127137
128- def download_image (url : str , path : Path , verbose : Optional [ bool ] = False ):
138+ def download_image (url : str , path : Path , api_key : str ):
129139 """Helper function: downloads one image from url.
130140
131141 Parameters
@@ -134,17 +144,18 @@ def download_image(url: str, path: Path, verbose: Optional[bool] = False):
134144 Url of the image to download
135145 path : Path
136146 Path where to download the image, with filename
137- verbose : bool
138- Flag for the logging level
147+ api_key : str
148+ API Key of the current team
139149 """
140150 if path .exists ():
141151 return
142- if verbose :
143- print (f"Dowloading { path .name } " )
144152 TIMEOUT = 60
145153 start = time .time ()
146154 while True :
147- response = requests .get (url , stream = True )
155+ if "token" in url :
156+ response = requests .get (url , stream = True )
157+ else :
158+ response = requests .get (url , headers = {"Authorization" : f"ApiKey { api_key } " }, stream = True )
148159 # Correct status: download image
149160 if response .status_code == 200 :
150161 with open (str (path ), "wb" ) as file :
0 commit comments