,g)vddlmZmZmZmZddlZddlZddlm Z ddl m Z m Z GddZ dZdZd d Zd Zy) )ListOptionalUnionTupleN) Detections)Color ColorPaletteceZdZdZej dejddddfdeeefde d ed e d e d e d e fdZ dde jdedeeede deee e fde jf dZy) BoxAnnotatora A class for drawing bounding boxes on an image using detections provided. Attributes: color (Union[Color, ColorPalette]): The color to draw the bounding box, can be a single color or a color palette thickness (int): The thickness of the bounding box lines, default is 2 text_color (Color): The color of the text on the bounding box, default is white text_scale (float): The scale of the text on the bounding box, default is 0.5 text_thickness (int): The thickness of the text on the bounding box, default is 1 text_padding (int): The padding around the text on the bounding box, default is 5 g? Tcolor thickness text_color text_scaletext_thickness text_padding avoid_overlapcf||_||_||_||_||_||_||_y)N)rrrrrrr)selfrrrrrrrs F/home/yadonglu/sandbox/huggingface/OmniParser-v2/util/box_annotator.py__init__zBoxAnnotator.__init__s827 '!+!+#1!-#0Nscene detectionslabels skip_label image_sizereturnc tj}tt|D]F}|j|j t \}} } } |j|j|nd} | | n|} t|jtr|jj| n |j}tj||| f| | f|j|j|r|t|t|k7r| n||}tj|||j |j"d\}}|j$sM||j&z}| |j&z }|}| d|j&zz |z }|d|j&zz|z}| }n$t)|j&|||| | | || \}}}}}}tj|||f||f|jtj*|j-}d|dzd|dzzd |dzz}|d kDrd nd }tj.||||f||j ||j"tj0 I|S)a Draws bounding boxes on the frame using the detections provided. Args: scene (np.ndarray): The image on which the bounding boxes will be drawn detections (Detections): The detections for which the bounding boxes will be drawn labels (Optional[List[str]]): An optional list of labels corresponding to each detection. If `labels` are not provided, corresponding `class_id` will be used as label. skip_label (bool): Is set to `True`, skips bounding box label annotation. Returns: np.ndarray: The image with the bounding boxes drawn on it Example: ```python import supervision as sv classes = ['person', ...] image = ... detections = sv.Detections(...) box_annotator = sv.BoxAnnotator() labels = [ f"{classes[class_id]} {confidence:0.2f}" for _, _, confidence, class_id, _ in detections ] annotated_frame = box_annotator.annotate( scene=image.copy(), detections=detections, labels=labels ) ``` N)imgpt1pt2rr)textfontFace fontScalerrr gA`"?gbX9?gv/?)rrr)r*r*)r"r%orgr&r'rrlineType)cv2FONT_HERSHEY_SIMPLEXrangelenxyxyastypeintclass_id isinstancerr by_idx rectangleas_bgrr getTextSizerrrrget_optimal_label_posFILLEDas_rgbputTextLINE_AA)rrrrrrfontix1y1x2y2r4idxrr% text_width text_heighttext_xtext_ytext_background_x1text_background_y1text_background_x2text_background_y2 box_color luminancers rannotatezBoxAnnotator.annotate-sT''s:'I A'__Q/66s;NBB*4*=*=*I ##A&t '2(Cdjj,7 !!#&ZZ  MMHHlln..  Nc*oV&D*AY  '*oo//-- '  ' #J %%d///d///%'"%'!d.?.?*?%?+%M"%'!d.?.?*?%?*%L"%'"rGHLHYHY[egrtvxz|~@BDNPZr[n 24FHZ\n MM');<');<lln**   I ! ,uy|/CCeiXYlFZZI$-OJ KKV$// -- I T r)NFN)__name__ __module__ __qualname____doc__r DEFAULTrBLACKrr3floatboolrnpndarrayrrrstrrrPrrr r s$-9,@,@!KK"1UL()11 1  1  111,'+ 04 uzzuuc# u  u U38_- u urr c0|d|dz |d|dz zS)Nr rr r(r\)boxs rbox_arear_s%AQCFSVO44rct|d|d}t|d|d}t|d|d}t|d|d}td||z td||z zS)Nrr(r r )maxmin)box1box2rArBrCrDs rintersection_arearesq T!Wd1g B T!Wd1g B T!Wd1g B T!Wd1g B q"r'?SBG_ ,,rct||}t|t|z|z }t|dkDr+t|dkDr|t|z }|t|z }nd\}}|rt||z ||S||z S)Nr)rr)rer_ra)rcrd return_max intersectionunionratio1ratio2s rIoUrls$T40L TNXd^ +l :E~htnq0..<%'88e##rc d} ||z} ||z } |} |d|zz |z } |d|zz|z}|}| || | |||}|s| | | | ||fS||z |z } ||z|z} |d|zz |z } |} |}|d|zz|z}| || | |||}|s| | | | ||fS||z} ||z|z} |} |} |d|zz|z}|d|zz|z}| || | |||}|s| | | | ||fS||z |z } ||z } |d|zz |z } |d|zz |z } |}|}| || | |||}|s| | | | ||fS| | | | ||fS)a' check overlap of text and background detection box, and get_optimal_label_pos, pos: str, position of the text, must be one of 'top left', 'top right', 'outer left', 'outer right' TODO: if all are overlapping, return the last one, i.e. outer right Threshold: default to 0.3 cd}tt|D];}|j|jt}t ||||g|dkDs9d}n|dks||dkDs |dks||dkDrd}|S)NFg333333?Trr()r/r0r1r2r3rl) rrJrKrLrMr is_overlapr@ detections rget_is_overlapz-get_optimal_label_pos..get_is_overlaps s:' A"*11#6I&(:@RTfhz}GHJ v13EGY[mmm,  +F ,  ,Fa,..;a,..< ,>@RTfhz}GHJ v13EGY[mmm, F ,  ,Fa,..;a,..< ,>@RTfhz}GHJ v13EGY[mmm,  +F , Fa,..;a,..< ,>@RTfhz}GHJ v13EGY[mmm 6-/ACUWi iir)T)typingrrrrr-numpyrYsupervision.detection.corersupervision.draw.colorrr r r_rerlr:r\rrrvs:// 16XXv5- $Ijr