Best Python code snippet using ATX
example.py
Source:example.py
1# coding: utf-82__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'3import cv24import numpy as np5from ensemble_boxes import *6def show_image(im, name='image'):7 cv2.imshow(name, im.astype(np.uint8))8 cv2.waitKey(0)9 cv2.destroyAllWindows()10def gen_color_list(model_num, labels_num):11 color_list = np.zeros((model_num, labels_num, 3))12 colors_to_use = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255), (255, 0, 255), (255, 255, 0), (0, 0, 0)]13 total = 014 for i in range(model_num):15 for j in range(labels_num):16 color_list[i, j, :] = colors_to_use[total]17 total = (total + 1) % len(colors_to_use)18 return color_list19def show_boxes(boxes_list, scores_list, labels_list, image_size=800):20 thickness = 521 color_list = gen_color_list(len(boxes_list), len(np.unique(labels_list)))22 image = np.zeros((image_size, image_size, 3), dtype=np.uint8)23 image[...] = 25524 for i in range(len(boxes_list)):25 for j in range(len(boxes_list[i])):26 x1 = int(image_size * boxes_list[i][j][0])27 y1 = int(image_size * boxes_list[i][j][1])28 x2 = int(image_size * boxes_list[i][j][2])29 y2 = int(image_size * boxes_list[i][j][3])30 lbl = labels_list[i][j]31 cv2.rectangle(image, (x1, y1), (x2, y2), color_list[i][lbl], int(thickness * scores_list[i][j]))32 show_image(image)33def example_wbf_2_models(iou_thr=0.55, draw_image=True):34 """35 This example shows how to ensemble boxes from 2 models using WBF method 36 :return: 37 """38 boxes_list = [39 [40 [0.00, 0.51, 0.81, 0.91],41 [0.10, 0.31, 0.71, 0.61],42 [0.01, 0.32, 0.83, 0.93],43 [0.02, 0.53, 0.11, 0.94],44 [0.03, 0.24, 0.12, 0.35],45 ],46 [47 [0.04, 0.56, 0.84, 0.92],48 [0.12, 0.33, 0.72, 0.64],49 [0.38, 0.66, 0.79, 0.95],50 [0.08, 0.49, 0.21, 0.89],51 ],52 ]53 scores_list = [54 [55 0.9,56 0.8,57 0.2,58 0.4,59 0.7,60 ],61 [62 0.5,63 0.8,64 0.7,65 0.3,66 ]67 ]68 labels_list = [69 [70 0,71 1,72 0,73 1,74 1,75 ],76 [77 1,78 1,79 1,80 0,81 ]82 ]83 weights = [2, 1]84 if draw_image:85 show_boxes(boxes_list, scores_list, labels_list)86 boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr, skip_box_thr=0.0)87 if draw_image:88 show_boxes([boxes], [scores], [labels.astype(np.int32)])89 print(len(boxes))90 print(boxes)91def example_wbf_1_model(iou_thr=0.55, draw_image=True):92 """93 This example shows how to ensemble boxes from single model using WBF method 94 :return: 95 """96 boxes_list = [97 [0.00, 0.51, 0.81, 0.91],98 [0.10, 0.31, 0.71, 0.61],99 [0.01, 0.32, 0.83, 0.93],100 [0.02, 0.53, 0.11, 0.94],101 [0.03, 0.24, 0.12, 0.35],102 [0.04, 0.56, 0.84, 0.92],103 [0.12, 0.33, 0.72, 0.64],104 [0.38, 0.66, 0.79, 0.95],105 [0.08, 0.49, 0.21, 0.89],106 ]107 scores_list = [0.9, 0.8, 0.2, 0.4, 0.7, 0.5, 0.8, 0.7, 0.3]108 labels_list = [0, 1, 0, 1, 1, 1, 1, 1, 0]109 if draw_image:110 show_boxes([boxes_list], [scores_list], [labels_list])111 boxes, scores, labels = weighted_boxes_fusion([boxes_list], [scores_list], [labels_list], weights=None, iou_thr=iou_thr, skip_box_thr=0.0)112 if draw_image:113 show_boxes([boxes], [scores], [labels.astype(np.int32)])114 print(len(boxes))115 print(boxes)116def example_nms_2_models(method, iou_thr=0.5, sigma=0.5, thresh=0.001, draw_image=True):117 """118 This example shows how to ensemble boxes from 2 models using NMS method 119 :return: 120 """121 boxes_list = [122 [123 [0.00, 0.51, 0.81, 0.91],124 [0.10, 0.31, 0.71, 0.61],125 [0.01, 0.32, 0.83, 0.93],126 [0.02, 0.53, 0.11, 0.94],127 [0.03, 0.24, 0.12, 0.35],128 ],129 [130 [0.04, 0.56, 0.84, 0.92],131 [0.12, 0.33, 0.72, 0.64],132 [0.38, 0.66, 0.79, 0.95],133 [0.08, 0.49, 0.21, 0.89],134 ],135 ]136 scores_list = [137 [138 0.9,139 0.8,140 0.2,141 0.4,142 0.7,143 ],144 [145 0.5,146 0.8,147 0.7,148 0.3,149 ]150 ]151 labels_list = [152 [153 0,154 1,155 0,156 1,157 1,158 ],159 [160 1,161 1,162 1,163 0,164 ]165 ]166 weights = [2, 1]167 if draw_image:168 show_boxes(boxes_list, scores_list, labels_list)169 boxes, scores, labels = nms_method(boxes_list, scores_list, labels_list, method=method, weights=weights, iou_thr=iou_thr, sigma=sigma, thresh=thresh)170 if draw_image:171 show_boxes([boxes], [scores], [labels.astype(np.int32)])172 print(len(boxes))173 print(boxes)174if __name__ == '__main__':175 draw_image = True176 example_wbf_2_models(draw_image=draw_image)177 example_wbf_1_model(draw_image=draw_image)178 example_nms_2_models(draw_image=draw_image, method=3, iou_thr=0.5, thresh=0.0)...
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!!