Files
TexTeller/texteller/models/det_model/__pycache__/preprocess.cpython-310.pyc

196 lines
14 KiB
Plaintext
Raw Normal View History

o
{<7B>g<EFBFBD>@<00>@s<>ddlZddlZddlZdd<03>ZGdd<05>de<05>ZGdd<07>de<05>ZGdd <09>d e<05>ZGd
d <0B>d e<05>Z Gd d <0A>d e<05>Z
Gdd<0F>de<05>Z dd<11>Z dd<13>Z  d dd<17>ZGdd<19>de<05>Zdd<1B>ZGdd<1D>de<05>ZGdd<1F>d<1F>ZdS)!<21>NcCs<>t|t<01>r&t|d<01><02> }|<01><03>}Wd<00>n1swYtj|dd<03>}n
t|tj<06>s.J<00>|}t<07>|d<04>}t<07> |tj
<EFBFBD>}tj |j dd<05>tj d<03>tj ddgtj d<03>d<07>}||fS)N<>rb<72>uint8<74><01>dtype<70><00><00><00>?)<02>im_shape<70> scale_factor)<0E>
isinstance<EFBFBD>str<74>open<65>read<61>np<6E>
frombuffer<EFBFBD>ndarray<61>cv2<76>imdecode<64>cvtColor<6F> COLOR_BGR2RGB<47>array<61>shape<70>float32)<06>img_path<74>fZim_read<61>data<74>imZimg_info<66>r<00>D/Users/Leehy/Code/TexTeller/texteller/models/det_model/preprocess.py<70> decode_images"
 
<1C> <04>
<04><06>rc@s0eZdZdZdejfdd<04>Zdd<06>Zdd<08>Zd S)
<EFBFBD>Resizez<65>resize image by target_size and max_size
Args:
target_size (int): the target size of image
keep_ratio (bool): whether keep_ratio or not, default true
interp (int): method of resize
TcCs(t|t<01>r ||g}||_||_||_dS<00>N)r <00>int<6E> target_size<7A>
keep_ratio<EFBFBD>interp)<04>selfr#r$r%rrr<00>__init__"s

zResize.__init__cCs<>t|j<01>dks J<00>|jddkr|jddksJ<00>|jd}|<00>|<01>\}}tj|dd|||jd<05>}t<07>|jdd<01><00><01> d<06>|d<t<07>||g<02><01> d<06>|d<||fS) <09><>
Args:
im (np.ndarray): image (np.ndarray)
im_info (dict): info of image
Returns:
im (np.ndarray): processed image (np.ndarray)
im_info (dict): info of processed image
rrrN)<03>fx<66>fy<66> interpolationrr r
)
<EFBFBD>lenr#r<00>generate_scaler<00>resizer%rr<00>astype)r&r<00>im_infoZ
im_channel<EFBFBD>
im_scale_y<EFBFBD>
im_scale_xrrr<00>__call__)s& 
<06><02><06>zResize.__call__c Cs<>|jdd<02>}|jd}|jrFt<02>|<02>}t<02>|<02>}t<02>|j<05>}t<02>|j<05>}t|<06>t|<04>}t<02>||<00>|kr>t|<07>t|<05>}|} |}
|
| fS|j\} } | t|d<00>}
| t|d<00>} |
| fS)z<>
Args:
im (np.ndarray): image (np.ndarray)
Returns:
im_scale_x: the resize ratio of X
im_scale_y: the resize ratio of Y
Nrrr)rr$r<00>min<69>maxr#<00>float<61>round) r&rZ origin_shape<70>im_cZ im_size_min<69> im_size_maxZtarget_size_minZtarget_size_max<61>im_scaler2r1<00>resize_h<5F>resize_wrrrr-Bs"


  
<EFBFBD>zResize.generate_scaleN) <09>__name__<5F>
__module__<EFBFBD> __qualname__<5F>__doc__r<00> INTER_LINEARr'r3r-rrrrr s
 r c@s"eZdZdZd dd<05>Zdd<07>ZdS)
