for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) print(tl.shape) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Icar = crop_region(Iorig, label) Lcars.append(label) I = Iorig wh = np.array(I.shape[1::-1]) ch = I.shape[2] if len(I.shape) == 3 else 1 tl = np.floor(label.tl() * wh).astype(int) br = np.ceil(label.br() * wh).astype(int) outwh = br - tl # if np.prod(outwh) == 0.: # return None outsize = (outwh[1], outwh[0], ch) if ch > 1 else (outwh[1], outwh[0]) if (np.array(outsize) < 0).any(): pause() Iout = np.zeros(outsize, dtype=I.dtype) + 0.5 offset = np.minimum(tl, 0) * (-1) tl = np.maximum(tl, 0) br = np.minimum(br, wh)
print('\t\t%d cars found' % len(R)) if len(R): Iorig = frame # 原始帧图片 WH = np.array(Iorig.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) print(label.wh()) print(label.tl()) Icar = crop_region(Iorig, label) # 截取车辆区域 Icar 车辆 label 车辆坐标信息 Icar = Icar.astype('uint8') Lcars.append(label) draw_label(Iorig, label, color=YELLOW, thickness=3) # lp detector print('Searching for license plates using WPOD-NET') ratio = float(max(Icar.shape[:2])) / min(Icar.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio))
br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Lcars.append(label) Icar = crop_region(arr, label) # print('Searching for license plates using WPOD-NET') ratio = float(max(Icar.shape[:2])) / min(Icar.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2 ** 4)), 608) # print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(wpod_net, Icar / 255, bound_dim, 2 ** 4, (240, 80), 0.5) if len(LlpImgs): Ilp = LlpImgs[0] res, confidence = ocrmodel.recognizeOneframe(Ilp * 255.) pts = Llp[0].pts * label.wh().reshape(2, 1) + label.tl().reshape(2, 1) ptspx = pts * np.array(arr.shape[1::-1], dtype=float).reshape(2, 1) draw_losangle(arr, ptspx, RED, 3) if confidence > 0.5: llp = Label(0, tl=pts.min(1), br=pts.max(1)) arr = write2img(arr, llp, res) for i, lcar in enumerate(Lcars): draw_label(arr, lcar, color=YELLOW, thickness=3) videoWriter.write(arr) print('finish writing %d frame!' % frame) frame = frame + 1 videoWriter.release() except: traceback.print_exc() sys.exit(1)
def main(indir, lpdir, imgdir, name_file, outdir): print("##Convert annotation function") indir = os.path.expanduser(indir) lpdir = os.path.expanduser(lpdir) imgdir = os.path.expanduser(imgdir) outdir = os.path.expanduser(outdir) if os.path.exists(outdir): os.system('rm -rf ' + outdir) os.system('mkdir -p ' + outdir) name_file = os.path.expanduser(name_file) with open(name_file) as f: names = f.readlines() print(names) print('sorted(names)', sorted(names)) name2idx = { name.rstrip().upper(): i for i, name in enumerate(sorted(names)) } print('name2idx = ', name2idx) for root, _, file_names in os.walk(indir): print('#Root:', root) print('#file_names: ', file_names) root_part_path = os.path.relpath(root, indir) print('#root_part_path: ', root_part_path) print(file_names) new_root = os.path.join(outdir, root_part_path) try: os.mkdir(new_root) except: pass des_file_names = list(filter(lambda x: x.endswith('.txt'), file_names)) des_file_paths = [os.path.join(root, e) for e in des_file_names] for des_file_name, des_file_path in zip(des_file_names, des_file_paths): bfile_name = os.path.splitext(des_file_name)[0] print('bfile_name: ', bfile_name) actual_strgs = [] with open(des_file_path) as f: lines = f.readlines() print(lines) for line in lines: line = line.replace("-", "") print('line=', line) colums = line.split(',') raw_strg = colums[9] actual_strg = raw_strg.split('.')[0] print('actual_strg: ', actual_strg) actual_strgs.append(actual_strg) xml_file_paths = sorted( glob.glob( os.path.join(lpdir, root_part_path, bfile_name + '_?.xml'))) xml_file_names = [ os.path.basename(path) for path in xml_file_paths ] print('nxml_file_names', len(xml_file_names)) for xml_file_name, xml_file_path, actual_strg in zip( xml_file_names, xml_file_paths, actual_strgs): xml_bfile_name = os.path.splitext(xml_file_name)[0] print('xml_bfile_name: ', xml_bfile_name) print( "os.path.join(imgdir, root_part_path, xml_bfile_name + '.jpg' = ", os.path.join(imgdir, root_part_path, xml_bfile_name + '.jpg')) print("os.path.join(outdir, root_part_path) = ", os.path.join(outdir, root_part_path)) os.system('cp ' + os.path.join(imgdir, root_part_path, xml_bfile_name + '.jpg') + ' ' + os.path.join(outdir, root_part_path)) tree = ET.parse(xml_file_path) root = tree.getroot() size = root[4] width, height = [int(e.text) for e in size[:2]] yolo_file_name = os.path.join(outdir, root_part_path, xml_bfile_name + '.txt') with open(yolo_file_name, 'w+') as f: L = [] for i, ob in enumerate(root.findall('object')): name, bndbox = ob[0], ob[4] print('name.text, bndbox', name.text, bndbox) xmin, ymin, xmax, ymax = [ int(e.text) for e in bndbox[:] ] print('xmin, ymin, xmax, ymax: ', xmin, ymin, xmax, ymax) l = Label() l.set_tl(np.array([xmin, ymin])) l.set_br(np.array([xmax, ymax])) L.append(l) print('len(L): ', len(L)) L_pair = list(zip(L, [(width, height)] * len(L))) L_pair = sorted(L_pair, cmp=numeric_compare) L, _ = zip(*L_pair) print('len(L): ', len(L)) for i, l in enumerate(L): print('i: ', i) (xmin, ymin), (xmax, ymax) = l.tl(), l.br() yolo_location = [(xmin + xmax) / 2 / width, (ymin + ymax) / 2 / height, (xmax - xmin) / width, (ymax - ymin) / height] yolo_strg = str( name2idx[actual_strg[i]]) + ' ' + ' '.join( ["%.6f" % round(e, 6) for e in yolo_location]) print('i, yolo_strg: ', i, yolo_strg) if i < len(root.findall('object')) - 1: yolo_strg += '\n' f.write(yolo_strg)