Package: bbox_objected
bbox-objectedMakes manipulations with bounding boxes easier in Computer Vision projects. With zero dependencies by default.Installation:pip install bbox-objectedOptional dependencies:pip install numpypip install opencv-pythonorpip install opencv-contrib-pythonExamples:AbsBBoxis needed to store absolute coordinates (usesinttype strictly)frombbox_objectedimportAbsBBoxbbox=AbsBBox((35,45,100,80),kind="x1y1wh",text="abs_sample")assertrepr(bbox)=="<AbsBBox(x1=35, y1=45, x2=135, y2=125) - abs_sample>"assertbbox.get_x1y1x2y2()==(35,45,135,125)If you need to store relative coordinates, you can useRelBBox(values in range [0., 1.])frombbox_objectedimportRelBBoxbbox=RelBBox((0.1,0.2,0.5,0.6),kind="x1x2y1y2",text="rel_sample")assertrepr(bbox)=="<RelBBox(x1=0.1, y1=0.5, x2=0.2, y2=0.6) - rel_sample>"assertbbox.get_tl_tr_br_bl()==((0.1,0.5),(0.2,0.5),(0.2,0.6),(0.1,0.6))Conversion between types is availablefrombbox_objectedimportRelBBoxbbox=RelBBox((0.1,0.2,0.5,0.6),kind="x1y1x2y2",text="sample")assertrepr(bbox)=="<RelBBox(x1=0.1, y1=0.2, x2=0.5, y2=0.6) - sample>"assert(repr(bbox.as_abs(1920,1080))=="<AbsBBox(x1=192, y1=216, x2=960, y2=648) - sample>")assert(bbox.as_abs(1920,1080).as_rel(1920,1080)isnotbbox)There is a bunch of attributes for each bboxfrombbox_objectedimportRelBBox,AbsBBoxbbox=AbsBBox((40,40,60,60))assert(bbox.x1,bbox.y1,bbox.x2,bbox.y2)==(40,40,60,60)assert(bbox.w,bbox.h)==(20,20)assert(bbox.tl,bbox.tr,bbox.br,bbox.bl)==((40,40),(60,40),(60,60),(40,60))bbox=AbsBBox((40,40,60,60))assert(bbox.center,bbox.area)==((50.0,50.0),400)assert(bbox.xc,bbox.yc)==(50.0,50.0)Availablekindsof bboxes:frombbox_objected.typesimportBBoxKind# special format of 'EasyOCR' libraryassertBBoxKind.free_list=="tl_tr_br_bl"assertBBoxKind.tl_tr_br_bl=="tl_tr_br_bl"# special format of 'EasyOCR' libraryassertBBoxKind.horizontal_list=="x1x2y1y2"assertBBoxKind.x1x2y1y2=="x1x2y1y2"# own format of PascalVOC image datasetassertBBoxKind.pascal_voc=="x1y1x2y2"assertBBoxKind.x1y1x2y2=="x1y1x2y2"# own format of COCO image datasetassertBBoxKind.coco=="x1y1wh"assertBBoxKind.x1y1wh=="x1y1wh"# gets object of '.rectangle()' method of 'PyWinAuto' libraryassertBBoxKind.pywinauto=="pywinauto"# gets special coords format of 'WinOCR' libraryassertBBoxKind.winocr=="winocr"# gets 'monitor' object of library 'mss'assertBBoxKind.mss=="mss"There is respectiveget_method for each bboxkind, except"pywinauto"and"winocr"Some simple editing of bboxes is also availablefrombbox_objectedimportAbsBBoxbbox=AbsBBox((100,200,300,400))assertrepr(bbox)=="<AbsBBox(x1=100, y1=200, x2=300, y2=400)>"bbox.zero_basis()assertrepr(bbox)=="<AbsBBox(x1=0, y1=0, x2=200, y2=200)>"bbox.move_basis(25,45)assertrepr(bbox)=="<AbsBBox(x1=25, y1=45, x2=225, y2=245)>"other_bbox=AbsBBox((200,300,400,500))assertrepr(other_bbox)=="<AbsBBox(x1=200, y1=300, x2=400, y2=500)>"# chooses coords to get max area, doesn't create new instancebbox.update_from(other_bbox)assertrepr(bbox)=="<AbsBBox(x1=25, y1=45, x2=400, y2=500)>"# takes all coords from 'other', doesn't create new instancebbox.replace_from(other_bbox)assertrepr(bbox)=="<AbsBBox(x1=200, y1=300, x2=400, y2=500)>"Additional functionality:Each bbox can be drawn or cropped from image. Can work both withAbsBBoxandRelBBoximportnumpyasnpimportcv2frombbox_objectedimportAbsBBoxbbox=AbsBBox((100,200,300,400))# 'x1y1x2y2' bbox kind is defaultimg=np.empty((512,512,3),dtype=np.uint8)# random RGB imagecropped=bbox.crop_from(img)# 'numpy' must be installedassertcropped.shape==(200,200,3)bbox.show_on(img,text="sample")# 'opencv' must be installedcv2.waitKey(1)cv2.destroyAllWindows()Also, there are several useful functions. Currently works only withAbsBBox.frombbox_objectedimportAbsBBoxfrombbox_objected.bbox_utilsimportget_distance,get_IoU,get_cos_betweenbbox_1=AbsBBox((100,200,300,400),kind="x1y1wh")bbox_2=AbsBBox((100,400,100,400),kind="horizontal_list")assertget_distance(bbox_1,bbox_2)==150.0# Intersection over Unionassertget_IoU(bbox_1,bbox_2)==0.4# angle around center in (450 ,350)assertget_cos_between(bbox_1,bbox_2,450,350)==0.7592566023652966