Esempio n. 1
0
    def __init__(self,
                 paths,
                 score_threshold=0.3,
                 iou_threshold=0.6,
                 max_detections=15):
        # Load classes and anchors.
        json_path, anchors_path, classes_path, weights_path = paths

        with open(classes_path) as f:
            self.class_names = f.readlines()
        self.class_names = [c.strip() for c in self.class_names]

        with open(anchors_path) as f:
            self.anchors = f.readline()
            self.anchors = [float(x) for x in self.anchors.split(',')]
            self.anchors = np.array(self.anchors).reshape(-1, 2)

        self.image_input = Input(shape=(None, None, 3))

        # yolo_json_file = open(json_path, 'r') # no longer loading model directly, using create_model
        # yolo_json = yolo_json_file.read()
        # yolo_json_file.close()

        self.yolo_model, garbage = create_model(self.anchors, self.class_names,
                                                False, 0, json_path)

        self.yolo_model.load_weights(weights_path)

        self.yolo_model.summary()

        self.max_boxes = max_detections,
        self.score_threshold = score_threshold,
        self.iou_threshold = iou_threshold

        print('yolo object created')
Esempio n. 2
0
    def __init__(self, viden_yolo_weights_path, viden_crnn_weights_path,
                 classes_path, out_path):
        self.act_model = create_crnn_model(train=False)
        self.act_model.load_weights(
            viden_crnn_weights_path
        )  # 'viden_trained_models\\viden_crnn_14May2021.hdf5')
        class_names = get_classes(classes_path)
        #print(class_names)
        self.out_path = out_path
        #self.anchors = YOLO_ANCHORS
        self.yolo_model_body, self.yolo_model = create_model(
            YOLO_ANCHORS,
            class_names,
            load_pretrained=False,
            freeze_body=False)
        self.yolo_model_body.load_weights(
            viden_yolo_weights_path
        )  #'viden_trained_models\\viden_yolo_14May2021.h5')
        self.yolo_outputs = yolo_head(self.yolo_model_body.output,
                                      YOLO_ANCHORS, len(class_names))
        self.yolo_input_image_shape = K.placeholder(shape=(2, ))

        self.boxes, self.scores, self.classes = yolo_eval(
            self.yolo_outputs,
            self.yolo_input_image_shape,
            max_boxes=1,
            score_threshold=.7,
            iou_threshold=0.5)
Esempio n. 3
0
def _main(args):
    # Current directory
    img_path = args.data_path
    classes_path = args.classes_path
    print("processing path:" + img_path)
    class_names = get_classes(classes_path)
    anchors = YOLO_ANCHORS  #read_anchors("model_data/yolo_anchors.txt")

    model_body, model = create_model(anchors, class_names)
    predict_draw(model_body,
                 class_names,
                 anchors,
                 img_path,
                 weights_name='trained_stage_3_best.h5',
                 out_path="output_images",
                 save_all=False)
Esempio n. 4
0
def _main(args):
    def predict(sess, image):
        # Preprocess your image
        image, image_data = preprocess_image(image,
                                             model_image_size=(416, 416))

        # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.
        # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})
        out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],
                                                      feed_dict={
                                                          yolo_model.input:
                                                          image_data,
                                                          K.learning_phase(): 0
                                                      })

        # Print predictions info
        print('Found {} boxes'.format(len(out_boxes)))
        # Generate colors for drawing bounding boxes.
        colors = generate_colors(class_names)
        # Draw bounding boxes on the image file
        out_image = draw_boxes(image, out_scores, out_boxes, out_classes,
                               class_names, colors)

        return out_image, out_scores, out_boxes, out_classes

    video_path = os.path.join(INPUT_PATH, args.input)
    input = cv2.VideoCapture(video_path)
    if (input.isOpened() == False):
        print("Error opening video file.")
        return
    height = input.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = input.get(cv2.CAP_PROP_FRAME_WIDTH)
    fps = input.get(cv2.CAP_PROP_FPS)

    if not os.path.exists(OUTPUT_PATH):
        os.mkdir(OUTPUT_PATH)
    output_file_path = os.path.join(OUTPUT_PATH, args.input)
    output = cv2.VideoWriter(output_file_path, cv2.VideoWriter_fourcc(*'MP4V'),
                             fps, (int(width), int(height)))

    image_shape = (height, width)

    sess = K.get_session()

    class_names = read_classes(CLASS_LIST_PATH)
    anchors = read_anchors(YOLO_ANCHORS_PATH)
    yolo_model, model = create_model(anchors, class_names)
    yolo_model.load_weights(args.weights)
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       image_shape,
                                       score_threshold=args.score_threshold,
                                       iou_threshold=args.iou_threshold)

    n_frame = 0
    while input.isOpened():
        ret, original_frame = input.read()
        if ret:
            n_frame = n_frame + 1
            print("processing frame {} ...".format(n_frame))
            process_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB)
            process_frame = Image.fromarray(process_frame)
            process_frame, out_scores, out_boxes, out_classes = predict(
                sess, process_frame)
            process_frame = cv2.cvtColor(process_frame, cv2.COLOR_BGR2RGB)

            output.write(process_frame)
        else:
            break

    print("Finished. Output file: ", output_file_path)
    input.release()
    output.release()
    cv2.destroyAllWindows()
