9
9
from PIL import Image
10
10
11
11
from roboflow .adapters import rfapi
12
- from roboflow .adapters .rfapi import RoboflowError
12
+ from roboflow .adapters .rfapi import RoboflowError , ImageUploadError , AnnotationSaveError
13
13
from roboflow .config import API_URL , CLIP_FEATURIZE_URL , DEMO_KEYS
14
- from roboflow .core .exceptions import UploadAnnotationError , UploadImageError
15
14
from roboflow .core .project import Project
16
15
from roboflow .util import folderparser
17
16
from roboflow .util .active_learning_utils import check_box_size , clip_encode , count_comparisons
17
+ from roboflow .util .image_utils import load_labelmap
18
18
from roboflow .util .two_stage_utils import ocr_infer
19
19
20
20
@@ -309,19 +309,22 @@ def upload_dataset(
309
309
310
310
location = parsed_dataset ["location" ]
311
311
312
- def _log_img_upload (image_path , uploadres ):
313
- image_id = uploadres .get ("image" , {}).get ("id" )
314
- img_success = uploadres .get ("image" , {}).get ("success" )
315
- img_duplicate = uploadres .get ("image" , {}).get ("duplicate" )
316
- annotation = uploadres .get ("annotation" )
317
- upload_time_str = f"[{ uploadres ['upload_time' ]:.1f} s]" if uploadres .get ("upload_time" ) else ""
318
- annotation_time_str = f"[{ uploadres ['annotation_time' ]:.1f} s]" if uploadres .get ("annotation_time" ) else ""
319
- retry_attempts = (
320
- f" (with { uploadres ['upload_retry_attempts' ]} retries)"
321
- if uploadres .get ("upload_retry_attempts" , 0 ) > 0
322
- else ""
323
- )
324
- # TODO: Will this duplicate case still occurs?
312
+ def _log_img_upload (
313
+ image_path ,
314
+ image ,
315
+ annotation ,
316
+ image_upload_time ,
317
+ image_upload_retry_attempts ,
318
+ annotation_time
319
+ ):
320
+ image_id = image .get ("id" )
321
+ img_success = image .get ("success" )
322
+ img_duplicate = image .get ("duplicate" )
323
+
324
+ upload_time_str = f"[{ image_upload_time :.1f} s]"
325
+ annotation_time_str = f"[{ annotation_time :.1f} s]"
326
+ retry_attempts = f" (with { image_upload_retry_attempts } retries)" if image_upload_retry_attempts > 0 else ""
327
+
325
328
if img_duplicate :
326
329
msg = f"[DUPLICATE]{ retry_attempts } { image_path } ({ image_id } ) { upload_time_str } "
327
330
elif img_success :
@@ -338,53 +341,73 @@ def _log_img_upload(image_path, uploadres):
338
341
339
342
print (msg )
340
343
341
- def _log_img_upload_err (image_path , e ):
342
- if isinstance (e , UploadImageError ):
343
- retry_attempts = f" (with { e .retry_attempts } retries)" if e .retry_attempts > 0 else ""
344
- print (f"[ERR]{ retry_attempts } { image_path } ({ e .message } )" )
345
- return
346
-
347
- if isinstance (e , UploadAnnotationError ):
348
- upload_time_str = f"[{ e .image_upload_time :.1f} s]" if e .image_upload_time else ""
349
- retry_attempts = f" (with { e .image_retry_attempts } retries)" if e .image_retry_attempts > 0 else ""
350
- image_msg = f"[UPLOADED]{ retry_attempts } { image_path } ({ e .image_id } ) { upload_time_str } "
351
- annotation_msg = f"annotations = ERR: { e .message } "
352
- print (f"{ image_msg } / { annotation_msg } " )
353
- return
354
-
355
- print (f"[ERR] { image_path } ({ e } )" )
356
-
357
344
def _upload_image (imagedesc ):
358
345
image_path = f"{ location } { imagedesc ['file' ]} "
359
346
split = imagedesc ["split" ]
360
- annotation_path = None
347
+
348
+ image , upload_time , upload_retry_attempts = project .upload_image (
349
+ image_path = image_path ,
350
+ split = split ,
351
+ batch_name = batch_name ,
352
+ sequence_number = imagedesc .get ("index" ),
353
+ sequence_size = len (images ),
354
+ )
355
+
356
+ return image , upload_time , upload_retry_attempts
357
+
358
+ def _save_annotation (image_id , imagedesc ):
361
359
labelmap = None
360
+ annotation_path = None
361
+
362
362
annotationdesc = imagedesc .get ("annotationfile" )
363
363
if annotationdesc :
364
364
if annotationdesc .get ("rawText" ):
365
365
annotation_path = annotationdesc
366
366
else :
367
367
annotation_path = f"{ location } { annotationdesc ['file' ]} "
368
368
labelmap = annotationdesc .get ("labelmap" )
369
+
370
+ if isinstance (labelmap , str ):
371
+ labelmap = load_labelmap (labelmap )
372
+
373
+ if not annotation_path :
374
+ return
375
+
376
+ annotation , upload_time = project .save_annotation (
377
+ annotation_path = annotation_path ,
378
+ annotation_labelmap = labelmap ,
379
+ image_id = image_id ,
380
+ job_name = batch_name ,
381
+ )
382
+
383
+ return annotation , upload_time
384
+
385
+ def _upload (imagedesc ):
386
+ image_path = f"{ location } { imagedesc ['file' ]} "
387
+
388
+ image_id = None
389
+ image_upload_time = None
390
+ image_retry_attempts = None
391
+
369
392
try :
370
- uploadres = project . single_upload (
371
- image_path = image_path ,
372
- annotation_path = annotation_path ,
373
- annotation_labelmap = labelmap ,
374
- split = split ,
375
- sequence_number = imagedesc . get ( "index" ),
376
- sequence_size = len ( images ),
377
- batch_name = batch_name ,
378
- num_retry_uploads = num_retries ,
379
- )
380
- _log_img_upload ( image_path , uploadres )
381
- except ( UploadImageError , UploadAnnotationError ) as e :
382
- _log_img_upload_err ( image_path , e )
393
+ image , image_upload_time , image_retry_attempts = _upload_image ( imagedesc )
394
+ image_id = image [ "id" ]
395
+ annotation , annotation_time = _save_annotation ( image_id , imagedesc )
396
+ _log_img_upload ( image_path , image , annotation , image_upload_time , image_retry_attempts , annotation_time )
397
+ except ImageUploadError as e :
398
+ retry_attempts = f" (with { e . retries } retries)" if e . retries > 0 else ""
399
+ print ( f"[ERR] { retry_attempts } { image_path } ( { e . message } )" )
400
+ except AnnotationSaveError as e :
401
+ upload_time_str = f"[ { image_upload_time :.1f } s]"
402
+ retry_attempts = f" (with { image_retry_attempts } retries)" if image_retry_attempts > 0 else ""
403
+ image_msg = f"[UPLOADED] { retry_attempts } { image_path } ( { image_id } ) { upload_time_str } "
404
+ annotation_msg = f"annotations = ERR: { e . message } "
405
+ print ( f" { image_msg } / { annotation_msg } " )
383
406
except Exception as e :
384
- _log_img_upload_err ( image_path , e )
407
+ print ( f"[ERR] { image_path } ( { e } )" )
385
408
386
409
with concurrent .futures .ThreadPoolExecutor (max_workers = num_workers ) as executor :
387
- list (executor .map (_upload_image , images ))
410
+ list (executor .map (_upload , images ))
388
411
389
412
def _get_or_create_project (self , project_id , license : str = "MIT" , type : str = "object-detection" ):
390
413
try :
0 commit comments