示例#1
0
def process_videos(video_folder, output_folder):

    video_list = [
        video for video in os.listdir(video_folder)
        if video.endswith((".mp4", ".mov", ".avi"))
    ]
    assert len(
        video_list) > 0, "The specified folder {} contains no video".format(
            video_folder)

    for video_name in video_list:

        if not args.config:
            cfg = config_by_video_name(video_name)
        else:
            cfg = config_by_config_name(args.config)

        temp_path = output_folder + video_name.split(".")[0] + "/temp/"
        os.makedirs(temp_path, exist_ok=True)

        preprocess(video_folder + video_name, temp_path, cfg)

        run_inference(temp_path, cfg)

        postprocess(temp_path, cfg)
示例#2
0
def process_images(image_folder, output_folder, cfg):
    temp_path = os.path.join(output_folder, "temp") + "/"
    os.makedirs(temp_path, exist_ok=True)

    preprocess_images(image_folder, temp_path, cfg)

    run_inference(temp_path, cfg)

    postprocess(temp_path, cfg)
示例#3
0
def main():
  """Build hparams, the graph, and train it."""
  hparams = initialize()

  if hparams.run_inference_test:
    hparams.batch_size = 2
    X_mixtures, phases, inference_summaries = graph.build_inference_graph(hparams)
    inference.run_inference(hparams, X_mixtures, phases, inference_summaries)
  else:
    inputs, embedding_info, loss, loss_summary, summaries = (
        graph.build_train_graph(hparams))

    train.run_train(hparams, inputs, embedding_info,
        loss, loss_summary, summaries)
示例#4
0
def hello():

    output_filename = None
    coconut_count = None
    if request.method == 'POST':
        if 'image' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['image']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)

            if os.path.isfile(
                    os.path.join(app.config['UPLOAD_FOLDER'], filename)):
                filename = "%s%s" % (uuid.uuid4(), filename)

            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            coconut_count = run_inference(
                os.path.join(app.config['UPLOAD_FOLDER'], filename), filename)

            return redirect(
                url_for('result',
                        file=filename,
                        count=coconut_count,
                        _anchor="output"))
        # check if the post request has
    return render_template('index.html',
                           output_filename=None,
                           coconut_count=None)
示例#5
0
def image(data_image):
    # for some reason the image doesn't always send.
    # I think it takes the camera a second to warm up.
    # Its best to handle this in javascript for optimizations
    if len(data_image) < 30:
        return

    sbuf = StringIO()
    sbuf.write(data_image)

    headers, base64_image = data_image.split(',', 1)

    # decode and convert into image
    b = io.BytesIO(base64.b64decode(base64_image))
    pimg = Image.open(b)

    # converting RGB to BGR, as opencv standards
    frame = cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)

    # Process the image frame
    frame = inference.run_inference(frame)
    imgencode = cv2.imencode('.jpg', frame)[1]

    # base64 encode
    stringData = base64.b64encode(imgencode).decode('utf-8')
    b64_src = 'data:image/jpg;base64,'
    stringData = b64_src + stringData

    # emit the frame back
    emit('response_back', stringData)
示例#6
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--time", help="Time elapsed, in seconds, so far in the game", 
                        required=True, type=int)
    parser.add_argument("-s", "--spread", help="Spread, relative to visiting team", 
                        required=True, type=float)
    parser.add_argument("-f", "--favorite", help="Current point total for the favorite", 
                        required=True, type=int)
    parser.add_argument("-u", "--underdog", help="Current point total for the underdog", 
                        required=True, type=int)
    args = parser.parse_args()

    
    time_sec = args.time
    spread = args.spread
    favorite_points = args.favorite
    underdog_points = args.underdog

    assert time_sec >= 0, "Time must be a positive integer"
    assert favorite_points >= 0, "Favorite points must be a positive integer"
    assert favorite_points >= 0, "Underdog points must be a positive integer"

    model = inference.load_model(MODEL_PATH)
    scaler = inference.load_scaler(SCALER_PATH)

    pred = inference.run_inference(model, scaler, time_sec, spread, 
                                   favorite_points, underdog_points)

    print(pred)
