
    i=                     v    d dl Z d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d Zd ZeeddfdZdd
Zd Zd ZdS )    N)DEFAULT_ARUCO_DICTDEFAULT_ARUCO_IDS)PipelineInputError)
dump_imagec                 h    t          j        |d         | d         z
  |d         | d         z
            S )Nr      )mathhypot)abs     "/code/app/lib/handwriting/aruco.py_distr      s,    :adQqTk1Q4!A$;///    c                     |                      d          sd|  } t          t          j        |           st	          d|            t          j                            t          t          j        |                     S )NDICT_zUnknown ArUco dictionary: )
startswithhasattrcv2aruco
ValueErrorgetPredefinedDictionarygetattr)	dict_names    r   get_aruco_dictionaryr      ss    (( ('I''	39i(( CAiAABBB9,,WSY	-J-JKKKr   Fc           
         t          j        | t           j                  }|j        \  }}t	          |          }t          t           j        d          rXt           j                                        }	t           j                            ||	          }
|
	                    |          \  }}}nDt           j        
                                }	t           j        	                    |||	          \  }}}|t          |          dk    rt          d          |                                                                }|d         d|d         d|d         d	|d
         di}d d d d d}t          ||          D ]\  }}|d         }t!          t#          j        |d d df                             }t!          t#          j        |d d df                             }||||fd}||v r||||         <   {||dz  k     r||dz  k     rd}n,||dz  k    r||dz  k     rd}n||dz  k    r||dz  k    rd	}nd}|||<   t'          d |                                D                       rt          d          |s|rt          j        |t           j                  }t           j                            ||t#          j        |                     |                                D ]b\  }}|d         \  }}t          j        ||                                t7          |          t7          |          ft           j        ddd           ct;          |pdd|           |d         |d         |d	         |d         g}|S )NArucoDetector)
parametersr   z\Could not detect the markers. Make sure all four markers are visible and the photo is clear.tlr   tr   br   bl)r   r   r!   r#   )idcornerscenterc              3      K   | ]}|d u V  	d S )N ).0vs     r   	<genexpr>z%find_aruco_markers.<locals>.<genexpr>L   s&      
1
119
1
1
1
1
1
1r   JCould not detect all four markers. Make sure the full template is visible.r&   gffffff?)r   r      .za4_aruco_markers.jpg)r   cvtColorCOLOR_BGR2GRAYshaper   r   r   DetectorParametersr   detectMarkersDetectorParameters_createlenr   flattentolistzipfloatnpmeananyvaluesCOLOR_GRAY2BGRdrawDetectedMarkersarrayitemsputTextupperintFONT_HERSHEY_SIMPLEXr   )imager   expected_idsverboseout_dirgrayhw
dictionaryparamsdetectorr%   ids_id_to_corner	quadrantsmarker_corners	marker_idptscxcyentryquaddebugnameinfoordereds                              r   find_aruco_markersr_      s    <s122D:DAq%i00Jsy/** W--//9**:v>>"0066aa4466)11$
v1VVa
{c#hh!mm &
 
 	

 ++--


 
 CQQQQ	L 4t4@@I%(#%6%6 $ $!	Q273qqq!t9%%&&273qqq!t9%%&& SRHEE$$16Il9-..AEzzb1q5jjq1ua!eq1uq1u#IdOO

1
1i..00
1
1
111 
 X
 
 	
  B' BT3#566	%%eWbhsmmDDD#//++ 
	 
	JD$(^FBK

R#b''"(    	7>c#95AAA	$4)D/RGNr   Q?c                    d |D             }d |D             \  }}}}t          j        ||||gd          }t          ||          }	t          ||          }
t          t	          |	|
                    }t          ||          }t          ||          }t          t	          ||                    }t          ||z            }t          ||z            }||dz  z   }||dz  z   }t          j        ||g||z   dz
  |g||z   dz
  ||z   dz
  g|||z   dz
  ggd          }t          j        ||          }t          j        | |||f          S )Nc                     g | ]
}|d          S )r&   r(   )r)   ms     r   
<listcomp>z2warp_perspective_using_markers.<locals>.<listcomp>f   s    
(
(
(11X;
(
(
(r   c                 R    g | ]$}t          t          t          |                    %S r(   )tuplemapr9   )r)   ps     r   rd   z2warp_perspective_using_markers.<locals>.<listcomp>g   s(    888qeCqMM**888r   float32)dtyper    r   )r:   r@   r   rD   maxr   getPerspectiveTransformwarpPerspective)gray_imgmarkerspadding_ratiorV   r   r   r!   r#   srcwidth_awidth_b	max_widthheight_aheight_b
max_heightpad_xpad_yout_wout_hdstMs                        r   warp_perspective_using_markersr~   e   s   
(
(
(
(
(C88C888NBB
(BB#9
5
5
5CBmmGBmmGC))**IR}}HR}}HS8,,--J	M)**E
]*++E	!E"E
(ENY"E*Y"EJ$6$:;EJ&*+		
   C 	#C--AxUEN;;;r   c                 
   t          t          j        | t          j                  t          t
          d|          }|rt          |          dk    rt          d          t          | |d          }t          |d|           |S )NF)r   rG   rH   rI      r,   r`   rp   za5_rectified_page.jpg)
r_   r   r/   r>   r   r   r5   r   r~   r   )rn   rI   ro   warpeds       r   rectify_pager      s     Xs122$&  G  
c'lla'' X
 
 	
 ,HgTRRRFw/888Mr   c                     t          | t          t          d          }|rt          |          dk    rt	          d          t          | |d          }|S )NF)r   rG   rH   r   r,   r`   r   )r_   r   r   r5   r   r~   )	color_imgrI   ro   r   s       r   rectify_page_colorr      sl     $&	  G  
c'lla'' X
 
 	
 ,IwdSSSFMr   )r`   )r	   r   numpyr:   app.lib.handwriting.configr   r   app.lib.handwriting.errorsr   app.lib.handwriting.io_utilsr   r   r   r_   r~   r   r   r(   r   r   <module>r      s     



     L L L L L L L L 9 9 9 9 9 9 3 3 3 3 3 30 0 0L L L !"K K K K\< < < <@  $    r   