<EFBFBD>NormalizeImagez<65>normalize image
Args:
mean (list): im - mean
std (list): im / std
is_scale (bool): whether need im / 255
norm_type (str): type in ['mean_std', 'none']
T<>mean_stdcCs||_||_||_||_dSr!)<04>mean<61>std<74>is_scale<6C> norm_type)r&rDrErFrGrrrr'fs
zNormalizeImage.__init__cCs<>|jtjdd<02>}|jrd}||9}|jdkr<t<01>|j<06>tjtjdd<05>f}t<01>|j<08>tjtjdd<05>f}||8}||}||fS)r(F)<01>copygp?rCN) r/rrrFrGrrD<00>newaxisrE)r&rr0<00>scalerDrErrrr3ls 
zNormalizeImage.__call__N)TrC<00>r=r>r?r@r'r3rrrrrB]s
 rBcs(eZdZdZ<03>fdd<03>Zdd<05>Z<05>ZS)<06>Permutez<65>permute image
Args:
to_bgr (bool): whether convert RGB to BGR
channel_first (bool): whether convert HWC to CHW
cstt|<00><02><02>dSr!)<03>superrLr')r&<00><01> __class__rrr'<00>szPermute.__init__cCs|<01>d<01><01><01>}||fS)r()rrr)<02> transposerH)r&rr0rrrr3<00>s zPermute.__call__)r=r>r?r@r'r3<00> __classcell__rrrNrrL<00>s rLc@<00>"eZdZdZddd<04>Zdd<06>ZdS) <09> PadStridez<65> padding image for model with FPN, instead PadBatch(pad_to_stride) in original config
Args:
stride (bool): model with FPN need image shape % stride == 0
rcCs
||_dSr!)<01>coarsest_stride)r&<00>striderrrr'<00>s
zPadStride.__init__c
Cs<>|j}|dkr ||fS|j\}}}tt<03>t|<05>|<00>|<00>}tt<03>t|<06>|<00>|<00>}tj|||ftjd<02>} || dd<03>d|<05>d|<06>f<| |fS)r(rrN)rTrr"r<00>ceilr6<00>zerosr)
r&rr0rTr8<00>im_h<5F>im_wZpad_hZpad_w<5F>
padding_imrrrr3<00>s  zPadStride.__call__N)rrKrrrrrS<00>s
 rScs.eZdZ<02>fdd<02>Zddd<05>Zdd<07>Z<05>ZS) <09>LetterBoxResizecs*tt|<00><02><02>t|t<04>r||g}||_dS)z<>
Resize image to target size, convert normalized xywh to pixel xyxy
format ([x_center, y_center, width, height] -> [x0, y0, x1, y1]).
Args:
target_size (int|list): image target size.
N)rMr[r'r r"r#)r&r#rNrrr'<00>s

zLetterBoxResize.__init__<5F><03><00>_@r]r]c Cs<>|jdd<01>}t|<02>|d}t|<03>|d}t||<07>}t|d|<00>t|d|<00>f} || dd}
|| dd} t| d<00>t| d<00>} } t|
d<00>t|
d<00>}}tj|| tjd<05>}tj|| | ||tj|d<06>}|||
| fS)Nrrr<><E79A99><EFBFBD><EFBFBD><EFBFBD>?)r+)<01>value) rr6r4r7rr.<00>
INTER_AREA<EFBFBD>copyMakeBorder<65>BORDER_CONSTANT)r&<00>img<6D>height<68>width<74>colorr<00>ratio_h<5F>ratio_w<5F>ratio<69> new_shape<70>padw<64>padh<64>top<6F>bottom<6F>left<66>rightrrr<00> letterbox<6F>s&
<04><06><06> zLetterBoxResize.letterboxc Cs<>t|j<01>dks J<00>|jddkr|jddksJ<00>|j\}}|jdd<01>\}}|j|||d<05>\}}}} t||<00>t||<00>g}
tj|
tjd<06>|d<tj||gtjd<06>|d<||fS) r(rrrN)rdrerr r
)r,r#rrqr7rrr) r&rr0rdre<00>h<>wrirkrlrjrrrr3<00>s 
zLetterBoxResize.__call__)r\)r=r>r?r'rqr3rQrrrNrr[<00>s 
r[cs,eZdZgd<01>f<01>fdd<03> Zdd<05>Z<04>ZS)<06>Pad)<03><00>\@rurucs0tt|<00><02><02>t|t<04>r||g}||_||_dS)z<>
Pad image to a specified size.
Args:
size (list[int]): image target size
fill_value (list[float]): rgb value of pad area, default (114.0, 114.0, 114.0)
N)rMrtr'r r"<00>size<7A>
fill_value)r&rvrwrNrrr'<00>s


