Skip to content

Commit 1fe0e8f

Browse files
bilalcodehubbilalcodehub
and
bilalcodehub
authored
Fix download_from_cvat function in cvat.py to handle job requests and download annotations (#1795)
Co-authored-by: bilalcodehub <[email protected]>
1 parent 1d5c56e commit 1fe0e8f

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

monailabel/datastore/cvat.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,55 @@ def download_from_cvat(self, max_retry_count=5, retry_wait_time=10):
189189
task_id, task_name = self.get_cvat_task_id(project_id, create=False)
190190
logger.info(f"Preparing to download/update final labels from: {project_id} => {task_id} => {task_name}")
191191

192-
download_url = f"{self.api_url}/api/tasks/{task_id}/annotations?action=download&format=Segmentation+mask+1.1"
192+
# Step 1: Initiate export process
193+
export_url = f"{self.api_url}/api/tasks/{task_id}/dataset/export?format=Segmentation+mask+1.1&location=local&save_images=false"
194+
try:
195+
response = requests.post(export_url, auth=self.auth)
196+
if response.status_code not in [200, 202]:
197+
logger.error(f"Failed to initiate export process: {response.status_code}, {response.text}")
198+
return None
199+
200+
rq_id = response.json().get("rq_id")
201+
if not rq_id:
202+
logger.error("Export process did not return a request ID (rq_id).")
203+
return None
204+
logger.info(f"Export process initiated successfully with request ID: {rq_id}")
205+
except Exception as e:
206+
logger.exception(f"Error while initiating export process: {e}")
207+
return None
208+
209+
# Step 2: Poll export status
210+
status_url = f"{self.api_url}/api/requests/{rq_id}"
211+
for _ in range(max_retry_count):
212+
try:
213+
status_response = requests.get(status_url, auth=self.auth)
214+
status = status_response.json().get("status")
215+
if status == "finished":
216+
logger.info("Export process completed successfully.")
217+
break
218+
elif status == "failed":
219+
logger.error(f"Export process failed: {status_response.json()}")
220+
return None
221+
logger.info(f"Export in progress... Retrying in {retry_wait_time} seconds.")
222+
time.sleep(retry_wait_time)
223+
except Exception as e:
224+
logger.exception(f"Error checking export status: {e}")
225+
time.sleep(retry_wait_time)
226+
else:
227+
logger.error("Export process did not complete within the maximum retries.")
228+
return None
229+
230+
# Step 3: Download the dataset
231+
download_url = f"{self.api_url}/api/tasks/{task_id}/annotations?format=Segmentation+mask+1.1&location=local&save_images=false&action=download"
193232
tmp_folder = tempfile.TemporaryDirectory().name
194233
os.makedirs(tmp_folder, exist_ok=True)
195234

196235
tmp_zip = tempfile.NamedTemporaryFile(suffix=".zip").name
197236
retry_count = 0
198237
for retry in range(max_retry_count):
199238
try:
239+
logger.info(f"Downloading exported dataset from: {download_url}")
200240
r = requests.get(download_url, allow_redirects=True, auth=self.auth)
201-
time.sleep(retry_wait_time)
202241

203242
with open(tmp_zip, "wb") as fp:
204243
fp.write(r.content)
@@ -227,7 +266,7 @@ def download_from_cvat(self, max_retry_count=5, retry_wait_time=10):
227266
Image.open(label).save(dest)
228267
logger.info(f"Copy Final Label: {label} to {dest}")
229268

230-
# Rename after consuming/downloading the labels
269+
# Rename task after consuming/downloading the labels
231270
patch_url = f"{self.api_url}/api/tasks/{task_id}"
232271
body = {"name": f"{self.done_prefix}_{task_name}"}
233272
requests.patch(patch_url, allow_redirects=True, auth=self.auth, json=body)

0 commit comments

Comments
 (0)