a *f1@s0ddlZddlZddlmZmZmZddlZddlZddl m Z ddl m Z ddl mZmZmZGdd d ZGd d d eZd d ZddZddZGdddeZGdddeZzddlZedeZWn*eyeeZedeZYn0GdddeZ GdddeZ!GdddeZ"dS)N)ListOptionalTuple)DensePoseDataRelative)DensePoseChartResult)BoxesImageMatrixVisualizerc@sbeZdZeeeeeeefedddZ edddZ e j ddd d Z d d Zd dZdS)DensePoseResultsVisualizer) image_bgrresults_and_boxes_xywhreturnc Cs|\}}|dus|dur|S|}||}t|D]L\}}t|jdtj|j dftj }| ||||q:| |}|SNo@) cpunumpycreate_visualization_context enumeratetorchcatlabelstypefloat32uvuint8visualize_iuv_arrcontext_to_image_bgr) selfr rZdensepose_resultZ boxes_xywhcontextiresultZ iuv_arrayr#L/data1/chongzheng_p23/Projects/CatVTON-hf/densepose/vis/densepose_results.py visualizes   z$DensePoseResultsVisualizer.visualizer cCs|SNr#)rr r#r#r$r$sz7DensePoseResultsVisualizer.create_visualization_contextNiuv_arrrcCsdSr'r#)rr r) bbox_xywhr#r#r$r'sz,DensePoseResultsVisualizer.visualize_iuv_arrcCs|Sr'r#rr r#r#r$r*sz/DensePoseResultsVisualizer.context_to_image_bgrcCs|Sr'r#r+r#r#r$get_image_bgr_from_context-sz5DensePoseResultsVisualizer.get_image_bgr_from_context)__name__ __module__ __qualname__r rrrrr r%rnpndarrayrrr,r#r#r#r$r s r c@s:eZdZdejddfddZddZejdd d d Z dS) (DensePoseMaskedColormapResultsVisualizerTffffff??cKs"t||||d|_||_||_dS)N)inplacecmap val_scalealpha)r mask_visualizerdata_extractorsegm_extractor)rr:r;r5r6r8r7kwargsr#r#r$__init__2s z1DensePoseMaskedColormapResultsVisualizer.__init__cCs|Sr'r#r+r#r#r$rBsz=DensePoseMaskedColormapResultsVisualizer.context_to_image_bgrNr(cCsR||}||}||}tj|jtjd}d||dk<|j||||}dS)Ndtyperr) r,r:r;r0zerosshaperr9r%)rr r)r*r matrixsegmmaskr#r#r$rEs     z:DensePoseMaskedColormapResultsVisualizer.visualize_iuv_arr) r-r.r/cv2COLORMAP_PARULAr=rr0r1rr#r#r#r$r21s r2cCs|dddddfS)Nrr#r)r#r#r$_extract_i_from_iuvarrNsrHcCs|dddddfS)Nrr#rGr#r#r$_extract_u_from_iuvarrRsrIcCs|dddddfS)Nrr#rGr#r#r$_extract_v_from_iuvarrVsrJc@s@eZdZd ddZedddZddZeje d d d d Z d S)$DensePoseResultsMplContourVisualizer cKs||_||_dSr')levels plot_args)rrMr<r#r#r$r=[sz-DensePoseResultsMplContourVisualizer.__init__r&c Csddlm}ddlm}i}||d<d}t|jd|}t|jd|}|j||f|d}|gd|d||d <||} | |d <d|jd|jddf} |j |ddddddd f| d |S) Nr)FigureCanvasAggr dr)figsizedpi)rrrrofffigcanvas)extent) matplotlib.pyplotpyplotZmatplotlib.backends.backend_aggrOfloatrAfigureaxesaxisimshow) rr pltZ FigureCanvasr rRZ height_inchesZ width_inchesrTrUrWr#r#r$r_s    &zADensePoseResultsMplContourVisualizer.create_visualization_contextc Csv|d}tt||\}}|d}|tj|dd}|||d}|dddddddf }|S)NrTrUrr>rV) mapintZget_size_inchesZget_dpidrawr0 fromstringZ tostring_rgbreshapecopy) rr rTwhrUZimage_1d image_rgbr r#r#r$rrs z9DensePoseResultsMplContourVisualizer.context_to_image_bgrNr)r*rcCsddlm}t|td}t|td}|d|d|d|d|d|df}|j||jfd|i|j|j||jfd|i|jdS)Nrrrrr`rW) rXrYrIastyperZrJcontourrMrN)rr r)r*r_uvrWr#r#r$r|s z6DensePoseResultsMplContourVisualizer.visualize_iuv_arr)rL) r-r.r/r=r rrr0r1r rr#r#r#r$rKZs  rKc@sFeZdZdZdddZejeddddZd d Z d d Z d dZ dS)'DensePoseResultsCustomContourVisualizerz6 Contour visualization using marching squares rLcKstj}t|tr"tdd||_n||_d|vr<|d|_ndgt|j|_||_ t |jd tj |}dd|D|_ dS)Nrr linewidthscSsg|]}dd|DqS)cSsg|] }t|qSr#)rb).0rnr#r#r$ zODensePoseResultsCustomContourVisualizer.__init__...)ravel)rrZ img_color_bgrr#r#r$rsszDDensePoseResultsCustomContourVisualizer.__init__..)rErF isinstancerbr0linspacerMrplenrN applyColorMaprkrlevel_colors_bgr)rrMr<r6Zimg_colors_bgrr#r#r$r=s  z0DensePoseResultsCustomContourVisualizer.__init__NrjcCsZ||}t|}t|td}t|td}||||||||||dSr)r,rHrIrkrZrJ _contours)rr r)r*r rCrmrnr#r#r$rs  z9DensePoseResultsCustomContourVisualizer.visualize_iuv_arrcCsdtdtjdD]L}||k}t|s*qt||}t||}t|\} } t| } t| d} t| } t| d}|| dks| | dkrq|jdd}|jdd}t |j D]\}}||ks||krq|| | | |f|k}|ddddf|ddddfd|ddddfd|ddddfd}|| | | |f}|ddddf|ddddfd|ddddfd|ddddfd}tj |dgd}|j |}|j |}|js|ddkrP|dd krP|j\}}|||fdkrP||||||||d|j|||| | f |qqqdS) NrrrVr multi_index)flags)ranger N_PART_LABELSr0anyaminamaxnonzerorArrMnditerrzrpfinishedr~ _draw_lineiternext)rr arrrCr*part_idxrDZarr_minZarr_maxIJi0i1j0j1NwNhZ level_idxlevelvpZ bin_codesmpZbin_mask_codesit color_bgr linewidthr!jr#r#r$r{sX   \\   z1DensePoseResultsCustomContourVisualizer._contoursc  Cs|||||| | | } | \}}}}||}||}| D]~}|d\}}|d\}}t||||t||||f}t||||t||||f}t|||||q6dS)Nrr)_bin_code_2_linesrbrEline)rr rrDrnrrbin_code multi_idxr*rroffsetlinesx0y0rgrhx1y1rZx0rZy0rZx1rZy1rZpt0pt1r#r#r$rs   ((z2DensePoseResultsCustomContourVisualizer._draw_linec Cs|\}} |\} } | |7} | | 7} || | f|| d| f|| d| df|| | dff\} } }}t| |}t| |}d|}d|}|dks|dkr|| | | }|| || }||||f}||||f}||fgS|dks|dkrF|| | | }|| || }||||f}|||||f}||fgS|dksZ|dkr|| || }|| || }||||f}|||||f}||fgS|dks|d kr|| || }||||}|||||f}|||||f}||fgS|d ks"|d krp|| | | }||||}||||f}|||||f}||fgS|d ks|d kr|| || }||||}||||f}|||||f}||fgS|dkrt|| | | }|| || }||||f}|||||f}|| || }||||}||||f}|||||f}||f||fgS|dkr|| || }|| | | }||||f}||||f}|| || }||||}|||||f}|||||f}||f||fgSgS)Nrr4r r` r|  r}rL)rZ) rrrnrrrrrrrr!rv0v1v2v3Zx0iZy0jHeZWeabrpt2a1b1Zpt11Zpt12a2b2Zpt21Zpt22r#r#r$rsD          z9DensePoseResultsCustomContourVisualizer._bin_code_2_lines)rL) r-r.r/__doc__r=r0r1r rr{rrr#r#r#r$ros  .roZAggzs4  !1,