Esempio n. 5
0
    print('Creating output path {}'.format(output_path))
    os.mkdir(output_path)

sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

with open(classes_path) as f:
    class_names = f.readlines()
class_names = [c.strip() for c in class_names]

with open(anchors_path) as f:
    anchors = f.readline()
    anchors = [float(x) for x in anchors.split(',')]
    anchors = np.array(anchors).reshape(-1, 2)

# yolo_model = load_model(model_path)
yolo_model, _ = create_model(anchors, class_names)
yolo_model.load_weights('trained_stage_3_best.h5')

# Verify model, anchors, and classes are compatible
num_classes = len(class_names)
num_anchors = len(anchors)
# TODO: Assumes dim ordering is channel last
model_output_channels = yolo_model.layers[-1].output_shape[-1]
assert model_output_channels == num_anchors * (num_classes + 5), \
    'Mismatch between model and given anchor and class sizes. ' \
    'Specify matching anchors and classes with --anchors_path and ' \
    '--classes_path flags.'
print('{} model, anchors, and classes loaded.'.format(model_path))

# Check if model is fully convolutional, assuming channel last order.
model_image_size = yolo_model.layers[0].input_shape[1:3]
def _main(args):
    def predict(sess, image):
        # Preprocess your image
        image, image_data = preprocess_image(image,
                                             model_image_size=(416, 416))

        # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.
        # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})
        out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],
                                                      feed_dict={
                                                          yolo_model.input:
                                                          image_data,
                                                          K.learning_phase(): 0
                                                      })

        # Print predictions info
        print('Found {} boxes'.format(len(out_boxes)))
        # Generate colors for drawing bounding boxes.
        colors = generate_colors(class_names)
        # Draw bounding boxes on the image file
        out_image = draw_boxes(image, out_scores, out_boxes, out_classes,
                               class_names, colors)

        return out_image, out_scores, out_boxes, out_classes

    image_path = os.path.join(INPUT_PATH, args.input)
    input = cv2.imread(image_path)
    if input is None:
        print("Error opening image file")
        return
    height = float(input.shape[0])
    width = float(input.shape[1])

    image_shape = (height, width)

    sess = K.get_session()

    class_names = read_classes(CLASS_LIST_PATH)
    anchors = read_anchors(YOLO_ANCHORS_PATH)
    yolo_model, model = create_model(anchors, class_names)
    yolo_model.load_weights(args.weights)
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       image_shape,
                                       score_threshold=args.score_threshold,
                                       iou_threshold=args.iou_threshold)

    process_img = cv2.cvtColor(input, cv2.COLOR_BGR2RGB)
    process_img = Image.fromarray(process_img)
    process_img, out_scores, out_boxes, out_classes = predict(
        sess, process_img)
    output = cv2.cvtColor(process_img, cv2.COLOR_BGR2RGB)

    cv2.imshow('prediction', output)
    cv2.waitKey(0)

    output_file_path = os.path.join(OUTPUT_PATH, args.input)
    if not os.path.exists(OUTPUT_PATH):
        os.mkdir(OUTPUT_PATH)
    cv2.imwrite(output_file_path, output)
    print("Saved: ", output_file_path)
