Exemplo n.º 1
0
def run_main():
    parser = argparse.ArgumentParser(
        description='Add a new image into database.')
    parser.add_argument('--id',
                        dest='image_id',
                        type=int,
                        required=True,
                        help='ID of the image to add into EuclidesDB.')
    parser.add_argument('--file',
                        dest='filename',
                        type=str,
                        required=True,
                        help='Image file name.')
    args = parser.parse_args()

    image = Image.open(args.filename)
    image_id = int(args.image_id)
    image.thumbnail((300, 300), Image.ANTIALIAS)
    image = F.center_crop(image, 224)

    with euclides.Channel("localhost", 50000) as channel:
        db = euclides.EuclidesDB(channel)
        ret_add = db.add_image(image_id, ["resnet18"], image)

        # After finishing adding items, you need to tell
        # the database to refresh the indexes to add newly
        # indexed items.
        db.refresh_index()

    predictions = ret_add.vectors[0].predictions
    print("Preds Len: ", len(predictions))

    # Category should be 281: 'tabby, tabby cat' for cat.jpg
    # Classes from https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a
    print("Category : ", np.array(predictions).argmax())
Exemplo n.º 2
0
def run_main():
    parser = argparse.ArgumentParser(
        description='Add all images from a directory into the database.')
    parser.add_argument('--directory',
                        dest='directory',
                        type=str,
                        required=True,
                        help='Image file name (ex. ./images).')
    parser.add_argument('--pattern',
                        dest='pattern',
                        type=str,
                        required=True,
                        help='Image file pattern (ex. *.jpg).')
    parser.add_argument('--output',
                        dest='output',
                        type=str,
                        required=True,
                        help='Output filename with IDs (ex. output.json).')
    args = parser.parse_args()

    channel = euclides.Channel("localhost", 50000)
    db = euclides.EuclidesDB(channel)

    path = Path(args.directory)
    items_dict = {}
    for id_item, pfile in enumerate(path.glob(args.pattern)):
        print("Adding file", pfile.name)
        add_image(db, id_item, pfile.absolute())
        items_dict[pfile.name] = id_item

    with open(args.output, "w") as fhandle:
        json.dump(items_dict, fhandle)

    db.refresh_index()
    channel.close()
Exemplo n.º 3
0
def run_main():
    parser = argparse.ArgumentParser(description='Find similar images in EuclidesDB.')
    parser.add_argument('--topk', dest='topk', type=int, required=True,
                        help='Find top k results.')
    parser.add_argument('--file', dest='filename', type=str, required=True,
                        help='Image file name.')
    args = parser.parse_args()

    image = Image.open(args.filename)
    image.thumbnail((300, 300), Image.ANTIALIAS)
    image = F.center_crop(image, 224)

    with euclides.Channel("localhost", 50000) as channel:
        db = euclides.EuclidesDB(channel)
        ret_similar = db.find_similar(image, ["resnet18"], args.topk)

    print(ret_similar)
Exemplo n.º 4
0
def recommend():
    image_url = request.args.get('url')

    response = requests.get(image_url)
    image = Image.open(BytesIO(response.content))
    image_id = int(1)
    image.thumbnail((300, 300), Image.ANTIALIAS)
    image = f.center_crop(image, 224)

    with euclides.Channel("localhost", 8000) as channel:
        db = euclides.EuclidesDB(channel)
        ret_add = db.add_image(image_id, ["resnet18"], image)

        # After finishing adding items, you need to tell
        # the database to refresh the indexes to add newly
        # indexed items.
        db.refresh_index()

    predictions = ret_add.vectors[0].predictions
    print("Preds Len: ", len(predictions))

    labels = requests.get(
        "https://gist.githubusercontent.com/ShamsUlAzeem/3ea5740b960d854f4bd43e7322b858e3/raw"
        "/e8e3d428fb94af3c1c20beea9183de364ecbc3a5/imagenet-labels.json").json(
        )

    category = np.array(predictions).argmax()

    output = labels[str(category)]

    images = []
    for name in output.split(","):
        images.extend(find_images(name))

    return render_template('show_images.html',
                           images=images,
                           output=output,
                           url=image_url)