def get_new_boards(indir, json_file):
    """extracts boards from new raw images using coordinates from the json file"""

    boards = {}
    infiles = listdir_nohidden(indir)

    with open(json_file, "r") as file:
        lines = file.readlines()

    for line in lines:  #iterates over all files with masks
        x = json.loads(line)
        url = x["content"]
        filename = url.split("_")[-1]

        if filename in infiles:  #there exists a hi-res raw version of this image.

            img = cv2.imread(os.path.join(indir, filename))

            scale = float(img.shape[0])

            points = x["annotation"][0]["points"]
            points = [[scale * p[0], scale * p[1]] for p in points[:-1]]
            points = np.array(points, dtype=np.int32)

            board = extract_perspective(img, points, cv_globals.BOARD_SIZE)

            board = cv2.cvtColor(board, cv2.COLOR_RGB2GRAY)
            board = cv2.resize(board, cv_globals.BOARD_SIZE)

            boards[filename] = board

    return boards
示例#2
0
def rename_images(indir):
    """Give images unique IDs"""
    for f in listdir_nohidden(indir):
        src = os.path.join(indir, f)
        unique_filename = str(uuid.uuid4())
        dst = os.path.join(indir, unique_filename + ".JPG")
        os.rename(src, dst)
        print("renamed: {} to {}".format(src, dst))
示例#3
0
def resize_images(indir, outdir):
    """Resizes new raw images"""
    for f in listdir_nohidden(indir):
        src = os.path.join(indir, f)
        image = cv2.imread(src)
        resized = cv2.resize(image, cv_globals.INPUT_SIZE, interpolation= cv2.INTER_AREA)  
        dst = os.path.join(outdir, f)
        cv2.imwrite(dst, resized)
        print("resized image {}".format(src))
示例#4
0
def get_test_generator():
    img_filenames = listdir_nohidden(test_data_dir + "raw/")
    test_imgs = np.array(
        list(
            map(lambda x: cv2.imread(test_data_dir + "raw/" + x),
                img_filenames)))

    for i in range(len(test_imgs)):
        yield img_filenames[i], test_imgs[i]
示例#5
0
import time

if __name__ == "__main__":

    # first store the already labelled examples
    already_labelled = []
    with open("../data/labels.csv", "r") as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            already_labelled.append(row[0])

    # next start labelling new examples
    with open('../data/labels.csv', 'a') as csvfile:

        w = csv.writer(csvfile)
        filenames = listdir_nohidden("../data/squares/")
        filenames = [f for f in filenames]

        labelled_sess = 0
        for f in filenames:
            if f in already_labelled:
                continue

            img = cv2.imread("../data/squares/" + f)

            plt.imshow(img, cmap="gray")
            plt.axis("off")
            plt.show()

            label = input("Enter label: ")
示例#6
0
        /path/to/cvroot/weights/modeltype/date/modeltype-{perf}.hdf
where performance is a float between 0 and 1 (1 is good).
"""

import cv_globals
from util import listdir_nohidden
import os


def find_best(models):
    best_perf = 0.0
    for i in range(len(models)):
        model = models[i]
        perf = float(model.split("-")[1][:-5])
        if perf > best_perf:
            best = i
    return models[best]


models = ["extractor", "classifier"]

for model in models:
    training_runs = listdir_nohidden(cv_globals.weights_dir + model)
    for run in training_runs:
        models = listdir_nohidden(cv_globals.weights_dir + model + "/" + run)
        best = find_best(models)
        for model_file in models:
            if model_file is not best:
                to_delete = cv_globals.weights_dir + model + "/" + run + "/" + model_file
                print("Removing: {}".format(to_delete))
                os.remove(to_delete)
示例#7
0
            squares.append(board[i*w:(i+1)*w, j*h:(j+1)*h])
            names.append(ranks[j]+files[7-i])
    
    squares = np.array(squares)
    squares = squares.reshape(squares.shape[0], 64, 64, 1)
    
    return squares, names

if __name__ == "__main__":

    # Extract all squares from all boards
    print("Extracting squares...")
    
    #board_dir = "../data/boards/"
    #square_dir = "../data/squares/"
    
    (_, board_dir, square_dir) = parse_arguments()

    board_filenames = listdir_nohidden(board_dir)

    filenames = [f for f in board_filenames]
    board_imgs = [cv2.imread(board_dir+f, 0) for f in filenames]

    for f, b in zip(filenames, board_imgs):    
        squares, names = extract_squares(b)
        for sq, name in zip(squares, names):
            #print("{}{}".format(name, sq.shape))
            #cv2.imwrite(square_dir + name + f, sq)
            pass

    print("\rExtracting squares...DONE")
示例#8
0
import cv_globals
import os
from os.path import join
from util import listdir_nohidden
import random

indir = join(cv_globals.CVROOT, "computeroot/user_uploads/squares/")
outdir = join(cv_globals.data_root, "squares/")

train_test_split = 0.2

for dirname in listdir_nohidden(indir):
    for filename in listdir_nohidden(join(indir, dirname)):
        split = "validation" if random.random(
        ) < train_test_split else "training"
        src = join(indir, dirname, filename)
        dst = join(outdir, split, dirname, filename)
        print("Moving {} to {}".format(
            src.split("/")[-3:],
            dst.split("/")[-4:]))
        os.rename(src, dst)
示例#9
0
def load_image_ids():
    filenames = [f[:-4] for f in listdir_nohidden(cv_globals.image_dir)]
    return filenames
示例#10
0
import os
from util import listdir_nohidden
import cv_globals
import uuid

i = 0
bases = [cv_globals.data_root + "squares/validation", cv_globals.data_root + "squares/training"]

tally = {}
for base in bases:
    for dirname in listdir_nohidden(base):
        for fname in listdir_nohidden(base + "/" + dirname):
            if len(fname) not in tally:
                tally[len(fname)] = 0 
            else:   
                tally[len(fname)] += 1
            if len(fname) != 18:
                src = base + "/" + dirname + "/" + fname
                uid = str(uuid.uuid1())
                dst = os.path.join(base, dirname, uid + ".JPG")
                #print("saving long file: {}".format(dst))
                #os.rename(src, dst)
print(tally)