Best Python code snippet using avocado_python
anno_func.py
Source:anno_func.py
1import json2import pylab as pl3import random4import numpy as np5#import cv26import copy7type42="i2,i4,i5,il100,il60,il80,ip,p10,p11,p12,p19,p23,p26,p27,p3,p5,p6,pg,ph4,ph4.5,ph5,pl100,pl120,pl20,pl30,pl40,pl5,pl50,pl60,pl70,pl80,pm20,pm30,pm55,pn,pne,pr40,w13,w32,w55,w57,w59"8type42 = type42.split(',')9def rect_cross(rect1, rect2):10 rect = [max(rect1[0], rect2[0]),11 max(rect1[1], rect2[1]),12 min(rect1[2], rect2[2]),13 min(rect1[3], rect2[3])]14 rect[2] = max(rect[2], rect[0])15 rect[3] = max(rect[3], rect[1])16 return rect17def rect_area(rect):18 return float(max(0.0, (rect[2]-rect[0])*(rect[3]-rect[1])))19def calc_cover(rect1, rect2):20 crect = rect_cross(rect1, rect2)21 return rect_area(crect) / rect_area(rect2)22def calc_iou(rect1, rect2):23 crect = rect_cross(rect1, rect2)24 ac = rect_area(crect)25 a1 = rect_area(rect1)26 a2 = rect_area(rect2)27 return ac / (a1+a2-ac)28def get_refine_rects(annos, raw_rects, minscore=20):29 cover_th = 0.530 refine_rects = {}31 for imgid in raw_rects.keys():32 v = raw_rects[imgid]33 tv = copy.deepcopy(sorted(v, key=lambda x:-x[2]))34 nv = []35 for obj in tv:36 rect = obj[1]37 rect[2]+=rect[0]38 rect[3]+=rect[1]39 if rect_area(rect) == 0: continue40 if obj[2] < minscore: continue41 cover_area = 042 for obj2 in nv:43 cover_area += calc_cover(obj2[1], rect)44 if cover_area < cover_th:45 nv.append(obj)46 refine_rects[imgid] = nv47 results = {}48 for imgid, v in refine_rects.items():49 objs = []50 for obj in v:51 mobj = {"bbox":dict(zip(["xmin","ymin","xmax","ymax"], obj[1])), 52 "category":annos['types'][int(obj[0]-1)], "score":obj[2]}53 objs.append(mobj)54 results[imgid] = {"objects":objs}55 results_annos = {"imgs":results}56 return results_annos57def box_long_size(box):58 return max(box['xmax']-box['xmin'], box['ymax']-box['ymin'])59def eval_annos(annos_gd, annos_rt, iou=0.5, imgids=None, check_type=True, types=None, minscore=40, minboxsize=0, maxboxsize=512, match_same=True):60 ac_n, ac_c = 0,061 rc_n, rc_c = 0,062 if imgids==None:63 imgids = annos_rt['imgs'].keys()64 if types!=None:65 types = { t:0 for t in types }66 miss = {"imgs":{}}67 wrong = {"imgs":{}}68 right = {"imgs":{}}69 70 for imgid in imgids:71 v = annos_rt['imgs'][imgid]72 vg = annos_gd['imgs'][imgid]73 convert = lambda objs: [ [ obj['bbox'][key] for key in ['xmin','ymin','xmax','ymax']] for obj in objs]74 objs_g = vg["objects"]75 objs_r = v["objects"]76 bg = convert(objs_g)77 br = convert(objs_r)78 79 match_g = [-1]*len(bg)80 match_r = [-1]*len(br)81 if types!=None:82 for i in range(len(match_g)):83 if not types.has_key(objs_g[i]['category']):84 match_g[i] = -285 for i in range(len(match_r)):86 if not types.has_key(objs_r[i]['category']):87 match_r[i] = -288 for i in range(len(match_r)):89 if objs_r[i].has_key('score') and objs_r[i]['score']<minscore:90 match_r[i] = -291 matches = []92 for i,boxg in enumerate(bg):93 for j,boxr in enumerate(br):94 if match_g[i] == -2 or match_r[j] == -2:95 continue96 #if match_same and objs_g[i]['category'] != objs_r[j]['category']: continue97 tiou = calc_iou(boxg, boxr)98 if tiou>iou:99 matches.append((tiou, i, j))100 matches = sorted(matches, key=lambda x:-x[0])101 for tiou, i, j in matches:102 if match_g[i] == -1 and match_r[j] == -1:103 match_g[i] = j104 match_r[j] = i105 106 for i in range(len(match_g)):107 boxsize = box_long_size(objs_g[i]['bbox'])108 erase = False109 if not (boxsize>=minboxsize and boxsize<maxboxsize):110 erase = True111 #if types!=None and not types.has_key(objs_g[i]['category']):112 # erase = True113 if erase:114 if match_g[i] >= 0:115 match_r[match_g[i]] = -2116 match_g[i] = -2117 118 for i in range(len(match_r)):119 boxsize = box_long_size(objs_r[i]['bbox'])120 if match_r[i] != -1: continue121 if not (boxsize>=minboxsize and boxsize<maxboxsize):122 match_r[i] = -2123 124 miss["imgs"][imgid] = {"objects":[]}125 wrong["imgs"][imgid] = {"objects":[]}126 right["imgs"][imgid] = {"objects":[]}127 miss_objs = miss["imgs"][imgid]["objects"]128 wrong_objs = wrong["imgs"][imgid]["objects"]129 right_objs = right["imgs"][imgid]["objects"]130 131 tt = 0132 for i in range(len(match_g)):133 if match_g[i] == -1:134 miss_objs.append(objs_g[i])135 for i in range(len(match_r)):136 if match_r[i] == -1:137 obj = copy.deepcopy(objs_r[i])138 obj['correct_catelog'] = 'none'139 wrong_objs.append(obj)140 elif match_r[i] != -2:141 j = match_r[i]142 obj = copy.deepcopy(objs_r[i])143 if not check_type or objs_g[j]['category'] == objs_r[i]['category']:144 right_objs.append(objs_r[i])145 tt+=1146 else:147 obj['correct_catelog'] = objs_g[j]['category']148 wrong_objs.append(obj)149 150 151 rc_n += len(objs_g) - match_g.count(-2)152 ac_n += len(objs_r) - match_r.count(-2)153 154 ac_c += tt155 rc_c += tt156 if types==None:157 styps = "all"158 elif len(types)==1:159 styps = types.keys()[0]160 elif not check_type or len(types)==0:161 styps = "none"162 else:163 styps = "[%s, ...total %s...]"%(types.keys()[0], len(types))164 report = "iou:%s, size:[%s,%s), types:%s, accuracy:%s, recall:%s"% (165 iou, minboxsize, maxboxsize, styps, 1 if ac_n==0 else ac_c*1.0/ac_n, 1 if rc_n==0 else rc_c*1.0/rc_n)166 summury = {167 "iou":iou,168 "accuracy":1 if ac_n==0 else ac_c*1.0/ac_n,169 "recall":1 if rc_n==0 else rc_c*1.0/rc_n,170 "miss":miss,171 "wrong":wrong,172 "right":right,173 "report":report174 }...
maximum_bipartite_matching.py
Source:maximum_bipartite_matching.py
1# Python program to find2# maximal Bipartite matching.3class GFG:4 def __init__(self, graph):5 self.graph = graph # residual graph6 self.ppl = len(graph) # applicantã®æ°7 self.jobs = len(graph[0]) # jobã®æ°8 def bpm(self, u, match_r, visited):9 """10 u(applicant)ã¨é£æ¥ããå
¨ã¦ã®v(job)ã«ã¤ãã¦ã11 """12 for v in range(self.jobs):13 if self.graph[u][v] and not visited[v]:14 visited[v] = True15 # v(job)ã¨ãããã³ã°ãã人ãããªãããããã¯vã¨ãããã³ã°ãã人ãä»ã®vã¨ãããã³ã°ãããããããvã¨uããããã³ã°ããã16 if match_r[v] == -1 or self.bpm(match_r[v], match_r, visited):17 match_r[v] = u18 return True19 return False20 def max_bpm(self):21 match_r = [-1] * self.jobs22 result = 023 for i in range(self.ppl):24 visited = [False] * self.jobs25 if self.bpm(i, match_r, visited):26 result += 127 return result28bpGraph = [[0, 1, 1, 0, 0, 0],29 [1, 0, 0, 1, 0, 0],30 [0, 0, 1, 0, 0, 0],31 [0, 0, 1, 1, 0, 0],32 [0, 0, 0, 0, 0, 0],33 [0, 0, 0, 0, 0, 1]]34g = GFG(bpGraph)35print("Maximum number of applicants that can get job is %d " % g.max_bpm())...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!