Esempio n. 7
0
def _main(args):

    input_path = args.input_path
    out_path = args.out_path
    classes_path = args.classes_path
    outF = open(out_path + "\prediction.txt", "w")

    # model to be used at test time
    act_model = create_crnn_model(train=False)
    # load the saved best model weights
    act_model.load_weights('viden_trained_models\\viden_crnn_14May2021.hdf5')

    class_names = get_classes(classes_path)
    anchors = YOLO_ANCHORS
    yolo_model_body, yolo_model = create_model(anchors, class_names)
    yolo_model_body.load_weights(
        'viden_trained_models\\viden_yolo_14May2021.h5')

    yolo_outputs = yolo_head(yolo_model_body.output, anchors, len(class_names))
    yolo_input_image_shape = K.placeholder(shape=(2, ))

    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       yolo_input_image_shape,
                                       max_boxes=6,
                                       score_threshold=.7,
                                       iou_threshold=0.5)
    config = ('-l eng --oem 1 --psm 3')
    time_lst = []
    orig_img_name_lst = []
    for img_path in glob.iglob(input_path + "*.jpg"):
        orig_img_name = ''
        print("Evaluating :" + img_path)
        start_time = time.time()
        pred_boxes, pred_box_classes = get_bounding_boxes(
            yolo_model_body,
            boxes,
            scores,
            classes,
            yolo_input_image_shape,
            img_path,
            class_names,
            out_path,
            save=True)
        outF.write("File Name:" + img_path)
        outF.write("\n")
        for i, box in list(enumerate(pred_boxes)):
            box_class = class_names[pred_box_classes[i]]
            # box = pred_boxes[i]
            top, left, bottom, right = box
            image = PIL.Image.open(img_path)
            pred_obj_img = image.crop((left, top, right, bottom))
            txt = get_text(act_model, pred_obj_img, box_class)

            outF.write(box_class + ":" + txt)
            outF.write("\n")
            print(txt)
            _, tail = ntpath.split(img_path)
            orig_img_name = os.path.splitext(tail)[0]

            pred_obj_img.save(out_path + "\\" + txt + "_" + str(i) + "_" +
                              box_class + ".jpg")
        end_time = time.time()
        time_lst.append(end_time - start_time)
        orig_img_name_lst.append(orig_img_name)
        outF.write("\n")

    outF.close()
    plot_time(out_path, orig_img_name_lst, time_lst)
Esempio n. 8
0
def _main(args):
    model_path = os.path.expanduser(args.model_path)
    assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    test_path = os.path.expanduser(args.test_path)
    output_path = os.path.expanduser(args.output_path)
    label_file = args.label_file

    if not os.path.exists(output_path):
        print('Creating output path {}'.format(output_path))
        os.mkdir(output_path)

    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]

    # TODO: USE THIS INSTEAD
    # with open(anchors_path) as f:
    #     anchors = f.readline()
    #     anchors = [float(x) for x in anchors.split(',')]
    #     anchors = np.array(anchors).reshape(-1, 2)

    # yolo_model = load_model(model_path)
    # ALEXANDER HACK
    anchors = np.array(
        ((0.57273, 0.677385), (1.87446, 2.06253), (3.33843, 5.47434),
         (7.88282, 3.52778), (9.77052, 9.16828)))
    yolo_model, model = create_model(anchors,
                                     class_names,
                                     load_pretrained=False,
                                     freeze_body=True)
    model.load_weights(model_path)

    # Verify model, anchors, and classes are compatible
    num_classes = len(class_names)
    num_anchors = len(anchors)
    # TODO: Assumes dim ordering is channel last
    model_output_channels = yolo_model.layers[-1].output_shape[-1]
    assert model_output_channels == num_anchors * (num_classes + 5), \
        'Mismatch between model and given anchor and class sizes. ' \
        'Specify matching anchors and classes with --anchors_path and ' \
        '--classes_path flags.'
    print('{} model, anchors, and classes loaded.'.format(model_path))

    # Check if model is fully convolutional, assuming channel last order.
    model_image_size = yolo_model.layers[0].input_shape[1:3]
    is_fixed_size = model_image_size != (None, None)

    # Generate colors for drawing bounding boxes.
    hsv_tuples = [(x / len(class_names), 1., 1.)
                  for x in range(len(class_names))]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(
        map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
            colors))
    random.seed(10101)  # Fixed seed for consistent colors across runs.
    random.shuffle(colors)  # Shuffle colors to decorrelate adjacent classes.
    random.seed(None)  # Reset seed to default.

    # Generate output tensor targets for filtered bounding boxes.
    # TODO: Wrap these backend operations with Keras layers.
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=args.score_threshold,
                                       iou_threshold=args.iou_threshold)

    lf = open(label_file)
    num_tests = 10
    count = 0
    while (count < num_tests):
        count += 1
        line = lf.readline()
        line = line.split(' ')
        image_file = os.path.join(utils.home(), 'data', line[0])
        image = Image.open(image_file)
        image_file = line[0]
        pts = line[1:]
        pts = np.asarray(pts, dtype=np.float)
        image = np.array(image)
        image = utils.imwarp(image, pts, sz=(416, 416))
        image_data = image
        image_data = np.expand_dims(image_data, 0)
        image = Image.fromarray(image)
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                yolo_model.input: image_data,
                input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for {}'.format(len(out_boxes), image_file))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)

            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        image.save(os.path.join(output_path, image_file), quality=90)
    sess.close()
