Exemplo n.º 1
0
def save_tf():
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config()

    input_layer = tf.keras.layers.Input(
        [FLAGS.input_size, FLAGS.input_size, 3])
    feature_maps = YOLOv4(input_layer, NUM_CLASS)
    bbox_tensors = []
    prob_tensors = []
    for i, fm in enumerate(feature_maps):
        if i == 0:
            output_tensors = decode_tf(fm, FLAGS.input_size // 8, NUM_CLASS,
                                       STRIDES, ANCHORS, i, XYSCALE)
        elif i == 1:
            output_tensors = decode_tf(fm, FLAGS.input_size // 16, NUM_CLASS,
                                       STRIDES, ANCHORS, i, XYSCALE)
        else:
            output_tensors = decode_tf(fm, FLAGS.input_size // 32, NUM_CLASS,
                                       STRIDES, ANCHORS, i, XYSCALE)
        bbox_tensors.append(output_tensors[0])
        prob_tensors.append(output_tensors[1])
    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)
    boxes, pred_conf = filter_boxes(pred_bbox,
                                    pred_prob,
                                    score_threshold=FLAGS.score_thres,
                                    input_shape=tf.constant(
                                        [FLAGS.input_size, FLAGS.input_size]))
    pred = tf.concat([boxes, pred_conf], axis=-1)
    model = tf.keras.Model(input_layer, pred)
    utils.load_weights(model, FLAGS.weights)
    model.summary()
    model.save(FLAGS.output)
def main(_argv):
    input_layer = tf.keras.layers.Input([FLAGS.size, FLAGS.size, 3])
    feature_maps = YOLOv4(input_layer, NUM_CLASS)
    bbox_tensors = []
    for i, fm in enumerate(feature_maps):
        bbox_tensor = decode(fm, NUM_CLASS, i)
        bbox_tensors.append(bbox_tensor)

    model = tf.keras.Model(input_layer, bbox_tensors)
    utils.load_weights(model, 'data/YOLOv4-obj_1000.weights')

    vid = cv2.VideoCapture(FLAGS.input)  # Reading input
    return_value, frame = vid.read()

    fourcc = cv2.VideoWriter_fourcc('F', 'M', 'P', '4')
    out = cv2.VideoWriter(FLAGS.output, fourcc, 10.0,
                          (frame.shape[1], frame.shape[0]), True)

    plates = []

    n = 0
    Sum = 0
    while True:
        start = time.time()
        n += 1
        return_value, frame = vid.read()
        if frame is None:
            continue

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        bboxes = plateDetect(frame, FLAGS.size,
                             model)  # License plate detection
        for i in range(len(bboxes)):
            img = frame[int(bboxes[i][1]):int(bboxes[i][3]),
                        int(bboxes[i][0]):int(bboxes[i][2])]
            prediction_groups = pipeline.recognize(
                [img])  # Text detection and recognition on license plate
            string = ''
            for j in range(len(prediction_groups[0])):
                string = string + prediction_groups[0][j][0].upper()

            if platePattern(string) == True and string not in plates:
                plates.append(string)

        if len(plates) > 0:
            drawText(frame, plates)

        frame = utils.draw_bbox(
            frame, bboxes)  # Draws bounding box around license plate
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        Sum += time.time() - start
        print('Avg fps:- ', Sum / n)

        out.write(frame)
        cv2.imshow("result", frame)
        if cv2.waitKey(1) == 27: break
    out.release()
    cv2.destroyAllWindows()
def main(_argv):
    print('main')
    input_layer = tf.keras.layers.Input([FLAGS.size, FLAGS.size, 3])
    feature_maps = YOLOv4(input_layer, NUM_CLASS)
    bbox_tensors = []
    for i, fm in enumerate(feature_maps):
        bbox_tensor = decode(fm, NUM_CLASS, i)
        bbox_tensors.append(bbox_tensor)

    model = tf.keras.Model(input_layer, bbox_tensors)
    utils.load_weights(model, 'data/YOLOv4-obj_1000.weights')

    img = cv2.imread(FLAGS.input)  # Reading input
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    bboxes = plateDetect(img, FLAGS.size, model)  # License plate detection
    plates = []

    for i in range(len(bboxes)):
        plate_img = img[int(bboxes[i][1]):int(bboxes[i][3]),
                        int(bboxes[i][0]):int(bboxes[i][2])]
        prediction_groups = pipeline.recognize(
            [plate_img])  # Text detection and recognition on license plate
        string = ''
        for j in range(len(prediction_groups[0])):
            string = string + prediction_groups[0][j][0].upper()

        if platePattern(string) == True and string not in plates:
            plates.append(string)

    if len(plates) > 0:
        drawText(img, plates)

    img = utils.draw_bbox(img,
                          bboxes)  # Draws bounding box around license plate
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

    cv2.imwrite(FLAGS.output, img)  # Saving output
    print('Output saved to ', FLAGS.output)
    cv2.imshow("result", img)
    cv2.waitKey(0)