
    ]j	                         d Z ddlZddlZddlmZ d Z	 ddZedk(  r9ddlZ e	dej                  d   d        e	d	        eej                  d
          yy)ap  This code allows to export Label Studio Export JSON to FUNSD format.
It's only the basic converter, it converts every bbox as a separate word.
Check this github issue for more details:
https://github.com/heartexlabs/label-studio/issues/2634#issuecomment-1251648670

Usage: funsd.py export.json
This command will export your LS OCR annotations to "./funsd/" directory.
    N)defaultdictc           
      @   t        t              }| D ]  }||d      }|j                  di       j                  dd       }|r|d   |d<   |j                  di       j                  dd       }|r|d   |d<   d|vsh|d   |d	   }}|j                  d      }|d
   dz  |z  }	|d   dz  |z  }
|	|d   dz  |z  z   }|
|d   dz  |z  z   }|	||
|g|d<    g }d}|D ]A  }|dz  }|j                  |||   d   ||   d   ||   d   ||   d   ||   d   dgg d       C d|iS )Nidvaluelabelsr   labeltextboxoriginal_widthoriginal_heightxg      Y@ywidthheight   )r
   r	   )r   r
   r	   r   wordslinkingform)r   dictgetappend)resultpreitemor   r	   whvx1y1x2y2outputcounterkeys                   J/root/env/lib/python3.12/site-packages/label_studio_sdk/converter/funsd.pyconvert_annotation_to_fundr'      s   
d
C (T
O'2&**8T:AgJxx$((6QAfI>()40A+BqA!A3%!#B3%!#Baj5(1,,BakE)A--BBB'AeH%(* FG 
13xC(S'*"%c(5/3s8F;KLM		

 F    export.jsonc           
         t        |       5 }t        j                  |      }d d d        t        j                  |d       D ]  }|d   D ]v  }t        |d         }|d   |   }t        j                  j                  |      }| d|d    d|d    d	| d
}t        |d      5 }t        j                  ||       d d d        x  y # 1 sw Y   xY w# 1 sw Y   xY w)NT)exist_okannotationsr   dataz/task-r   z-annotation--z.jsonr   )	openjsonloadosmakedirsr'   pathbasenamedump)	ls_export_path	funsd_dirdata_keyftaskstask
annotationr#   filenames	            r&   ls_to_funsd_converterr?   9   s     
n	 		! KK	D) %}- 
	%J/
80DEFF|H-Hww''1H+VDJ<|Jt<L;MQ*E# 
 h$ %		&!$% %
	%% % %s   C#CCC__main__zUsage:zHThis command will export your LS OCR annotations to "./funsd/" directoryr   )r)   funsdocr)__doc__r2   r0   collectionsr   r'   r?   __name__sysprintargv r(   r&   <module>rJ      se    
  #'V ?D%, z	(CHHQK/	
TU#((1+& r(   