@@ -25,8 +25,11 @@ def custom_formatwarning(msg, *args, **kwargs):
2525
2626warnings .formatwarning = custom_formatwarning
2727
28+
2829class UploadError (Exception ):
2930 pass
31+
32+
3033class Project :
3134 def __init__ (self , api_key , a_project , model_format = None ):
3235 if api_key in DEMO_KEYS :
@@ -357,11 +360,15 @@ def __image_upload(
357360 raise UploadError (f"Server rejected image: { responsejson } " )
358361 return responsejson .get ("id" )
359362 else :
360- warnings .warn (f"upload image { image_path } 200 OK, weird response: { response } " )
363+ warnings .warn (
364+ f"upload image { image_path } 200 OK, weird response: { response } "
365+ )
361366 return None
362367 else :
363368 if responsejson :
364- raise UploadError (f"Bad response: { response .status_code } - { responsejson } " )
369+ raise UploadError (
370+ f"Bad response: { response .status_code } - { responsejson } "
371+ )
365372 else :
366373 raise UploadError (f"Bad response: { response } " )
367374
@@ -423,21 +430,35 @@ def __annotation_upload(
423430 if response .status_code == 200 :
424431 if responsejson :
425432 if responsejson .get ("error" ):
426- raise UploadError (f"Failed to save annotation for { image_id } : { responsejson ['error' ]} " )
433+ raise UploadError (
434+ f"Failed to save annotation for { image_id } : { responsejson ['error' ]} "
435+ )
427436 elif not responsejson .get ("success" ):
428- raise UploadError (f"Failed to save annotation for { image_id } : { responsejson } " )
437+ raise UploadError (
438+ f"Failed to save annotation for { image_id } : { responsejson } "
439+ )
429440 else :
430- warnings .warn (f"save annotation { annotation_path } 200 OK, weird response: { response } " )
431- elif response .status_code == 409 and "already annotated" in (responsejson or {}).get ("error" , {}).get ("message" ):
441+ warnings .warn (
442+ f"save annotation { annotation_path } 200 OK, weird response: { response } "
443+ )
444+ elif response .status_code == 409 and "already annotated" in (
445+ responsejson or {}
446+ ).get ("error" , {}).get ("message" ):
432447 print (f"image already annotated: { annotation_path } " )
433448 else :
434449 if responsejson :
435- if responsejson .get ('error' ):
436- raise UploadError (f"save annotation for { image_id } / bad response: { response .status_code } - { responsejson ['error' ]} " )
450+ if responsejson .get ("error" ):
451+ raise UploadError (
452+ f"save annotation for { image_id } / bad response: { response .status_code } - { responsejson ['error' ]} "
453+ )
437454 else :
438- raise UploadError (f"save annotation for { image_id } / bad response: { response .status_code } - { responsejson } " )
455+ raise UploadError (
456+ f"save annotation for { image_id } / bad response: { response .status_code } - { responsejson } "
457+ )
439458 else :
440- raise UploadError (f"save annotation for { image_id } bad response: { response } " )
459+ raise UploadError (
460+ f"save annotation for { image_id } bad response: { response } "
461+ )
441462
442463 def check_valid_image (self , image_path ):
443464 try :
@@ -555,7 +576,10 @@ def single_upload(
555576 annotation_success = False
556577 if image_path is not None :
557578 try :
558- image_id = retry (num_retry_uploads , Exception , self .__image_upload ,
579+ image_id = retry (
580+ num_retry_uploads ,
581+ Exception ,
582+ self .__image_upload ,
559583 image_path ,
560584 hosted_image = hosted_image ,
561585 split = split ,
@@ -565,17 +589,24 @@ def single_upload(
565589 )
566590 success = True
567591 except BaseException as e :
568- print (f'{ image_path } ERROR uploading image after { num_retry_uploads } retries: { e } ' , file = sys .stderr )
592+ print (
593+ f"{ image_path } ERROR uploading image after { num_retry_uploads } retries: { e } " ,
594+ file = sys .stderr ,
595+ )
569596 return
570597
571598 # Upload only annotations to image based on image Id (no image)
572599 if annotation_path is not None and image_id is not None and success :
573600 # Get annotation upload response
574601 try :
575- self .__annotation_upload (annotation_path , image_id , is_prediction = is_prediction )
602+ self .__annotation_upload (
603+ annotation_path , image_id , is_prediction = is_prediction
604+ )
576605 annotation_success = True
577606 except BaseException as e :
578- print (f'{ annotation_path } ERROR saving annotation: { e } ' , file = sys .stderr )
607+ print (
608+ f"{ annotation_path } ERROR saving annotation: { e } " , file = sys .stderr
609+ )
579610 return False
580611 # Give user warning that annotation failed to upload
581612 if not annotation_success :
0 commit comments