def crop_rectify_GT(dic): img = cv2.imread(os.path.join(img_path, dic['img_name'])) height, width, channel = img.shape carplate_x_top_left = int(dic['carplate_x_top_left']) carplate_y_top_left = int(dic['carplate_y_top_left']) carplate_x_top_right = int(dic['carplate_x_top_right']) carplate_y_top_right = int(dic['carplate_y_top_right']) carplate_x_bottom_right = int(dic['carplate_x_bottom_right']) carplate_y_bottom_right = int(dic['carplate_y_bottom_right']) carplate_x_bottom_left = int(dic['carplate_x_bottom_left']) carplate_y_bottom_left = int(dic['carplate_y_bottom_left']) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1, height) # bbox carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) # GT cv2.imwrite(os.path.join(GT_target_dir, dic['img_name']), img[carplate_ymin:carplate_ymax, carplate_xmin:carplate_xmax]) # GT_rectified warp_width = math.sqrt( math.pow(carplate_x_top_left - carplate_x_top_right, 2) + math.pow(carplate_y_top_left - carplate_y_top_right, 2)) warp_height = math.sqrt( math.pow(carplate_x_top_left - carplate_x_bottom_left, 2) + math.pow(carplate_y_top_left - carplate_y_bottom_left, 2)) pts1 = np.float32([[carplate_xmin, carplate_ymin+int(warp_height)], [carplate_xmin+int(warp_width), carplate_ymin+int(warp_height)], \ [carplate_xmin, carplate_ymin], [carplate_xmin+int(warp_width), carplate_ymin]]) pts2 = np.float32([[carplate_x_bottom_left, carplate_y_bottom_left], [carplate_x_bottom_right, carplate_y_bottom_right], \ [carplate_x_top_left, carplate_y_top_left], [carplate_x_top_right, carplate_y_top_right]]) M = cv2.getPerspectiveTransform(pts2, pts1) dst = cv2.warpPerspective(img, M, (width, height)) carplate_crop = dst[carplate_ymin:carplate_ymin + int(warp_height), carplate_xmin:carplate_xmin + int(warp_width)] cv2.imwrite(os.path.join(GT_rectified_target_dir, dic['img_name']), carplate_crop)
def gen_car_and_carplate_independently(fileitem): num_car = 0 num_carplate = 0 dom = xml.dom.minidom.parse(os.path.join(root_dir, fileitem)) root = dom.documentElement # size size = root.getElementsByTagName('size')[0] width = int(size.getElementsByTagName('width')[0].childNodes[0].nodeValue) height = int( size.getElementsByTagName('height')[0].childNodes[0].nodeValue) depth = int(size.getElementsByTagName('depth')[0].childNodes[0].nodeValue) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('car carplate')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild(doc.createTextNode(fileitem.split('.')[0] + '.jpg')) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('car carplate')) source.appendChild(database_) # 创建size节点 size = doc.createElement('size') annotation.appendChild(size) width_ = doc.createElement('width') width_.appendChild(doc.createTextNode(str(width))) height_ = doc.createElement('height') height_.appendChild(doc.createTextNode(str(height))) depth_ = doc.createElement('depth') depth_.appendChild(doc.createTextNode(str(depth))) size.appendChild(width_) size.appendChild(height_) size.appendChild(depth_) # segmentation segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented.appendChild(doc.createTextNode(str(0))) # notice object and obj objects = root.getElementsByTagName('object') for index, obj in enumerate(objects): bndbox = obj.getElementsByTagName('bndbox')[0] misc = int(obj.getElementsByTagName('misc')[0].childNodes[0].nodeValue) if need_car and not misc: # 不要misc的车辆 num_car += 1 car_xmin = int( bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue) car_ymin = int( bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue) car_xmax = int( bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue) car_ymax = int( bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue) # 限制不超过图片上下界 car_xmin = ex.limit_in_bounds(car_xmin, 1, width) car_ymin = ex.limit_in_bounds(car_ymin, 1, height) car_xmax = ex.limit_in_bounds(car_xmax, 1, width) car_ymax = ex.limit_in_bounds(car_ymax, 1, height) IsOccluded = int( obj.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) IsTruncated = int( obj.getElementsByTagName('truncated') [0].childNodes[0].nodeValue) IsDifficult = int( obj.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) # car object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode('car')) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode(str(IsTruncated))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode(str(IsDifficult))) object.appendChild(difficult_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(car_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(car_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(car_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(car_ymax))) bndbox.appendChild(ymax_) # 003786.xml是一辆车两个车牌 if need_carplate: carplates = obj.getElementsByTagName('carplate') if len(carplates) == 0: continue else: for i in range(len(carplates)): num_carplate += 1 carplate = carplates[i] carplate_x_top_left = round( float( carplate.getElementsByTagName('x_top_left') [0].childNodes[0].nodeValue)) carplate_y_top_left = round( float( carplate.getElementsByTagName('y_top_left') [0].childNodes[0].nodeValue)) carplate_x_top_right = round( float( carplate.getElementsByTagName('x_top_right') [0].childNodes[0].nodeValue)) carplate_y_top_right = round( float( carplate.getElementsByTagName('y_top_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_right = round( float( carplate.getElementsByTagName('x_bottom_right') [0].childNodes[0].nodeValue)) carplate_y_bottom_right = round( float( carplate.getElementsByTagName('y_bottom_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_left = round( float( carplate.getElementsByTagName('x_bottom_left') [0].childNodes[0].nodeValue)) carplate_y_bottom_left = round( float( carplate.getElementsByTagName('y_bottom_left') [0].childNodes[0].nodeValue)) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds( carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds( carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds( carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds( carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds( carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds( carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds( carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds( carplate_y_bottom_left, 1, height) carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) IsOccluded = int( carplate.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) IsDifficult = int( carplate.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) # carplate object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode("carplate")) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode(str(0))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode( str(IsDifficult))) object.appendChild(difficult_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(carplate_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(carplate_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(carplate_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(carplate_ymax))) bndbox.appendChild(ymax_) x_top_left_ = doc.createElement('x_top_left') x_top_left_.appendChild( doc.createTextNode(str(carplate_x_top_left))) bndbox.appendChild(x_top_left_) y_top_left_ = doc.createElement('y_top_left') y_top_left_.appendChild( doc.createTextNode(str(carplate_y_top_left))) bndbox.appendChild(y_top_left_) x_top_right_ = doc.createElement('x_top_right') x_top_right_.appendChild( doc.createTextNode(str(carplate_x_top_right))) bndbox.appendChild(x_top_right_) y_top_right_ = doc.createElement('y_top_right') y_top_right_.appendChild( doc.createTextNode(str(carplate_y_top_right))) bndbox.appendChild(y_top_right_) x_bottom_right_ = doc.createElement('x_bottom_right') x_bottom_right_.appendChild( doc.createTextNode(str(carplate_x_bottom_right))) bndbox.appendChild(x_bottom_right_) y_bottom_right_ = doc.createElement('y_bottom_right') y_bottom_right_.appendChild( doc.createTextNode(str(carplate_y_bottom_right))) bndbox.appendChild(y_bottom_right_) x_bottom_left_ = doc.createElement('x_bottom_left') x_bottom_left_.appendChild( doc.createTextNode(str(carplate_x_bottom_left))) bndbox.appendChild(x_bottom_left_) y_bottom_left_ = doc.createElement('y_bottom_left') y_bottom_left_.appendChild( doc.createTextNode(str(carplate_y_bottom_left))) bndbox.appendChild(y_bottom_left_) if (need_car and num_car > 0) or (need_carplate and num_carplate > 0): fp = open(os.path.join(target_dir, fileitem), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
# 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1, height) # bbox carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right)
def crop_rectify_GT(fileitem): img = cv2.imread(os.path.join(root_dir, fileitem)) height = 1160 width = 720 depth = 3 # four corners four_corners = fileitem.split('-')[3] if len(four_corners.split('_')) != 4: print("wrong four corners") print(fileitem) carplate_x_bottom_right = int(four_corners.split('_')[0].split('&')[0]) carplate_y_bottom_right = int(four_corners.split('_')[0].split('&')[1]) carplate_x_bottom_left = int(four_corners.split('_')[1].split('&')[0]) carplate_y_bottom_left = int(four_corners.split('_')[1].split('&')[1]) carplate_x_top_left = int(four_corners.split('_')[2].split('&')[0]) carplate_y_top_left = int(four_corners.split('_')[2].split('&')[1]) carplate_x_top_right = int(four_corners.split('_')[3].split('&')[0]) carplate_y_top_right = int(four_corners.split('_')[3].split('&')[1]) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1, height) # bbox carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) # GT cv2.imwrite(os.path.join(GT_target_dir, fileitem), img[carplate_ymin:carplate_ymax, carplate_xmin:carplate_xmax]) # GT_rectified warp_width = math.sqrt( math.pow(carplate_x_top_left - carplate_x_top_right, 2) + math.pow(carplate_y_top_left - carplate_y_top_right, 2)) warp_height = math.sqrt( math.pow(carplate_x_top_left - carplate_x_bottom_left, 2) + math.pow(carplate_y_top_left - carplate_y_bottom_left, 2)) pts1 = np.float32([[carplate_xmin, carplate_ymin+int(warp_height)], [carplate_xmin+int(warp_width), carplate_ymin+int(warp_height)], \ [carplate_xmin, carplate_ymin], [carplate_xmin+int(warp_width), carplate_ymin]]) pts2 = np.float32([[carplate_x_bottom_left, carplate_y_bottom_left], [carplate_x_bottom_right, carplate_y_bottom_right], \ [carplate_x_top_left, carplate_y_top_left], [carplate_x_top_right, carplate_y_top_right]]) M = cv2.getPerspectiveTransform(pts2, pts1) dst = cv2.warpPerspective(img, M, (width, height)) carplate_crop = dst[carplate_ymin:carplate_ymin + int(warp_height), carplate_xmin:carplate_xmin + int(warp_width)] cv2.imwrite(os.path.join(GT_rectified_target_dir, fileitem), carplate_crop)
def CCPD_to_VOC(fileitem): # image info # img = cv2.imread(os.path.join(root_dir, fileitem)) # height, width, depth = img.shape height = 1160 width = 720 depth = 3 # check image size if height != 1160 or width != 720: print("wrong size") print(fileitem) # four corners four_corners = fileitem.split('-')[3] if len(four_corners.split('_')) != 4: print("wrong four corners") print(fileitem) carplate_x_bottom_right = int(four_corners.split('_')[0].split('&')[0]) + 1 carplate_y_bottom_right = int(four_corners.split('_')[0].split('&')[1]) + 1 carplate_x_bottom_left = int(four_corners.split('_')[1].split('&')[0]) + 1 carplate_y_bottom_left = int(four_corners.split('_')[1].split('&')[1]) + 1 carplate_x_top_left = int(four_corners.split('_')[2].split('&')[0]) + 1 carplate_y_top_left = int(four_corners.split('_')[2].split('&')[1]) + 1 carplate_x_top_right = int(four_corners.split('_')[3].split('&')[0]) + 1 carplate_y_top_right = int(four_corners.split('_')[3].split('&')[1]) + 1 # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1, height) # bbox carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) # 官方自带的bbox,视觉效果很不准,还是通过四点得到bbox # # bbox # bbox = fileitem.split('-')[2] # if len(bbox.split('_')) != 2: # print("wrong bbox") # print(fileitem) # carplate_xmin = int(bbox.split('_')[0].split('&')[0]) # carplate_ymin = int(bbox.split('_')[0].split('&')[1]) # carplate_xmax = int(bbox.split('_')[1].split('&')[0]) # carplate_ymax = int(bbox.split('_')[1].split('&')[1]) # # 限制不超过图片上下界 # carplate_xmin = ex.limit_in_bounds(carplate_xmin, 1, width) # carplate_ymin = ex.limit_in_bounds(carplate_ymin, 1, height) # carplate_xmax = ex.limit_in_bounds(carplate_xmax, 1, width) # carplate_ymax = ex.limit_in_bounds(carplate_ymax, 1, height) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('carplate')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild(doc.createTextNode(str(fileitem))) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('carplate')) source.appendChild(database_) # 创建size节点 size = doc.createElement('size') annotation.appendChild(size) width_ = doc.createElement('width') width_.appendChild(doc.createTextNode(str(width))) height_ = doc.createElement('height') height_.appendChild(doc.createTextNode(str(height))) depth_ = doc.createElement('depth') depth_.appendChild(doc.createTextNode(str(depth))) size.appendChild(width_) size.appendChild(height_) size.appendChild(depth_) # segmentation segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented.appendChild(doc.createTextNode(str(0))) # carplate object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode("carplate")) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(0))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode(str(0))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode(str(0))) object.appendChild(difficult_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(carplate_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(carplate_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(carplate_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(carplate_ymax))) bndbox.appendChild(ymax_) x_top_left_ = doc.createElement('x_top_left') x_top_left_.appendChild(doc.createTextNode(str(carplate_x_top_left))) bndbox.appendChild(x_top_left_) y_top_left_ = doc.createElement('y_top_left') y_top_left_.appendChild(doc.createTextNode(str(carplate_y_top_left))) bndbox.appendChild(y_top_left_) x_top_right_ = doc.createElement('x_top_right') x_top_right_.appendChild(doc.createTextNode(str(carplate_x_top_right))) bndbox.appendChild(x_top_right_) y_top_right_ = doc.createElement('y_top_right') y_top_right_.appendChild(doc.createTextNode(str(carplate_y_top_right))) bndbox.appendChild(y_top_right_) x_bottom_right_ = doc.createElement('x_bottom_right') x_bottom_right_.appendChild( doc.createTextNode(str(carplate_x_bottom_right))) bndbox.appendChild(x_bottom_right_) y_bottom_right_ = doc.createElement('y_bottom_right') y_bottom_right_.appendChild( doc.createTextNode(str(carplate_y_bottom_right))) bndbox.appendChild(y_bottom_right_) x_bottom_left_ = doc.createElement('x_bottom_left') x_bottom_left_.appendChild(doc.createTextNode(str(carplate_x_bottom_left))) bndbox.appendChild(x_bottom_left_) y_bottom_left_ = doc.createElement('y_bottom_left') y_bottom_left_.appendChild(doc.createTextNode(str(carplate_y_bottom_left))) bndbox.appendChild(y_bottom_left_) # save xml fp = open(os.path.join(target_dir, fileitem[:-4] + ".xml"), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
def gen_carplate_size(fileitem): dom = xml.dom.minidom.parse(os.path.join(root_xml_dir, fileitem)) root = dom.documentElement # size size = root.getElementsByTagName('size')[0] width = int(size.getElementsByTagName('width')[0].childNodes[0].nodeValue) height = int(size.getElementsByTagName('height')[0].childNodes[0].nodeValue) depth = int(size.getElementsByTagName('depth')[0].childNodes[0].nodeValue) # iteration objects = root.getElementsByTagName('object') for index, obj in enumerate(objects): bndbox = obj.getElementsByTagName('bndbox')[0] carplate_x_top_left = round(float(bndbox.getElementsByTagName('x_top_left')[0].childNodes[0].nodeValue)) carplate_y_top_left = round(float(bndbox.getElementsByTagName('y_top_left')[0].childNodes[0].nodeValue)) carplate_x_top_right = round(float(bndbox.getElementsByTagName('x_top_right')[0].childNodes[0].nodeValue)) carplate_y_top_right = round(float(bndbox.getElementsByTagName('y_top_right')[0].childNodes[0].nodeValue)) carplate_x_bottom_right = round(float(bndbox.getElementsByTagName('x_bottom_right')[0].childNodes[0].nodeValue)) carplate_y_bottom_right = round(float(bndbox.getElementsByTagName('y_bottom_right')[0].childNodes[0].nodeValue)) carplate_x_bottom_left = round(float(bndbox.getElementsByTagName('x_bottom_left')[0].childNodes[0].nodeValue)) carplate_y_bottom_left = round(float(bndbox.getElementsByTagName('y_bottom_left')[0].childNodes[0].nodeValue)) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1, height) carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) QP = np.array([carplate_x_top_left - carplate_x_bottom_left, carplate_y_top_left - carplate_y_bottom_left]) v = np.array([carplate_x_bottom_left - carplate_x_bottom_right, carplate_y_bottom_left - carplate_y_bottom_right]) h = np.linalg.norm(np.cross(QP, v))/np.linalg.norm(v) # save image img = cv2.imread(os.path.join(root_img_dir, fileitem.split('.')[0] + '.jpg')) roi = img[carplate_ymin:carplate_ymax, carplate_xmin:carplate_xmax, :] if h <= 16: cv2.imwrite(os.path.join(target_img_dir + 'small', fileitem.split('.')[0] + '_' + str(index + 1) + '.jpg'), roi) elif h > 16 and h <= 32: cv2.imwrite(os.path.join(target_img_dir + 'medium', fileitem.split('.')[0] + '_' + str(index + 1) + '.jpg'), roi) else: cv2.imwrite(os.path.join(target_img_dir + 'large', fileitem.split('.')[0] + '_' + str(index + 1) + '.jpg'), roi)
def gen_car_carplate_two_stage_end2end(fileitem): num_car_with_carplate = 0 num_car_without_carplate = 0 dom = xml.dom.minidom.parse(os.path.join(root_dir, fileitem)) root = dom.documentElement # size size = root.getElementsByTagName('size')[0] width = int(size.getElementsByTagName('width')[0].childNodes[0].nodeValue) height = int( size.getElementsByTagName('height')[0].childNodes[0].nodeValue) depth = int(size.getElementsByTagName('depth')[0].childNodes[0].nodeValue) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('car_carplate_two_stage_end2end')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild(doc.createTextNode(fileitem.split('.')[0] + '.jpg')) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('car_carplate_two_stage_end2end')) source.appendChild(database_) # 创建size节点 size = doc.createElement('size') annotation.appendChild(size) width_ = doc.createElement('width') width_.appendChild(doc.createTextNode(str(width))) height_ = doc.createElement('height') height_.appendChild(doc.createTextNode(str(height))) depth_ = doc.createElement('depth') depth_.appendChild(doc.createTextNode(str(depth))) size.appendChild(width_) size.appendChild(height_) size.appendChild(depth_) # segmentation segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented.appendChild(doc.createTextNode(str(0))) # notice object and obj objects = root.getElementsByTagName('object') for index, obj in enumerate(objects): bndbox = obj.getElementsByTagName('bndbox')[0] misc = int(obj.getElementsByTagName('misc')[0].childNodes[0].nodeValue) if not misc: # 不要misc的车辆 car_xmin = int( bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue) car_ymin = int( bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue) car_xmax = int( bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue) car_ymax = int( bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue) # 限制不超过图片上下界 car_xmin = ex.limit_in_bounds(car_xmin, 1, width) car_ymin = ex.limit_in_bounds(car_ymin, 1, height) car_xmax = ex.limit_in_bounds(car_xmax, 1, width) car_ymax = ex.limit_in_bounds(car_ymax, 1, height) car_IsOccluded = int( obj.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) car_IsTruncated = int( obj.getElementsByTagName('truncated') [0].childNodes[0].nodeValue) car_IsDifficult = int( obj.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) car_x_center = (car_xmin + car_xmax) / 2 car_y_center = (car_ymin + car_ymax) / 2 # carplate carplates = obj.getElementsByTagName('carplate') if len(carplates) == 0: num_car_without_carplate += 1 # car object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode('car')) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(car_IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode( str(car_IsTruncated))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode( str(car_IsDifficult))) object.appendChild(difficult_) # dont have carplate has_carplate_ = doc.createElement('has_carplate') has_carplate_.appendChild(doc.createTextNode(str(0))) object.appendChild(has_carplate_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(car_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(car_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(car_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(car_ymax))) bndbox.appendChild(ymax_) elif len(carplates) > 0: num_car_with_carplate += 1 for i in range(len(carplates)): # car object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode('car')) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild( doc.createTextNode(str(car_IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild( doc.createTextNode(str(car_IsTruncated))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild( doc.createTextNode(str(car_IsDifficult))) object.appendChild(difficult_) # has carplate has_carplate_ = doc.createElement('has_carplate') has_carplate_.appendChild(doc.createTextNode(str(1))) object.appendChild(has_carplate_) # carplate carplate = carplates[i] carplate_IsOccluded = int( carplate.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) carplate_IsDifficult = int( carplate.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) carplate_IsUnrecognable = int( carplate.getElementsByTagName('unrecognable') [0].childNodes[0].nodeValue) # occluded occluded_ = doc.createElement('carplate_occluded') occluded_.appendChild( doc.createTextNode(str(carplate_IsOccluded))) object.appendChild(occluded_) # difficult difficult_ = doc.createElement('carplate_difficult') difficult_.appendChild( doc.createTextNode(str(carplate_IsDifficult))) object.appendChild(difficult_) # unrecognable unrecognable_ = doc.createElement('carplate_unrecognable') unrecognable_.appendChild( doc.createTextNode(str(carplate_IsUnrecognable))) object.appendChild(unrecognable_) carplate_x_top_left = round( float( carplate.getElementsByTagName('x_top_left') [0].childNodes[0].nodeValue)) carplate_y_top_left = round( float( carplate.getElementsByTagName('y_top_left') [0].childNodes[0].nodeValue)) carplate_x_top_right = round( float( carplate.getElementsByTagName('x_top_right') [0].childNodes[0].nodeValue)) carplate_y_top_right = round( float( carplate.getElementsByTagName('y_top_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_right = round( float( carplate.getElementsByTagName('x_bottom_right') [0].childNodes[0].nodeValue)) carplate_y_bottom_right = round( float( carplate.getElementsByTagName('y_bottom_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_left = round( float( carplate.getElementsByTagName('x_bottom_left') [0].childNodes[0].nodeValue)) carplate_y_bottom_left = round( float( carplate.getElementsByTagName('y_bottom_left') [0].childNodes[0].nodeValue)) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds( carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds( carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds( carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds( carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds( carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds( carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds( carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds( carplate_y_bottom_left, 1, height) carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) carplate_x_center = (carplate_xmin + carplate_xmax) / 2 carplate_y_center = (carplate_ymin + carplate_ymax) / 2 # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(car_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(car_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(car_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(car_ymax))) bndbox.appendChild(ymax_) # carplate size and offset width_ = doc.createElement('width') width_.appendChild( doc.createTextNode(str(carplate_xmax - carplate_xmin))) bndbox.appendChild(width_) height_ = doc.createElement('height') height_.appendChild( doc.createTextNode(str(carplate_ymax - carplate_ymin))) bndbox.appendChild(height_) x_offset_ = doc.createElement('x_offset') x_offset_.appendChild( doc.createTextNode( str(carplate_x_center - car_x_center))) bndbox.appendChild(x_offset_) y_offset_ = doc.createElement('y_offset') y_offset_.appendChild( doc.createTextNode( str(carplate_y_center - car_y_center))) bndbox.appendChild(y_offset_) # carplate bbox carplate_xmin_ = doc.createElement('carplate_xmin') carplate_xmin_.appendChild( doc.createTextNode(str(carplate_xmin))) bndbox.appendChild(carplate_xmin_) carplate_ymin_ = doc.createElement('carplate_ymin') carplate_ymin_.appendChild( doc.createTextNode(str(carplate_ymin))) bndbox.appendChild(carplate_ymin_) carplate_xmax_ = doc.createElement('carplate_xmax') carplate_xmax_.appendChild( doc.createTextNode(str(carplate_xmax))) bndbox.appendChild(carplate_xmax_) carplate_ymax_ = doc.createElement('carplate_ymax') carplate_ymax_.appendChild( doc.createTextNode(str(carplate_ymax))) bndbox.appendChild(carplate_ymax_) # carplate four points carplate_x_top_left_ = doc.createElement( 'carplate_x_top_left') carplate_x_top_left_.appendChild( doc.createTextNode(str(carplate_x_top_left))) bndbox.appendChild(carplate_x_top_left_) carplate_y_top_left_ = doc.createElement( 'carplate_y_top_left') carplate_y_top_left_.appendChild( doc.createTextNode(str(carplate_y_top_left))) bndbox.appendChild(carplate_y_top_left_) carplate_x_top_right_ = doc.createElement( 'carplate_x_top_right') carplate_x_top_right_.appendChild( doc.createTextNode(str(carplate_x_top_right))) bndbox.appendChild(carplate_x_top_right_) carplate_y_top_right_ = doc.createElement( 'carplate_y_top_right') carplate_y_top_right_.appendChild( doc.createTextNode(str(carplate_y_top_right))) bndbox.appendChild(carplate_y_top_right_) carplate_x_bottom_right_ = doc.createElement( 'carplate_x_bottom_right') carplate_x_bottom_right_.appendChild( doc.createTextNode(str(carplate_x_bottom_right))) bndbox.appendChild(carplate_x_bottom_right_) carplate_y_bottom_right_ = doc.createElement( 'carplate_y_bottom_right') carplate_y_bottom_right_.appendChild( doc.createTextNode(str(carplate_y_bottom_right))) bndbox.appendChild(carplate_y_bottom_right_) carplate_x_bottom_left_ = doc.createElement( 'carplate_x_bottom_left') carplate_x_bottom_left_.appendChild( doc.createTextNode(str(carplate_x_bottom_left))) bndbox.appendChild(carplate_x_bottom_left_) carplate_y_bottom_left_ = doc.createElement( 'carplate_y_bottom_left') carplate_y_bottom_left_.appendChild( doc.createTextNode(str(carplate_y_bottom_left))) bndbox.appendChild(carplate_y_bottom_left_) fp = open(os.path.join(target_dir, fileitem), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
def CLPD_to_VOC(dic): img_name = dic['img_name'] width = int(dic['width']) height = int(dic['height']) channel = dic['channel'] carplate_x_top_left = int(dic['carplate_x_top_left']) carplate_y_top_left = int(dic['carplate_y_top_left']) carplate_x_top_right = int(dic['carplate_x_top_right']) carplate_y_top_right = int(dic['carplate_y_top_right']) carplate_x_bottom_right = int(dic['carplate_x_bottom_right']) carplate_y_bottom_right = int(dic['carplate_y_bottom_right']) carplate_x_bottom_left = int(dic['carplate_x_bottom_left']) carplate_y_bottom_left = int(dic['carplate_y_bottom_left']) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1, height) # bbox carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('carplate')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild(doc.createTextNode(str(img_name))) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('carplate')) source.appendChild(database_) # 创建size节点 size = doc.createElement('size') annotation.appendChild(size) width_ = doc.createElement('width') width_.appendChild(doc.createTextNode(str(width))) height_ = doc.createElement('height') height_.appendChild(doc.createTextNode(str(height))) depth_ = doc.createElement('depth') depth_.appendChild(doc.createTextNode(str(channel))) size.appendChild(width_) size.appendChild(height_) size.appendChild(depth_) # segmentation segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented.appendChild(doc.createTextNode(str(0))) # carplate object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode("carplate")) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(0))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode(str(0))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode(str(0))) object.appendChild(difficult_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(carplate_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(carplate_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(carplate_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(carplate_ymax))) bndbox.appendChild(ymax_) x_top_left_ = doc.createElement('x_top_left') x_top_left_.appendChild(doc.createTextNode(str(carplate_x_top_left))) bndbox.appendChild(x_top_left_) y_top_left_ = doc.createElement('y_top_left') y_top_left_.appendChild(doc.createTextNode(str(carplate_y_top_left))) bndbox.appendChild(y_top_left_) x_top_right_ = doc.createElement('x_top_right') x_top_right_.appendChild(doc.createTextNode(str(carplate_x_top_right))) bndbox.appendChild(x_top_right_) y_top_right_ = doc.createElement('y_top_right') y_top_right_.appendChild(doc.createTextNode(str(carplate_y_top_right))) bndbox.appendChild(y_top_right_) x_bottom_right_ = doc.createElement('x_bottom_right') x_bottom_right_.appendChild( doc.createTextNode(str(carplate_x_bottom_right))) bndbox.appendChild(x_bottom_right_) y_bottom_right_ = doc.createElement('y_bottom_right') y_bottom_right_.appendChild( doc.createTextNode(str(carplate_y_bottom_right))) bndbox.appendChild(y_bottom_right_) x_bottom_left_ = doc.createElement('x_bottom_left') x_bottom_left_.appendChild(doc.createTextNode(str(carplate_x_bottom_left))) bndbox.appendChild(x_bottom_left_) y_bottom_left_ = doc.createElement('y_bottom_left') y_bottom_left_.appendChild(doc.createTextNode(str(carplate_y_bottom_left))) bndbox.appendChild(y_bottom_left_) # save xml fp = open(os.path.join(target_dir, img_name[:-4] + ".xml"), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8") # save test txt fin = open(test_path, 'a+') fin.write(img_name[:-4] + '\n') fin.close()
def expand_carplate(fileitem, n): dom = xml.dom.minidom.parse(os.path.join(root_xmls, fileitem)) root = dom.documentElement # size size = root.getElementsByTagName('size')[0] width = int(size.getElementsByTagName('width')[0].childNodes[0].nodeValue) height = int( size.getElementsByTagName('height')[0].childNodes[0].nodeValue) depth = int(size.getElementsByTagName('depth')[0].childNodes[0].nodeValue) # notice object and obj objects = root.getElementsByTagName('object') for index, obj in enumerate(objects): bndbox = obj.getElementsByTagName('bndbox')[0] car_xmin = int( bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue) car_ymin = int( bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue) car_xmax = int( bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue) car_ymax = int( bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue) # 限制不超过图片上下界 car_xmin = ex.limit_in_bounds(car_xmin, 1, width) car_ymin = ex.limit_in_bounds(car_ymin, 1, height) car_xmax = ex.limit_in_bounds(car_xmax, 1, width) car_ymax = ex.limit_in_bounds(car_ymax, 1, height) # 003786.xml是一辆车两个车牌 carplates = obj.getElementsByTagName('carplate') if len(carplates) == 0: continue else: # 如果只有一个车牌的情况,多于一个车牌直接跳过,不然容易造成干扰 if len(carplates) == 1: carplate = carplates[0] carplate_x_top_left = round( float( carplate.getElementsByTagName('x_top_left') [0].childNodes[0].nodeValue)) carplate_y_top_left = round( float( carplate.getElementsByTagName('y_top_left') [0].childNodes[0].nodeValue)) carplate_x_top_right = round( float( carplate.getElementsByTagName('x_top_right') [0].childNodes[0].nodeValue)) carplate_y_top_right = round( float( carplate.getElementsByTagName('y_top_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_right = round( float( carplate.getElementsByTagName('x_bottom_right') [0].childNodes[0].nodeValue)) carplate_y_bottom_right = round( float( carplate.getElementsByTagName('y_bottom_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_left = round( float( carplate.getElementsByTagName('x_bottom_left') [0].childNodes[0].nodeValue)) carplate_y_bottom_left = round( float( carplate.getElementsByTagName('y_bottom_left') [0].childNodes[0].nodeValue)) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds( carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds( carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds( carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds( carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds( carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds( carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds( carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds( carplate_y_bottom_left, 1, height) carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) carplate_width = carplate_xmax - carplate_xmin carplate_height = carplate_ymax - carplate_ymin for x in range(1, expand_num + 1): print("generate NO. " + str(x) + "'s data.") # 产生左上角随机偏移(xrand, yrand) xrand = int(random.uniform(0, (n - 1) * carplate_width)) yrand = int(random.uniform(0, (n - 1) * carplate_height)) roi_xmin = max(int(carplate_xmin - xrand), car_xmin) roi_ymin = max(int(carplate_ymin - yrand), car_ymin) roi_xmax = min(int(roi_xmin + n * carplate_width), car_xmax) roi_ymax = min(int(roi_ymin + n * carplate_height), car_ymax) roi_x_top_left = int(carplate_x_top_left) - roi_xmin roi_y_top_left = int(carplate_y_top_left) - roi_ymin roi_x_top_right = int(carplate_x_top_right) - roi_xmin roi_y_top_right = int(carplate_y_top_right) - roi_ymin roi_x_bottom_right = int( carplate_x_bottom_right) - roi_xmin roi_y_bottom_right = int( carplate_y_bottom_right) - roi_ymin roi_x_bottom_left = int(carplate_x_bottom_left) - roi_xmin roi_y_bottom_left = int(carplate_y_bottom_left) - roi_ymin carplate_xmin_new = min(roi_x_top_left, roi_x_bottom_left) carplate_ymin_new = min(roi_y_top_left, roi_y_top_right) carplate_xmax_new = max(roi_x_bottom_right, roi_x_top_right) carplate_ymax_new = max(roi_y_bottom_right, roi_y_bottom_left) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('carplate')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild( doc.createTextNode( fileitem.split('.')[0] + '_' + str(index + 1) + '_' + str(x) + '.jpg')) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('carplate')) source.appendChild(database_) # 创建size节点 size = doc.createElement('size') annotation.appendChild(size) width_ = doc.createElement('width') width_.appendChild( doc.createTextNode(str(roi_xmax - roi_xmin))) height_ = doc.createElement('height') height_.appendChild( doc.createTextNode(str(roi_ymax - roi_ymin))) depth_ = doc.createElement('depth') depth_.appendChild(doc.createTextNode(str(depth))) size.appendChild(width_) size.appendChild(height_) size.appendChild(depth_) # segmentation segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented.appendChild(doc.createTextNode(str(0))) IsOccluded = int( carplate.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) IsDifficult = int( carplate.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) # carplate object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode("carplate")) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode(str(0))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode( str(IsDifficult))) object.appendChild(difficult_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild( doc.createTextNode(str(carplate_xmin_new))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild( doc.createTextNode(str(carplate_ymin_new))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild( doc.createTextNode(str(carplate_xmax_new))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild( doc.createTextNode(str(carplate_ymax_new))) bndbox.appendChild(ymax_) x_top_left_ = doc.createElement('x_top_left') x_top_left_.appendChild( doc.createTextNode(str(roi_x_top_left))) bndbox.appendChild(x_top_left_) y_top_left_ = doc.createElement('y_top_left') y_top_left_.appendChild( doc.createTextNode(str(roi_y_top_left))) bndbox.appendChild(y_top_left_) x_top_right_ = doc.createElement('x_top_right') x_top_right_.appendChild( doc.createTextNode(str(roi_x_top_right))) bndbox.appendChild(x_top_right_) y_top_right_ = doc.createElement('y_top_right') y_top_right_.appendChild( doc.createTextNode(str(roi_y_top_right))) bndbox.appendChild(y_top_right_) x_bottom_right_ = doc.createElement('x_bottom_right') x_bottom_right_.appendChild( doc.createTextNode(str(roi_x_bottom_right))) bndbox.appendChild(x_bottom_right_) y_bottom_right_ = doc.createElement('y_bottom_right') y_bottom_right_.appendChild( doc.createTextNode(str(roi_y_bottom_right))) bndbox.appendChild(y_bottom_right_) x_bottom_left_ = doc.createElement('x_bottom_left') x_bottom_left_.appendChild( doc.createTextNode(str(roi_x_bottom_left))) bndbox.appendChild(x_bottom_left_) y_bottom_left_ = doc.createElement('y_bottom_left') y_bottom_left_.appendChild( doc.createTextNode(str(roi_y_bottom_left))) bndbox.appendChild(y_bottom_left_) # save xml fp = open( os.path.join( target_xmls, fileitem.split('.')[0] + '_' + str(index + 1) + '_' + str(x) + '.xml'), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8") # save image img = cv2.imread( os.path.join(root_imgs, fileitem.split('.')[0] + '.jpg')) roi = img[roi_ymin:roi_ymax, roi_xmin:roi_xmax, :] cv2.imwrite( os.path.join( target_imgs, fileitem.split('.')[0] + '_' + str(index + 1) + '_' + str(x) + '.jpg'), roi)