示例#7
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)

    input_mode = int(os.path.expanduser(args.mode))
    assert input_mode == 0 or input_mode == 1, 'Input mode must be in {0,1}'
    test_path = os.path.expanduser(args.test_dir)

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

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

    mAP, precision, recalls = run_inference(model_path,
                                            anchors,
                                            classes_path,
                                            test_path,
                                            output_path,
                                            input_mode,
                                            args.score_threshold,
                                            args.iou_threshold,
                                            args.map_iou_threshold,
                                            args.class_index,
                                            args.num_saved_images)

    plt.scatter(recalls, precision)
    plt.xlim(0,1)
    plt.ylim(0,1)
    plt.xlabel("Recall")
    plt.ylabel("Precision")
    plt.savefig(args.plot_file)
    plt.clf()

    print("%s,%.6g,%.6g,%.6g" % (args.identifier,
                                 mAP,
                                 np.average(precision),
                                 np.average(recalls)))
示例#8
0
    def post(self):
        # initialize params
        id = uuid.uuid4()
        args = inference_args.parse_args()
        abort_if_url_invalid(args['url'])

        result_dict[id] = args

        # perform the inference task
        result = run_inference(args['network'], args['url'])

        return {"result": {
                "id": str(id),
                "network": "{:s}".format(args['network']),
                "recognized_object": "{:s}".format(result['recognized_object']),
                "class_number": "{:d}".format(result['class_number']),
                "confidence": "{:f}%" .format(result['confidence'])
                }}, 200
示例#9
0
def run_inference():
    try:
        input = request.get_json()
    except BadRequest as e:
        app.logger.error(e)
        app.logger.error("Raw request body is: {}".format(request.get_data()))
        return e.description

    app.logger.debug("JSON request body is: {}".format(input))
    time_sec = int(input["time_sec"])
    hspread = input[
        "hspread"]  # The database stores spread relative to the home team
    spread = -1 * hspread  # The model uses spread relative to the visiting team
    favorite_points = int(input["favorite_points"])
    underdog_points = int(input["underdog_points"])

    start = time.time()
    with graph.as_default():
        pred = inference.run_inference(model, scaler, time_sec, spread,
                                       favorite_points, underdog_points)
    end = time.time()
    app.logger.debug("Finished in {} secs".format(end - start))
    return {"cover_probability": float(pred)}
示例#10
0
def main(_):
    # override checkpoint
    if config.checkpoint_dir == 'default':
        config.checkpoint_dir = config.logdir + "/model_save/"
    # override parameter file
    if config.parameter_override_filepath == 'default':
        config.parameter_override_filepath = config.logdir + "/parameters.txt"

    # copy stdout and stderr to checkpoint dir
    tee_output(config.logdir, "out")

    # load parameter file
    if config.use_parameter_override_file is True:
        load_parameter_file(config.parameter_override_filepath, config)

    # print date and time for output records
    now = datetime.datetime.now()
    print("Current date and time: {}".format(
        now.strftime("%Y-%m-%d %H:%M:%S")))

    # load training data
    print('loading training data')
    dataset = load_dataset(config.dataset_file, config.pickle_filepath,
                           config.load_dataset)
    logging.debug('dataset size: %i' % len(dataset))

    # load metadata
    print('loading metadata')
    word2id, id2word, max_sentence_len, max_conversation_len, \
            max_conversation_words, max_persona_len = get_data_info(dataset)

    # load runtime "flags"
    config.max_sentence_len = max_sentence_len
    config.max_conversation_len = max_conversation_len
    config.max_conversation_words = max_conversation_words
    config.max_persona_len = max_persona_len
    config.vocab_size = len(word2id)

    # output parameters to stdout
    print("\n\n")
    print("parameters:")
    for key, value in config.flag_values_dict().items():
        print("    ", end="")
        print("{} : {}".format(key, value))
    print("\n\n")

    # load word vectors
    print('loading word vectors')
    word2vec = load_word_embeddings(config.embedding_fname,
                                    config.embedding_dim, word2id)

    logging.debug('word2vec type: %s' % type(word2vec))
    logging.debug('word2vec shape: %s' % str(word2vec.shape))

    # convert dataset to integer ids
    print('converting dataset to ids')
    if config.mode != "data_viz":
        dataset = convert_to_id(dataset, word2id)

    # split into train and test
    # TODO make split ratio into a parameter
    print('splitting dataset')
    #random.shuffle(dataset)

    if config.dataset_size < 0:
        train_size = int(len(dataset) * 0.9)
    else:
        train_size = config.dataset_size

    train_data = dataset[:train_size]
    test_data = dataset[train_size:]

    # setup debugger
    if config.debug == True:
        sess = tf_debug.TensorBoardDebugWrapperSession(sess, 'localhost:6064')

    logging.debug('building model')

    model = Model(config, word2vec, id2word, word2id)

    # load model
    if config.load_model == True or config.mode == 'inference':
        # ensure load folder exists
        if os.path.isdir(config.checkpoint_dir):
            print("loading model from: {}".format(config.checkpoint_dir))
            model.load()
        else:
            print("no save folder exists. Continuing without loading model.")

    # ensure print outs in main get printed out before further logging debugs
    # TODO change all logging type printouts to logging.debug calls
    print("", flush=True)

    # perform parameter search
    if config.mode == "parameter_search":
        logging.debug("performing parameter search")
        parameter_ranges = {}
        parameter_ranges["learning_rate"] = (-5, -2)
        parameter_ranges["hidden_size"] = (250, 950)
        parameter_ranges["num_layers"] = (2, 4)

        perform_parameter_search(
            Model,
            config,
            word2vec,
            id2word,
            word2id,
            parameter_ranges,
            train_data,
            num_epochs_per_parameter=config.parameter_search_epochs)
    # run inference
    elif config.mode == "inference":
        config.batch_size = 1
        run_inference(dataset, config, word2id, word2vec, id2word, 1)
    # run data visualization
    elif config.mode == "data_viz":
        look_at_data(train_data)
    # train model
    elif config.mode == "train":
        logging.debug('training model')
        model.train(train_data, test_data, config.train_steps)
    elif config.mode == "unit_test":
        logging.debug('running unit tests')
        run_all_tests(config, train_data, word2vec, id2word, word2id)
    else:
        print("invalid mode! Exiting.")
