
    9i0                     \   S SK Jr  S SKJr  S SKrS SKrS SKJ	r	  S SK
r
S SKJr  S SKJr  S SKJrJr  S SKJr  S SKJrJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJ r   S SK!J"r"  S SK#J$r$J%r%  S SK&J'r'  \'" 5       r(\RR                  " \%RT                  \RT                  S9 " S S\5      5       r+g)    N)Optional)imread)estimate_transformwarp)Models)Tensor
TorchModel)MODELS)
FasterRCNN)human_segmenter)define_G)
Pixto3DNet)create_grid)	ModelFileTasks)
get_logger)module_namec                      ^  \ rS rSrU 4S jrS r\R                  " 5       S 5       r\R                  " 5       S 5       r	Sr
U =r$ )HumanReconstruction   c                   > [         TU ]  " USU0UD6  [        R                  R	                  5       (       aJ  [        R
                  " S5      U l        [        R                  SR                  U R
                  5      5        OI[        R
                  " S5      U l        [        R                  SR                  U R
                  5      5        SR                  U[        R                  5      nSR                  US5      nSR                  US5      nSR                  U[        R                  5      nSR                  US	5      n	[        S0 US
   D6U l        [        XR
                  S9U l        U R                  R!                  [        R"                  " USS95        [%        SSSSSSSSS5	      U l        [%        SSSSSSSSS5	      U l        U R(                  R!                  [        R"                  " U5      5        U R&                  R!                  [        R"                  " U5      5        U R(                  R+                  U R
                  5      U l        U R&                  R+                  U R
                  5      U l        U R(                  R-                  5         U R&                  R-                  5         U R                  R+                  U R
                  5      R-                  5       U l        [/        US9U l        [2        R4                  " / SQ5      n
[2        R4                  " / SQ5      n[7        US   U
U5      u  U l        U l        [2        R<                  " S5      nSUS'   [        R>                  " U5      RA                  5       R+                  U R
                  5      U l!        U RB                  SS2SS24   RE                  S5      U l!        [        R                  S5        g)a'  The HumanReconstruction is modified based on PiFuHD and pix2pixhd, publicly available at
        https://shunsukesaito.github.io/PIFuHD/ &
        https://github.com/NVIDIA/pix2pixHD

Args:
    model_dir: the root directory of the model files
    modelconfig: the config param path of the model
	model_dircudazUse GPU: {}cpuzUse CPU: {}z{}/{}zNorm_B_GAN.pthzNorm_F_GAN.pthzfasterrcnn_resnet50.pthmodel)ckptdevice)map_location   @   global   	      instance)
model_path)r'   r'   )r$   r$   r$   
resolutionr'   )r$   r$   Nr   zmodel load over )#super__init__torchr   is_availabler   loggerinfoformatr   TORCH_MODEL_FILETF_GRAPH_FILEr   	meshmodelr   detectorload_state_dictloadr   netBnetFtoevalr   portrait_mattingnparrayr   coordsmatidentityr   floatcalib	unsqueeze)selfr   modelconfigargskwargsr&   normal_back_modelnormal_front_modelhuman_seg_modelfastrcnn_ckptb_minb_maxprojection_matrix	__class__s                x/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/cv/human_reconstruction/Reconstruction.pyr+   HumanReconstruction.__init__"   s    	t>9>v>::""$$,,v.DKKK,,T[[9:,,u-DKKK,,T[[9:^^Iy/I/IJ
#NN96FG$^^I7GH!..I4K4KLy2KL#;k'&:;"kkJ&&JJz6	8Q2xAq!ZH	Q2xAq!ZH			!!%**-?"@A		!!%**->"?@IILL-	IILL-					**4;;7<<> /? K&# +K,Eu,1!3TXKKN"$$\\"34::<??L
ZZBQB'11!4
%&    c                 h    U R                   R                  U5      nUS   nUR                  SSS9nX4$ )N).Nr      )axis)r;   runrepeat)rD   imgresultmasks       rP   get_maskHumanReconstruction.get_maskP   s<    &&**3/	"}}QQ}'yrR   c                 L   [        U5      S S 2S S 2S S24   S-  nUR                  u  p4nSn[        R                  " UR	                  SSS5      [        R
                  S9S   nU R                  R                  U5      nUS   n	US   n
US   nUS   n[        X-
  X-
  5      n[        R                  " XU	-
  S	-  -
  XU-
  S	-  -
  /5      n[        US
-  5      n[        R                  " US   US-  -
  US   US-  -
  /US   US-  -
  US   US-  -   /US   US-  -   US   US-  -
  //5      n[        R                  " SS/SUS-
  /US-
  S//5      n[        SUU5      n[        UUR                  Xf4S9nUS S 2S S 2S S S24   S-  R                  [        R                   5      nU$ )Nr        o@   rT   r   r$   )dtype)N.g       @g?
similarity)output_shaper'      )r   shaper,   tensor	transposefloat32r4   rV   maxr<   r=   intr   r   inverseastypeuint8)rD   img_urlimagehw_
image_sizeimage_tensorbboxleftrighttopbottomold_sizecentersizesrc_ptsDST_PTStform	dst_images                       rP   crop_imgHumanReconstruction.crop_imgV   s   w1bqb)D0++a
||OOAq!$EMM;;DF}}  .AwQ1gau|V\2dlc))6c\S4H+HIK8c>"((VAY16!9tax3GH#AY16!9tax3GH#AY16!9tax3GHJ K ((QFQ
Q$7*q.!9LMN"<'B5==
/GI	q!TrTz*S088B	rR   c                    [         R                  " [         R                  " 5       [         R                  " SS5      /5      n[        R
                  " US5      n[        R                  " U5      R                  S5      nU" U5      R                  S5      nUR                  U R                  5      nU R                  R                  U5      nU R                  R                  U5      n[        R
                  " US5      n[         R                  " 5       " U5      R                  S5      nUR                  5       U-  R!                  5       R                  5       R#                  5       S   n[$        R&                  " US5      S-  S-   S S 2S S 2S S S24   S-  nUR                  5       U-  R!                  5       R                  5       R#                  5       S   n[$        R&                  " US5      S-  S-   S S 2S S 2S S S24   S-  nUR)                  [$        R*                  5      nUR)                  [$        R*                  5      nXg4$ )	N)      ?r   r   )r_   r_   RGBr   )r$   rT   r   r   r'   r^   )
transformsComposeToTensor	Normalizecv2resizeImage	fromarrayconvertrC   r9   r   r8   forwardr7   r   detachnumpyr<   rf   rk   rl   )rD   rX   rZ   	to_tensorim_512	image_512nml_fnml_bs           rP   generation_normal%HumanReconstruction.generation_normalq   s   &&!  /B(
 	 C,OOF+33E:	i(2215	ll4;;'		!!#&		!!#&zz$
+""$T*44Q7t#++-11399;A>e')+./14567DbDjBDIJt#++-11399;A>e')+./14567DbDjBDIJRXX&RXX&|rR   )	rB   r>   r4   r   r?   r3   r7   r8   r;   )__name__
__module____qualname____firstlineno__r+   r[   r,   no_gradr   r   __static_attributes____classcell__)rO   s   @rP   r   r      sB    ,'\ ]]_ 4 ]]_ rR   r   ),os.pathpathosptypingr   r   r   r<   	PIL.Imager   r,   torchvision.transformsr   
skimage.ior   skimage.transformr   r   modelscope.metainfor   modelscope.models.baser   r	   modelscope.models.builderr
   :modelscope.models.cv.human_reconstruction.models.detectorsr   @modelscope.models.cv.human_reconstruction.models.human_segmenterr   9modelscope.models.cv.human_reconstruction.models.networksr   :modelscope.models.cv.human_reconstruction.models.PixToMeshr   /modelscope.models.cv.human_reconstruction.utilsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r.   register_modulehuman_reconstructionr   r)   rR   rP   <module>r      s      
    +  6 & 5 , N G 6 .	 	F,G,GIg* gIgrR   