def genOutput(img_path, output_dir, bname): I = cv2.imread(img_path) detected_cars_labels = '%s/%s_cars.txt' % (output_dir, bname) Lcar = lread(detected_cars_labels) sys.stdout.write('%s' % bname) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) if isfile(lp_label): Llp_shapes = readShapes(lp_label) pts = Llp_shapes[0].pts * lcar.wh().reshape( 2, 1) + lcar.tl().reshape(2, 1) ptspx = pts * np.array(I.shape[1::-1], dtype=float).reshape( 2, 1) draw_losangle(I, ptspx, RED, 3) if isfile(lp_label_str): with open(lp_label_str, 'r') as f: lp_str = f.read().strip() llp = Label(0, tl=pts.min(1), br=pts.max(1)) write2img(I, llp, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
def draw_without_car(self, img, Llp_shapes, Llp_str, wh, tl): for i in range(len(Llp_shapes)): if Llp_shapes[i] is not None: pts = Llp_shapes[i].pts * wh.reshape(2, 1) + tl.reshape(2, 1) ptspx = pts * np.array(img.shape[1::-1], dtype=float).reshape( 2, 1) draw_losangle(img, ptspx, RED, 3) if Llp_str[i] is not None: llp = Label(0, tl=pts.min(1), br=pts.max(1)) write2img(img, llp, Llp_str[i]) return img
def gen_output(output_dir): YELLOW = (0, 255, 255) RED = (0, 0, 255) bname = splitext(basename(img_path))[0] I = cv2.imread(img_path) detected_cars_labels = '%s/%s_cars.txt' % (output_dir, bname) Lcar = lread(detected_cars_labels) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) C = cv2.imread('%s/%s_%dcar.png' % (output_dir, bname, i)) if isfile(lp_label): Llp = lread(lp_label) for j, llp in enumerate(Llp): draw_label(I, llp, color=RED, thickness=3, lp=True, lcar=lcar, C=C) if isfile(lp_label_str): with open(lp_label_str, 'r') as f: lp_str = f.read().strip() cwh = np.array(C.shape[1::-1]).astype(float) iwh = np.array(I.shape[1::-1]).astype(float) tl = tuple( np.add((llp.tl() * cwh).tolist(), (lcar.tl() * iwh).tolist()) / iwh) br = tuple( np.add((llp.br() * cwh).tolist(), (lcar.tl() * iwh).tolist()) / iwh) temp_label = Label(0, tl=tl, br=br) write2img(I, temp_label, lp_str) return I
def draw(self, img, Lcars, Llp_shapes, Llp_str): for i, lcar in enumerate(Lcars): draw_label(img, lcar, color=YELLOW, thickness=3) if Llp_shapes[i] is not None: # print("Llp.shapes[i].pts: ", Llp_shapes[i].pts) pts = Llp_shapes[i].pts * lcar.wh().reshape( 2, 1) + lcar.tl().reshape(2, 1) # print("pts: ", pts) ptspx = pts * np.array(img.shape[1::-1], dtype=float).reshape( 2, 1) # print("ptspx: ", ptspx) draw_losangle(img, ptspx, RED, 3) if Llp_str[i] is not None: llp = Label(0, tl=pts.min(1), br=pts.max(1)) write2img(img, llp, Llp_str[i]) return img
def generate_outputs(input_dir, output_dir): img_files = image_files_from_folder(input_dir) for img_file in img_files: bname = splitext(basename(img_file))[0] I = cv2.imread(img_file) detected_cars_labels = '%s/%s_cars.txt' % (output_dir, bname) Lcar = lread(detected_cars_labels) sys.stdout.write('%s' % bname) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) if isfile(lp_label): Llp_shapes = readShapes(lp_label) pts = Llp_shapes[0].pts * lcar.wh().reshape(2, 1) + lcar.tl().reshape(2, 1) ptspx = pts * np.array(I.shape[1::-1], dtype=float).reshape(2, 1) draw_losangle(I, ptspx, RED, 3) if isfile(lp_label_str): with open(lp_label_str, 'r') as f: lp_str = f.read().strip() llp = Label(0, tl=pts.min(1), br=pts.max(1)) write2img(I, llp, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I)
def draw_car(img, Lcars, Tcars, Ccars, IDcars, Llps, lp_strs): print ('Performing outputting car ...') YELLOW = ( 0,255,255) RED = ( 0, 0,255) WHITE = (255,255,255) I = img if Lcars: for i,(lcar, tcar, ccar, idcar) in enumerate(zip(Lcars, Tcars, Ccars, IDcars)): draw_label(I,lcar,color=YELLOW,thickness=3) if (ccar is not np.nan): car_cat = str(tcar.decode("utf-8")) + " " + ccar + " " + str(idcar) else: car_cat = str(tcar.decode("utf-8")) + " " + str(idcar) draw_text(I,lcar,car_cat,color=YELLOW,thickness=1) lp_label = Llps[i] lp_label_str = lp_strs[i] if (lp_label is not np.nan): pts = lp_label.pts*lcar.wh().reshape(2,1) + lcar.tl().reshape(2,1) ptspx = pts*np.array(I.shape[1::-1],dtype=float).reshape(2,1) draw_losangle(I,ptspx,RED,3) if (lp_label_str is not np.nan): lp_str = lp_label_str.strip() llp = Label(0,tl=pts.min(1),br=pts.max(1)) write2img(I,llp,lp_str) sys.stdout.write('%s\n' % lp_str) else: if (lp_label_str is not np.nan): lp_str = lp_label_str.strip() draw_text2(I,lcar,lp_str,color=WHITE,thickness=3) sys.stdout.write('%s\n' % lp_str) return I
sys.stdout.write('%s' % bname) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) if isfile(lp_label): Llp_shapes = readShapes(lp_label) pts = Llp_shapes[0].pts * lcar.wh().reshape( 2, 1) + lcar.tl().reshape(2, 1) ptspx = pts * np.array(I.shape[1::-1], dtype=float).reshape( 2, 1) draw_losangle(I, ptspx, RED, 3) if isfile(lp_label_str): with open(lp_label_str, 'r', encoding='utf-8') as f: lp_str = f.read().strip() llp = Label(0, tl=pts.min(1), br=pts.max(1)) I = write2img(I, llp, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I) sys.stdout.write('\n')
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) sys.exit(0)
print(width, height) if len(R2): L = dknet_label_conversion(R2, width, height) L = nms(L, .45) L.sort(key=lambda x: x.tl()[0]) lp_str = ''.join([chr(l.cl()) for l in L]) # ocr识别出的车牌字符串 #with open('%s/%s_str.txt' % (output_dir,bname),'w') as f: # f.write(lp_str + '\n') print('\t\tLP: %s' % lp_str) label_lp = Label(0, tl=lpts.min(1), br=lpts.max(1)) write2img(Iorig, label_lp, lp_str) else: print('No characters found') cv2.imshow("test.vedio", Iorig) cv2.waitKey(10) #lwrite('%s/%s_cars.txt' % (output_dir,bname),Lcars) except: traceback.print_exc() sys.exit(1) sys.exit(0)
C = cv2.imread('%s/%s_%dcar.png' % (output_dir, bname, i)) if isfile(lp_label): Llp = lread(lp_label) for j, llp in enumerate(Llp): draw_label(I, llp, color=RED, thickness=3, lp=True, lcar=lcar, C=C) if isfile(lp_label_str): with open(lp_label_str, 'r') as f: lp_str = f.read().strip() cwh = np.array(C.shape[1::-1]).astype(float) iwh = np.array(I.shape[1::-1]).astype(float) tl = tuple( np.add((llp.tl() * cwh).tolist(), (lcar.tl() * iwh).tolist()) / iwh) br = tuple( np.add((llp.br() * cwh).tolist(), (lcar.tl() * iwh).tolist()) / iwh) temp_label = Label(0, tl=tl, br=br) write2img(I, temp_label, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I) sys.stdout.write('\n')