z Pad.__init__cCs<>|jdd<01>\}}|j\}}||kr ||kr |<01>tj<04>}||fStj||dftjd<03>}|tj|jtjd<03>9}|<01>tj<04>|d|<03>d|<04>dd<00>f<|}||fS)Nr<00>rr)rrvr/rr<00>onesrrw)r&rr0rXrYrrrs<00>canvasrrrr3<00>s
 "z Pad.__call__)r=r>r?r'r3rQrrrNrrt<00>s rtcCsbt|<00>dksJ<00>t<01>|<01>t<01>|<01>}}|d||d|}|d||d|}||g}|S)z<>Rotate a point by an angle.
Args:
pt (list[float]): 2 dimensional point to be rotated
angle_rad (float): rotation angle by radian
Returns:
list[float]: Rotated point.
rrr)r,r<00>sin<69>cos)<07>ptZ angle_radZsn<73>cs<63>new_x<5F>new_yZ
rotated_ptrrr<00> rotate_points 
r<>cCsNt|<00>dksJ<00>t|<01>dksJ<00>||}|tj|d |dgtjd<04>}|S)a<>To calculate the affine matrix, three pairs of points are required. This
function is used to get the 3rd point, given 2D points a & b.
The 3rd point is defined by rotating vector `a - b` by 90 degrees
anticlockwise, using b as the rotation center.
Args:
a (np.ndarray): point(x,y)
b (np.ndarray): point(x,y)
Returns:
np.ndarray: The 3rd point.
rrrr)r,rrr)<04>a<>b<> directionZthird_ptrrr<00>_get_3rd_points
"r<><00><02>r<>FcCs<>t|<00>dksJ<00>t|<03>dksJ<00>t|<04>dksJ<00>t|tjtf<02>s*tj||gtjd<02>}|}t<02>|<04>}|d}|d}|d} tj|d}
td|dg|
<EFBFBD>} t<02>d|dg<02>} tj dtjd<02>} |||| dd d <09>f<|| ||| dd d <09>f<t
| dd d <09>f| dd d <09>f<00>| dd d <09>f<tj dtjd<02>}|d
| d
g|dd d <09>f<t<02>|d
| d
g<02>| |dd d <09>f<t
|dd d <09>f|dd d <09>f<00>|dd d <09>f<|r<>t <0B> t<02>|<0E>t<02>| <0A><01>}|St <0B> t<02>| <0A>t<02>|<0E><01>}|S) a<>Get the affine transform matrix, given the center/scale/rot/output_size.
Args:
center (np.ndarray[2, ]): Center of the bounding box (x, y).
scale (np.ndarray[2, ]): Scale of the bounding box
wrt [width, height].
rot (float): Rotation angle (degree).
output_size (np.ndarray[2, ]): Size of the destination heatmaps.
shift (0-100%): Shift translation ratio wrt the width/height.
Default (0., 0.).
inv (bool): Option to inverse the affine transform direction.
(inv=False: src->dst or inv=True: dst->src)
Returns:
np.ndarray: The transform matrix.
rrrr<00><>r<><00><00><>)rxrN<><00>?) r,r rr<00>listrr<00>pir<69>rWr<>r<00>getAffineTransform)<10>center<65>
input_size<EFBFBD>rot<6F> output_size<7A>shift<66>invZ scale_tmp<6D>src_wZdst_wZdst_hZrot_radZsrc_dirZdst_dir<69>src<72>dst<73>transrrr<00>get_affine_transform1s4
.&.<18>r<>c@s.eZdZdZ      d dd<08>Zd d
<EFBFBD>Zd S) <0A>
WarpAffinezWarp affine the image
F<><00><><E79A99><EFBFBD><EFBFBD><EFBFBD>?r^cCs(||_||_||_||_||_||_dSr!)<06>keep_res<65>pad<61>input_h<5F>input_wrJr<>)r&r<>r<>r<>r<>rJr<>rrrr'ms 
zWarpAffine.__init__c Cs<>t<00>|tj<02>}|jdd<02>\}}|jr:||jBd}||jBd}tj||gtjd<04>}tj|d|dgtjd<04>} nt ||<05>d}|j
|j }}tj|d|dgtjd<04>} t | |d||g<02>}
t<00> |||f<02>}tj||
||ftjd<08>} | |fS) r(Nrrrr<00>@r<00><01>flags)rr<00> COLOR_RGB2BGRrr<>r<>rrrr5r<>r<>r<>r.<00>
warpAffinerA) r&rr0rcrrrsr<>r<><00>s<>cZ trans_input<75>inprrrr3{s  <06>zWarpAffine.__call__N)Fr<46>r<>r<>r<>r^rKrrrrr<>is
<EFBFBD> r<>cCst<00>|<00>}tjdtjd<02>}|d|d}|d|d}t<00>|<00>||d<t<00>|<00> ||d<|d|dt<00>|<00>d|dt<00>|<00>d|d|d <t<00>|<00>||d
<t<00>|<00>||d <|d|dt<00>|<00>d|dt<00>|<00>d|d|d <|S) a<>This code is based on
https://github.com/open-mmlab/mmpose/blob/master/mmpose/core/post_processing/post_transforms.py
Calculate the transformation matrix under the constraint of unbiased.
Paper ref: Huang et al. The Devil is in the Details: Delving into Unbiased
Data Processing for Human Pose Estimation (CVPR 2020).
Args:
theta (float): Rotation angle in degrees.
size_input (np.ndarray): Size of input image [w, h].
size_dst (np.ndarray): Size of output image [w, h].
size_target (np.ndarray): Size of ROI in input plane [w, h].
Returns:
matrix (np.ndarray): A matrix for transformation.
)rrxrrr)rr)rrr<>r<>)rr)rr)rr)rr)r<00>deg2radrWrr|r{)<07>thetaZ
size_inputZsize_dstZ size_target<65>matrix<69>scale_x<5F>scale_yrrr<00>get_warp_matrix<69>s.
<04>
<02><08><04>
<02><08>r<>c@rR) <09>TopDownEvalAffineaQapply affine transform to image and coords
Args:
trainsize (list): [w, h], the standard size used to train
use_udp (bool): whether to use Unbiased Data Processing.
records(dict): the dict contained the image and coords
Returns:
records (dict): contain the image and coords after tranformed
FcCs||_||_dSr!)<02> trainsize<7A>use_udp)r&r<>r<>rrrr'<00>s
zTopDownEvalAffine.__init__cCs<>d}|dddd<03>}d|vr|dn|d}d|vr|dn|}|jrSt||d|jdd|jddg|<06>}tj||t|jd<00>t|jd<00>ftjd <09>}||fSt||||j<02>}tj||t|jd<00>t|jd<00>ftjd <09>}||fS)
Nrr <00><><EFBFBD><EFBFBD><EFBFBD>r<EFBFBD>r<>rJrrr<>)r<>r<>r<>rr<>r"rAr<>)r&<00>imager0r<>Zimshaper<65>rJr<>rrrr3<00>s,<04><06> <10><06>zTopDownEvalAffine.__call__N)FrKrrrrr<><00>s
r<>c@seZdZdd<02>Zdd<04>ZdS)<06>ComposecCs@g|_|D]}|<02><01>}|<03>d<01>}|j<00>t|<04>di|<03><01><01>qdS)N<>typer)<05>
transformsrH<00>pop<6F>append<6E>eval)r&r<><00>op_infoZ new_op_info<66>op_typerrrr'<00>s 
<04>zCompose.__init__cCs<t|<01>\}}|jD] }|||<03>\}}q t<02>|<03>}||d<|S)Nr<4E>)rr<>rH<00>deepcopy)r&rrcr0<00>t<>inputsrrrr3<00>s 

zCompose.__call__N)r=r>r?r'r3rrrrr<><00>s r<>)r<>F)<13>numpyrrrHr<00>objectr rBrLrSr[rtr<>r<>r<>r<>r<>r<>r<>rrrr<00><module>s&C%6
<EFBFBD>82"(