Best Python code snippet using tox_python
precise.py
Source:precise.py
1import os2import sys3import codecs4import json5import xml.etree.ElementTree as ET6import numpy as np7import argparse8import glob9parser = argparse.ArgumentParser()10parser.add_argument("--truth", default="/home/ubuntu/tools/alice/data/ideal/test/ann/", help="path to ground truth annotations folder")11parser.add_argument("--predicted", default="/home/ubuntu/tools/alice/data/ideal/test/out/test/", help="path to predicted boxes folder")12parser.add_argument("--threshold", default=0.5, help="threshold for IOU score")13parser.add_argument("--center", default=True, help="Whether to show Center Score")14parser.add_argument("--iou", default=True, help="Whether to show IOU Score")15parser.add_argument("--labels", default="/home/ubuntu/darkflow/labels.txt", help="path to labels.txt")16args = parser.parse_args()17def isCenterMatch(true, predicted):18 """19 Defines whether true and predicted intersects or not20 Args:21 true - [filename, label, xmin, ymin, xmax, ymax]22 predicted - [filename, label, xmin, ymin, xmax, ymax]23 """24 # predicted coordinates25 xmin_predicted = predicted[2]26 ymin_predicted = predicted[3]27 xmax_predicted = predicted[4]28 ymax_predicted = predicted[5]29 # center of the predicted box30 predictedCenter = (int((xmax_predicted - xmin_predicted)/2.0+xmin_predicted),31 int((ymax_predicted - ymin_predicted)/2.0)+ymin_predicted)32 # true coordinates33 xmin_true = true[2]34 ymin_true = true[3]35 xmax_true = true[4]36 ymax_true = true[5]37 # center of the true box38 trueCenter = (int((xmax_true - xmin_true)/2.0+xmin_true),39 int((ymax_true - ymin_true)/2.0)+ymin_true)40 if isPointInsideRect(predictedCenter, true) or isPointInsideRect(trueCenter, predicted):41 return 142 else:43 return 044def isPointInsideRect(point, rectangle):45 """46 defines whether point inside rectangle or not47 """48 x, y = point[0], point[1]49 xmin = rectangle[2]50 ymin = rectangle[3]51 xmax = rectangle[4]52 ymax = rectangle[5]53 if xmin<=x and ymin<=y and xmax>=x and ymax>=y:54 return True55 else:56 return False57def find_intersection_square(predicted, true):58 """59 returns intersection square between rectangles60 """61 # predicted coordinates62 xmin_predicted = predicted[2]63 ymin_predicted = predicted[3]64 xmax_predicted = predicted[4]65 ymax_predicted = predicted[5]66 # true coordinates67 xmin_true = true[2]68 ymin_true = true[3]69 xmax_true = true[4]70 ymax_true = true[5]71 dx = abs(min(xmax_predicted, xmax_true) - max(xmin_predicted, xmin_true))72 #print min(xmax_predicted, xmax_true)73 dy = abs(min(ymax_predicted, ymax_true) - max(ymin_predicted, ymin_true))74 #print "dx*dy = {}".format(dx*dy)75 if (dx>=0) and (dy>=0):76 #print "dx*dy = ".format(dx*dy)77 return dx*dy78def find_square(coordinates):79 """80 coordinates - [filename, label,xmin,ymin,xmax,ymax]81 returns square for rectangle with given coordinates (coords)82 """83 xmin = coordinates[2]84 ymin = coordinates[3]85 xmax = coordinates[4]86 ymax = coordinates[5]87 width = xmax - xmin88 height = ymax - ymin89 return width * height90def IOU(predicted, true):91 """92 returns intersection over union for rectangles with given coordinates93 """94 predicted_square = find_square(predicted) #square of predicted box95 true_square = find_square(true) # square of ground truth box96 sum_of_squares = predicted_square + true_square97 intersection = find_intersection_square(predicted, true)98 if intersection != sum_of_squares:99 union = sum_of_squares - intersection100 return float(intersection)/union101 else:102 warning = "WARNING!\nIntersection = {}\nSum of squares = {}\npredicted box - {}\ntrue box - {}"103 print warning.format(intersection,104 sum_of_squares, predicted, true)105 return 0106def get_list_of_classes(filename):107 return open(filename).read().splitlines()108def get_class_measures_dict(classes):109 TP, FP, TN, AP = 0, 0, 0, 0110 measure = [TP, FP, TN, AP]111 class_measures_dict = {}112 for class_ in classes:113 class_measures_dict[class_] = [0,0,0,0]114 return class_measures_dict115# Parse xml annotations116ann_folder = args.truth117dumps_xml = {}118for file in glob.glob(ann_folder + '/' + '*.xml'):119 annotation_file = open(file)120 tree = ET.parse(annotation_file)121 root = tree.getroot()122 # filename without extension123 filename = str(root.find('filename').text).rsplit('.', 1)[0]124 for obj in root.iter('object'):125 current = list()126 label = obj.find('name').text127 xmlbox = obj.find('bndbox')128 xmin = int(float(xmlbox.find('xmin').text))129 xmax = int(float(xmlbox.find('xmax').text))130 ymin = int(float(xmlbox.find('ymin').text))131 ymax = int(float(xmlbox.find('ymax').text))132 current = (filename, label, xmin, ymin, xmax, ymax)133 dumps_xml[current] = 0134 annotation_file.close()135# Parse recognized metadata from json format136json_folder = args.predicted137dumps_json = {}138for file in glob.glob(json_folder + '/' + '*.json'):139 try:140 config = json.loads(codecs.open(file, "r", encoding='utf-8', errors='ignore').read())141 except ValueError:142 print "{} does not have any bounding box".format(file)143 continue144 for dictionary in config:145 if dictionary == None:146 continue147 label = dictionary["label"]148 xmin = dictionary["topleft"]["x"]149 ymin = dictionary["topleft"]["y"]150 xmax = dictionary["bottomright"]["x"]151 ymax = dictionary["bottomright"]["y"]152 filename = os.path.basename(file).rsplit('.', 1)[0]153 current = (filename, label, xmin, ymin, xmax, ymax)154 dumps_json[current] = 0155classes = get_list_of_classes(args.labels)156class_measures_dict_iou = get_class_measures_dict(classes)157#Intersection Over Union158threshold = args.threshold159class_measures_dict_iou = get_class_measures_dict(classes)160for predicted_coordinate in dumps_json:161 for true_coordinate in dumps_xml:162 if predicted_coordinate[0] == true_coordinate[0] and dumps_xml[true_coordinate] == 0:163 iou_value = IOU(true = true_coordinate, predicted = predicted_coordinate)164 if iou_value >= threshold:165 # filling class measures166 for class_ in classes:167 true_label = true_coordinate[1]168 predicted_label = predicted_coordinate[1]169 if true_label == predicted_label:170 if true_label == class_:171 dumps_xml[true_coordinate] = 1172 dumps_json[predicted_coordinate] = 1173 class_measures_dict_iou[class_][0] += 1174 break175 else:176 class_measures_dict_iou[predicted_label][1] += 1177 break178for key in dumps_json:179 if dumps_json[key] == 0: # key - (filename, label, xmin, ymin, xmax, ymax)180 label = key[1]181 if label != 'None':182 class_measures_dict_iou[label][2] += 1183for key in dumps_xml:184 if dumps_xml[key] == 0: # key - (filename, label, xmin, ymin, xmax, ymax)185 label = key[1]186 if label != 'None':187 class_measures_dict_iou[label][2] += 1188AP_list = []189for class_ in classes:190 TP = class_measures_dict_iou[class_][0]191 FP = class_measures_dict_iou[class_][1]192 TN = class_measures_dict_iou[class_][2]193 #print TP, FP, TN194 if TP != 0 or FP != 0 or TN != 0:195 AP = float(TP)/(TP+FP+TN)196 class_measures_dict_iou[class_][3] = AP197 AP_list.append(AP)198IOU_mAP = np.mean(AP_list)199# mAP with centers200# making all values equals 0201for key in dumps_json:202 dumps_json[key] = 0203for key in dumps_xml:204 dumps_xml[key] = 0205class_measures_dict = get_class_measures_dict(classes)206for predicted_coordinate in dumps_json:207 for true_coordinate in dumps_xml:208 if predicted_coordinate[0] == true_coordinate[0] and dumps_xml[true_coordinate] == 0:209 if isCenterMatch(true_coordinate, predicted_coordinate) == 1:210 # filling class measures211 for class_ in classes:212 true_label = true_coordinate[1]213 predicted_label = predicted_coordinate[1]214 if true_label == predicted_label:215 if true_label == class_:216 dumps_xml[true_coordinate] = 1217 dumps_json[predicted_coordinate] = 1218 class_measures_dict[class_][0] += 1219 break220 else:221 class_measures_dict[predicted_label][1] += 1222 break223for key in dumps_json:224 if dumps_json[key] == 0: # key - (filename, label, xmin, ymin, xmax, ymax)225 label = key[1]226 if label != 'None':227 class_measures_dict[label][2] += 1228for key in dumps_xml:229 if dumps_xml[key] == 0: # key - (filename, label, xmin, ymin, xmax, ymax)230 label = key[1]231 if label != 'None':232 class_measures_dict[label][2] += 1233AP_list = []234for class_ in classes:235 TP = class_measures_dict[class_][0]236 FP = class_measures_dict[class_][1]237 TN = class_measures_dict[class_][2]238 if TP != 0 or FP != 0 or TN != 0:239 AP = float(TP)/(TP+FP+TN)240 class_measures_dict[class_][3] = AP241 AP_list.append(AP)242centre_mAP = np.mean(AP_list)243bool_center = args.center244bool_iou = args.iou245if bool_center:246 print "CENTRE_score - {}".format(centre_mAP)247 for class_ in classes:248 AP = class_measures_dict[class_][3]249 print "{} - {}".format(class_, AP)250if bool_iou:251 print "IOU_mAP - {}".format(IOU_mAP)252 for class_ in classes:253 AP = class_measures_dict_iou[class_][3]254 print "{} - {}".format(class_, AP)255if not (bool_iou or bool_center):...
ChinaNatureService.py
Source:ChinaNatureService.py
1# # -*- coding: utf-8 -*-2# # @Time : 2022/7/29 15:123# # @Author : lzc4# # @Email : hybpjx@163.com5# # @File : ChinaNatureService.py6# # @Software: PyCharm7# import re8# import time9# from scrapy.selector import Selector10# # from playwright import11#12# from pkg.Invoking import APIInvoke13# from utils.data_to_html import DataFormat14#15# import xmltodict, json16#17#18# class ChinaNatureService:19# name_dict = {20# "naMineName": "项ç®å称",21# "approvalDate": "æ¹åæ¥æ",22# "province": "ç份",23# "qtLatStart": "èµ·å§çº¬åº¦",24# "qtLatEnd": "ç»æ¢çº¬åº¦",25# "qtLonStart": "èµ·å§ç»åº¦",26# "qtLonEnd": "ç»æ¢ç»åº¦",27# "naApplyPerson": "å
¬å¸",28# "qtArea": "é¢ç§¯",29# "businessType": "项ç®ç±»å",30# 'naGeographyPosition': 'å°çä½ç½®',31# 'approvalStatus': 'åçç¶æ',32# 'qtMainMine': 'å¼éç¿ç§',33# 'naMineKind': 'ç¿æ',34# 'naMineKindName': 'ç¿æå称'35# }36#37# key = re.compile(r'</(.*?)>', re.S)38# value = re.compile(r'>(.*?)<', re.S)39# API = APIInvoke40#41# driver = uc.Chrome()42#43# def parse_kyqysl(self, url_list, item):44# for url in url_list:45# dumps_json = self.get_json(url)46#47# for data in dumps_json['ResponseObj']['repMap']['list']:48# item['title_name'] = data['projectName']49# item[50# 'title_url'] = f'https://zwfw.mnr.gov.cn:8090/tlwserver/publicservices/bjgs/kq/yq_ck?id={data["id"]}'51# item['title_date'] = data['approvalDate']52# try:53# detail_json = self.get_json(item['title_url'])54# item['content_html'] = DataFormat().dictToHtml(detail_json['CkYqGsEntity'], self.name_dict)55# except TypeError:56# item['content_html'] = DataFormat().dictToHtml(data, self.name_dict)57# self.API().data_update(item)58#59# def parse_bjgsKflyFags(self, url_list, item):60# for url in url_list:61# dumps_json = self.get_json(url)62# for data in dumps_json['ResponseObj']['repMap']['plgsInfoEntityList']:63# item['title_name'] = data['fagsQdmcTitle']64# item[65# 'title_url'] = f'https://zwfw.mnr.gov.cn:8090/bjgsKflyFagsDetail?fagsId={data["id"]}'66# item['title_date'] = data['fagsKssj']67# try:68# detail_json = self.get_json(item['title_url'])69# item['content_html'] = DataFormat().dictToHtml(detail_json['repMap'], self.name_dict)70# except TypeError:71# item['content_html'] = DataFormat().dictToHtml(data, self.name_dict)72# self.API().data_update(item)73#74# def parse_kyqypz(self, url_list, item):75# for url in url_list:76# dumps_json = self.get_json(url)77#78# for data in dumps_json['ResponseObj']['repMap']['list']:79# item['title_name'] = data['projectName']80# item[81# 'title_url'] = f'https://zwfw.mnr.gov.cn:8090/tlwserver/publicservices/bjgs/kq/yq_ck?id={data["id"]}'82# item['title_date'] = data['approvalDate']83# try:84# detail_json = self.get_json(item['title_url'])85# item['content_html'] = DataFormat().dictToHtml(detail_json['CkYqGsEntity'], self.name_dict)86# except TypeError:87# item['content_html'] = DataFormat().dictToHtml(data, self.name_dict)88# self.API().data_update(item)89#90# def get_json(self, url):91# self.driver.get(url=url)92# content_html = self.driver.page_source93# selector = Selector(text=content_html, type='html')94# html = selector.css('pre::text').get()95# o = xmltodict.parse(html)96# dumps_json = json.loads(json.dumps(o))97# return dumps_json98#99# def run(self):100# self.parse_kyqysl(101# url_list=[102# 'https://zwfw.mnr.gov.cn:8090/tlwserver/publicservices/bjgs/main?type=kyq&projectName=&licenseKey=&businessType=&obligee=&approvalDate=&approvalState=%E5%B7%B2%E5%8F%97%E7%90%86&' \103# f'pageNum={num}&pageSize=10' for num in range(1, 2)],104# item={105# "site_name": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",106# "title_type": "å½å®¶é¨å§",107# "site_path_url": "https://zwfw.mnr.gov.cn/flow/open?type=kyqysl",108# "site_path_name": "ç¿ä¸æç»è®°åçä¿¡æ¯å
¬å¼",109# "title_source": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",110# "site_id": "D29C4CA128",111# },112# )113#114# time.sleep(1)115#116# self.parse_bjgsKflyFags(117# url_list=[118# f'https://zwfw.mnr.gov.cn:8090/tlwserver/publicservices/bjgs/fags/initKflyFags?approvalDate=&pageNum={num}&pageSize=10×tamp=1660544155664'119# for num in range(1, 2)],120# item={121# "site_name": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",122# "site_path_url": 'https://zwfw.mnr.gov.cn:8090/bjgsKflyFags',123# "site_path_name": "ç¿äº§èµæºå¼åå©ç¨æ¹æ¡",124# "title_type": "å½å®¶é¨å§",125# "site_id": "08CE9E9B6D",126# "title_source": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",127# },128# )129# time.sleep(1)130# self.parse_kyqypz(131# url_list=[132# f'https://zwfw.mnr.gov.cn:8090/tlwserver/publicservices/bjgs/main?type=ckdyba&projectName=&licenseKey=&businessType=&obligee=&approvalDate=&approvalState=&pageNum={num}&pageSize=10×tamp=1660542992028'133# for num in range(1, 2)],134# item={135# "site_name": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",136# "site_path_url": 'https://zwfw.mnr.gov.cn:8090/bjgs?type=kyqypz',137# "site_path_name": "ç¿ä¸æç»è®°åçç»æå
¬å¼",138# "title_type": "å½å®¶é¨å§",139# "site_id": "66331CB7FF",140# "title_source": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",141# },142# )143# time.sleep(1)144# # ææ¶æ²¡çæ145# self.parse_kyqypz(146# url_list=[147# f'https://zwfw.mnr.gov.cn:8090/tlwserver/publicservices/bjgs/main?type=ckdyba&projectName=&licenseKey=&businessType=&obligee=&approvalDate=&approvalState=&pageNum={num}&pageSize=10×tamp=1661147296701'148# for num in range(1, 2)],149# item={150# "site_name": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",151# "site_path_url": 'https://zwfw.mnr.gov.cn:8090/bjgs?type=ckdyba',152# "site_path_name": "ç¿ä¸ææµæ¼å¤æ¡",153# "title_type": "å½å®¶é¨å§",154# "site_id": "AA664FB952",155# "title_source": "èªç¶èµæºé¨æ¿å¡æå¡é¨æ·",156# },157# )158#159# self.spider.close()160#161#162# if __name__ == '__main__':...
json_demo.py
Source:json_demo.py
1import json2books = [3 {4 'title': 'é¢éæ¯ææ ·ç»æç',5 'price': 9.86 },7 {8 'title': '红楼梦',9 'price': 9.910 }11]12dumps_json = json.dumps(books, ensure_ascii=False)13print(dumps_json)14load_json = json.loads(dumps_json)...
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!!