Beispiel #1
0
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 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
Beispiel #4
0
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)
Beispiel #5
0
def labelImgShow():

	YELLOW = (  0,255,255)
	RED    = (  0,  0,255)

	img_file = '/home/yaokun/code/alpr-unconstrained/samples/train-detector/00011.jpg'
	lp_label = '/home/yaokun/code/alpr-unconstrained/samples/train-detector/00011.txt'

	I = cv2.imread(img_file)

	if isfile(lp_label):
		#pt1 = tuple(pts[:,i].astype(int).tolist())
		#pt2 = tuple(pts[:,(i+1)%4].astype(int).tolist())
		#cv2.line(I,pt1,pt2,color,thickness)
		Llp_shapes = readShapes(lp_label)
		print(np.array(I.shape[1::-1]).reshape(2,1))
		print(Llp_shapes[0].pts)
		pts = Llp_shapes[0].pts*np.array(I.shape[1::-1],dtype=float).reshape(2,1)
		print(pts)
		#ptspx = pts*np.array(I.shape[1::-1],dtype=float).reshape(2,1)
		draw_losangle(I,pts,RED,3)
		show(I)
Beispiel #6
0
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')
Beispiel #8
0
                    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)
Beispiel #9
0
                    if len(LlpImgs):
                        # 检测的车牌图像信息
                        Ilp = LlpImgs[0]
                        Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY)
                        Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR)

                        s = Shape(Llp[0].pts)  # 车牌坐标信息

                        #cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.)
                        #writeShapes('%s/%s_lp.txt' % (output_dir,bname),[s])
                        lpts = Llp[0].pts * label.wh().reshape(
                            2, 1) + label.tl().reshape(2, 1)
                        lptspx = lpts * np.array(Iorig.shape[1::-1],
                                                 dtype=float).reshape(2, 1)
                        draw_losangle(Iorig, lptspx, RED, 3)

                        Ilp = Ilp * 255.
                        Ilp = Ilp.astype('uint8')  # 十分重要,不做这个转换,就无法识别
                        '''
						cv2.imshow("img", Ilp)
						cv2.waitKey(3000)
						cv2.imwrite('%s/%s_new_lp.png' % (output_dir,bname),Ilp)	
						'''
                        dk_image = nparray_to_image(Ilp)

                        R2, (width, height) = detect(ocr_net,
                                                     ocr_meta,
                                                     dk_image,
                                                     thresh=ocr_threshold,
                                                     nms=None,