示例#11
0
def _main(args):
    data_path = os.path.expanduser(args.data_path)
    classes_path = os.path.expanduser(args.classes_path)
    anchors_path = os.path.expanduser(args.anchors_path)
    result_path = os.path.expanduser(args.result_path)
    test_path = os.path.expanduser(args.test_path)
    model_prefix = os.path.expanduser(args.model_prefix)
    num_frozen = int(args.num_frozen)
    num_trials = int(args.num_trials)
    num_epochs = int(args.num_epochs)
    shuffle_input = bool(int(args.shuffle))

    class_names = get_classes(classes_path)

    data = np.load(data_path)  # custom data saved as a numpy file.
    #  has 2 arrays: an object array 'boxes' (variable length of boxes in each image)
    #  and an array of images 'images'

    anchors = get_anchors(anchors_path)
    anchors = YOLO_ANCHORS

    for trial in range(num_trials):

        # Reprocess data to populate image_data_gen. Sacrifice latency for memory
        image_data_gen, boxes = data_utils.process_data(
            iter(data['images']),
            data['images'].shape[2],
            data['images'].shape[1],
            data['boxes'],
            dim=608)
        detectors_mask, matching_true_boxes = get_detector_mask(boxes, anchors)

        model_name = model_prefix + "-" + str(num_frozen) + "fr-trial" + str(
            trial)
        print "Training model:", model_name

        train(class_names,
              anchors,
              image_data_gen,
              boxes,
              detectors_mask,
              matching_true_boxes,
              model_name,
              num_frozen,
              num_epochs,
              shuffle_input=shuffle_input)

        if test_path != "" and result_path != "":
            mAP, precision, recalls = run_inference(
                model_name + ".h5",
                anchors,
                classes_path,
                test_path,
                None,  # output_path
                1,  # mode
                0.5,  # score_threshold
                0.5,  # iou_threshold
                0.5)  # mAP_iou_threshold
            with open(result_path, "a+") as f:
                line = "%d,%d,%.6g,%.6g,%.6g,%d,%s\n" % (
                    trial, num_frozen, mAP, np.average(precision),
                    np.average(recalls), num_epochs, model_name + ".h5")
                f.write(line)
示例#12
0
from inference import run_inference

if __name__ == '__main__':
    parser = ArgumentParser(
        description='Lightweight 3D human pose estimation demo. '
        'Press esc to exit, "p" to (un)pause video or process next image.')
    parser.add_argument('--video',
                        help='Optional. Path to video file or camera id.',
                        type=str,
                        default='')
    parser.add_argument('--images',
                        help='Optional. Path to input image(s).',
                        nargs='+',
                        default='')
    parser.add_argument('--height-size',
                        help='Optional. Network input layer height size.',
                        type=int,
                        default=256)
    parser.add_argument(
        '--port',
        help='Optional. Port number on which to host socket server.',
        type=int,
        default=8082)

    args = parser.parse_args()

    if args.video == '' and args.images == '':
        raise ValueError('Either --video or --image has to be provided')

    run_inference(args)