def _main(args):
    def predict(sess, image):
        # Preprocess your image
        image, image_data = preprocess_image(image,
                                             model_image_size=(416, 416))

        # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.
        # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})
        out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],
                                                      feed_dict={
                                                          yolo_model.input:
                                                          image_data,
                                                          K.learning_phase(): 0
                                                      })

        # Print predictions info
        print('Found {} boxes'.format(len(out_boxes)))
        # Generate colors for drawing bounding boxes.
        colors = generate_colors(class_names)
        # Draw bounding boxes on the image file
        out_image = draw_boxes(image, out_scores, out_boxes, out_classes,
                               class_names, colors)

        return out_image, out_scores, out_boxes, out_classes

    cap = cv2.VideoCapture(args.input)
    if (cap.isOpened() == False):
        print("Error opening video capture device.")
        return
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)

    image_shape = (height, width)

    sess = K.get_session()

    class_names = read_classes(CLASS_LIST_PATH)
    anchors = read_anchors(YOLO_ANCHORS_PATH)
    yolo_model, model = create_model(anchors, class_names)
    yolo_model.load_weights(args.weights)
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       image_shape,
                                       score_threshold=args.score_threshold,
                                       iou_threshold=args.iou_threshold)

    while True:
        ret, original_frame = cap.read()

        cv2.imshow('original', original_frame)

        process_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB)
        process_frame = Image.fromarray(process_frame)
        process_frame, out_scores, out_boxes, out_classes = predict(
            sess, process_frame)
        process_frame = cv2.cvtColor(process_frame, cv2.COLOR_BGR2RGB)

        cv2.imshow('output', process_frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
Esempio n. 10
0
def run_nn(hWnd):
    global loaded

    model_path = os.path.expanduser('YAD2K/model_data/yolo.h5')
    anchors_path = os.path.expanduser('YAD2K/model_data/yolo_anchors.txt')
    classes_path = os.path.expanduser('YAD2K/model_data/league_classes.txt')

    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]

    with open(anchors_path) as f:
        anchors = f.readline()
        anchors = [float(x) for x in anchors.split(',')]
        anchors = np.array(anchors).reshape(-1, 2)

    yolo_model, _ = create_model(anchors, class_names)
    yolo_model.load_weights('trained_stage_3_best.h5')

    # Verify model, anchors, and classes are compatible
    num_classes = len(class_names)
    num_anchors = len(anchors)
    # TODO: Assumes dim ordering is channel last
    model_output_channels = yolo_model.layers[-1].output_shape[-1]
    assert model_output_channels == num_anchors * (
        num_classes +
        5), 'Mismatch between model and given anchor and class sizes. '
    print('{} model, anchors, and classes loaded.'.format(model_path))

    # Check if model is fully convolutional, assuming channel last order.
    model_image_size = yolo_model.layers[0].input_shape[1:3]
    is_fixed_size = model_image_size != (None, None)

    # Generate colors for drawing bounding boxes.
    hsv_tuples = [(x / len(class_names), 1., 1.)
                  for x in range(len(class_names))]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(
        map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
            colors))
    random.seed(10101)  # Fixed seed for consistent colors across runs.
    random.shuffle(colors)  # Shuffle colors to decorrelate adjacent classes.
    random.seed(None)  # Reset seed to default.

    # Generate output tensor targets for filtered bounding boxes.
    # TODO: Wrap these backend operations with Keras layers.
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=args.score_threshold,
                                       iou_threshold=args.iou_threshold)

    # Save the output into a compact JSON file.
    outfile = open('output/game_data.json', 'w')
    # This will be appended with an object for every frame.
    data_to_write = []

    loaded = True
    win32gui.RedrawWindow(hWnd, None, None,
                          win32con.RDW_INVALIDATE | win32con.RDW_ERASE)

    with mss.mss() as sct:
        monitor = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
        while 'Screen capturing':
            last_time = time.time()

            # Get raw pixels from the screen, save it to a Numpy array
            img = np.array(sct.grab(monitor))
            img = Image.fromarray(img)
            img.load()
            background = Image.new("RGB", img.size, (255, 255, 255))
            background.paste(img, mask=img.split()[3])

            test_yolo(background, is_fixed_size, model_image_size, sess, boxes,
                      scores, classes, yolo_model, input_image_shape,
                      class_names, colors)
            win32gui.RedrawWindow(hWnd, None, None,
                                  win32con.RDW_INVALIDATE | win32con.RDW_ERASE)
            # img = test_yolo(background)
            # basewidth = 700
            # wpercent = (basewidth/float(img.size[0]))
            # hsize = int((float(img.size[1])*float(wpercent)))
            # img = img.resize((basewidth,hsize), Image.ANTIALIAS)
            # img = np.array(img)
            # # Display the picture
            # cv2.imshow('OpenCV/Numpy normal', img)
            # Press "q" to quit
            if cv2.waitKey(25) & 0xFF == ord(';'):
                cv2.destroyAllWindows()
                break

    sess.close()
    return