예제 #1
0
def images_to_csv_with_label(csv_file, images, label, width, height):
    csv_file.write("label," + ",".join(["pixel" + str(i) for i in range(0, int(width) * int(height))]) + "\n")
    for i, nameFile in enumerate(images):
        print(nameFile)
        csv_file.write(
            label + "," + ",".join(
                str(x) for x in misc.img_to_1d_gray(Image(nameFile).resize(width, height))) + "\n")
예제 #2
0
def images_to_csv_with_label(csv_file, images, label, width, height):
    csv_file.write("label," + ",".join(
        ["pixel" + str(i) for i in range(0,
                                         int(width) * int(height))]) + "\n")
    for i, nameFile in enumerate(images):
        print(nameFile)
        csv_file.write(label + "," + ",".join(
            str(x) for x in misc.img_to_1d_gray(
                Image(nameFile).resize(width, height))) + "\n")
예제 #3
0
def segmented_to_csv(path="", csv_path="train.csv", folder_to_label_dict={}, width=28, height=28):
    with open(csv_path, "w") as train_csv:
        train_csv.write("label," + ",".join(["pixel" + str(i) for i in range(0, int(width) * int(height))]) + "\n")
        # print(folder_to_label_dict)
        for folder in folder_to_label_dict.keys():
            # print(folder)
            files = glob.glob(path + folder + "/*")
            for img_name in files:
                # print(img_name)
                train_csv.write(folder_to_label_dict[folder] + "," + ",".join(
                    str(x) for x in misc.img_to_1d_gray(Image(img_name).resize(width, height))) + "\n")
예제 #4
0
def create_train_csv(path=None, width=28, height=28):
    """create a train csv file from image files and their labels"""
    if path is None:
        path = Popen("pwd", shell=True, stdin=PIPE, stdout=PIPE).stdout.read()[:-1]
    with open(path + "/train.csv", "w") as train_csv, open(
                    path + "/trainLabels.csv", "r") as label_maps_csv:
        train_csv.write("label," + ",".join(["pixel" + str(i) for i in range(0, int(width) * int(height))]) + "\n")
        files = glob.glob(path + "/trainResized/*")
        labels = {line.strip().split(",")[0]: line.strip().split(",")[1] for line in label_maps_csv}
        for i, nameFile in enumerate(files):
            file_id = strip_extension(get_file_name(nameFile))
            train_csv.write(labels[file_id] + "," + ",".join(str(x) for x in misc.img_to_1d_gray(nameFile)) + "\n")
예제 #5
0
def create_test_csv(path=None, width=28, height=28):
    """create a test csv file and their id list from image files"""
    if path is None:
        path = Popen("pwd", shell=True, stdin=PIPE, stdout=PIPE).stdout.read()[:-1]
    with open(path + "/test.csv", "w") as test_csv, open(
                    path + "/testIDs.csv", "w") as test_ids_csv:
        test_csv.write(",".join(["pixel" + str(i) for i in range(0, int(width) * int(height))]) + "\n")
        test_ids_csv.write("ID," + "\n")
        files = glob.glob(path + "/testResized/*")
        for i, nameFile in enumerate(files):
            file_id = strip_extension(get_file_name(nameFile))
            test_ids_csv.write(file_id + "\n")  # save the order of test data (which line is which file)
            test_csv.write(",".join(str(x) for x in misc.img_to_1d_gray(nameFile)) + "\n")
예제 #6
0
def segmented_to_csv(path="",
                     csv_path="train.csv",
                     folder_to_label_dict={},
                     width=28,
                     height=28):
    with open(csv_path, "w") as train_csv:
        train_csv.write("label," + ",".join(
            ["pixel" + str(i)
             for i in range(0,
                            int(width) * int(height))]) + "\n")
        # print(folder_to_label_dict)
        for folder in folder_to_label_dict.keys():
            # print(folder)
            files = glob.glob(path + folder + "/*")
            for img_name in files:
                # print(img_name)
                train_csv.write(folder_to_label_dict[folder] + "," + ",".join(
                    str(x) for x in misc.img_to_1d_gray(
                        Image(img_name).resize(width, height))) + "\n")
예제 #7
0
def create_train_csv(path=None, width=28, height=28):
    """create a train csv file from image files and their labels"""
    if path is None:
        path = Popen("pwd", shell=True, stdin=PIPE,
                     stdout=PIPE).stdout.read()[:-1]
    with open(path + "/train.csv",
              "w") as train_csv, open(path + "/trainLabels.csv",
                                      "r") as label_maps_csv:
        train_csv.write("label," + ",".join(
            ["pixel" + str(i)
             for i in range(0,
                            int(width) * int(height))]) + "\n")
        files = glob.glob(path + "/trainResized/*")
        labels = {
            line.strip().split(",")[0]: line.strip().split(",")[1]
            for line in label_maps_csv
        }
        for i, nameFile in enumerate(files):
            file_id = strip_extension(get_file_name(nameFile))
            train_csv.write(labels[file_id] + "," + ",".join(
                str(x) for x in misc.img_to_1d_gray(nameFile)) + "\n")
예제 #8
0
def create_test_csv(path=None, width=28, height=28):
    """create a test csv file and their id list from image files"""
    if path is None:
        path = Popen("pwd", shell=True, stdin=PIPE,
                     stdout=PIPE).stdout.read()[:-1]
    with open(path + "/test.csv",
              "w") as test_csv, open(path + "/testIDs.csv",
                                     "w") as test_ids_csv:
        test_csv.write(",".join(
            ["pixel" + str(i)
             for i in range(0,
                            int(width) * int(height))]) + "\n")
        test_ids_csv.write("ID," + "\n")
        files = glob.glob(path + "/testResized/*")
        for i, nameFile in enumerate(files):
            file_id = strip_extension(get_file_name(nameFile))
            test_ids_csv.write(
                file_id +
                "\n")  # save the order of test data (which line is which file)
            test_csv.write(",".join(
                str(x) for x in misc.img_to_1d_gray(nameFile)) + "\n")
예제 #9
0
def generate_letter_images():
    """generate a new data of warped characters [a-zA-Z0-9]"""
    if "(" in sys.argv[1]:  # assume a dimension input and an empty bg
        [w, h] = sys.argv[1][1:-1:].split(',')
        print(int(w) * int(h))
    with open("/home/agp/workspace/deep_learning/streetView/new_dataset/train_with_inverted.csv", "w") as train_csv:
        train_csv.write("label," + ",".join(["pixel" + str(i) for i in range(0, int(w) * int(h))]) + "\n")
        for text in string.ascii_letters + string.digits:  # for all alphanumeric
            print(text)
            if "(" in sys.argv[1]:  # assume a dimension input and an empty bg
                img = Image(tuple([int(i) for i in sys.argv[1][1:-1:].split(',')]))
                fname = "test_input.Bmp"
            else:
                img = Image(sys.argv[1]).invert()  # background e.g. Image((150,100))#
                fname = sys.argv[1]
            path = "/home/agp/workspace/deep_learning/streetView/new_dataset/"
            sys.stdout.flush()
            # text = sys.argv[2] # e.g. "A.G.Polat"
            w = img.width
            h = img.height

            # extended fonts: wont work in windows!
            if False and "Linux" in platform.system():  # skip them for now
                fonts = Popen('find /usr/share/fonts | grep ttf', shell=True, stdin=PIPE,
                              stdout=PIPE).stdout.read().strip().split('\n') + img.dl().listFonts()
            else:
                fonts = img.dl().listFonts()
            # print(fonts)
            # fonts=xfonts+fonts
            colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in
                      range(0, len(fonts))]
            i = 0
            newImg = img.copy()
            img2 = Image(img.size())
            for font in fonts:
                if font == "" or font in ["qbicle1brk", "dblayer1brk", "dblayer3brk", "droidsanshebrew", "mrykacstqurn",
                                          "zoetropebrk", "lohitpunjabi", "loopybrk", "kacstqurn", "droidsansgeorgian",
                                          "mallige", "webdings", "3dletbrk", "scalelinesmazebrk", "kacstpen",
                                          "nucleusbrk", "unresponsivebrk", "kacstart", "kacstdecorative", "dblayer2brk",
                                          "kacstoffice", "binaryxbrk", "binaryx01sbrk", "lklug", "symmetrybrk",
                                          "linedingsbrk", "droidsansarmenian", "zurklezoutlinebrk",
                                          "entangledlayerbbrk", "kacstdigital", "skullcapzbrk", "kacstbook",
                                          "konectoro1brk", "yourcomplexobrk", "opensymbol", "18holesbrk",
                                          "droidsansjapanese", "qbicle4brk",
                                          "qbicle3brk", "lohitdevanagari", "doublebogeybrk", "binary01sbrk",
                                          "kacstscreen", "lohitbengali", "kacstposter", "kedage", "kacstone",
                                          "kacstnaskh", "droidsansethiopic", "qbicle2brk", "vemana2000", "kacstfarsi",
                                          "xmaslightsbrk", "headdingmakerbrk", "kacstletter", "binarybrk", "ori1uni",
                                          "90starsbrk", "codeoflifebrk", "saab", "zurklezsolidbrk", "pothana2000",
                                          "kacsttitle", "fauxsnowbrk", "lohitgujarati", "tetricidebrk", "lohittamil",
                                          "kacsttitlel", "droidsansthai", "dblayer4brk", "bitblocksttfbrk",
                                          "pindownbrk", "droidarabicnaskh"]:  # font is no good
                    continue
                # if text == "":
                #    text=font.split("/")[-1]
                img2.dl().selectFont(font)
                y = random.randint(0, h)
                x = random.randint(0, w)
                fs = int(w / 1.1)
                fontSize = random.randint(random.randint(fs, w), int(w * 1.1))
                img2.dl().setFontSize(fontSize)  # font size between 2 and 100
                (w1, h1) = img2.dl().textDimensions(text)
                k = 0
                skip_this_one = False
                while x + w1 > w * 1.1 or y + h1 > h * 1.1:  # 1.2 for allowing text to be partly outside the box
                    k += 1
                    y = random.randint(0, h)
                    x = random.randint(0, w)
                    # if k>5:
                    #    y=random.randint(0,h//3+int(random.random()*0.6*h))
                    #    x=random.randint(0,int(w*random.random()*0.4))
                    if k > 500:
                        skip_this_one = True  # the font is too big for the image dimensions
                        print("skipping the big font:" + str(font))
                        break
                    fontSize = int(0.95 * fontSize)
                    if fontSize < w // 5:
                        fontSize = random.randint(random.randint(fs, w - 1), w)
                    fs -= 2
                    if fs < 3:
                        fs = random.randint(2, w - 1)
                    img2.dl().setFontSize(fontSize)
                    (w1, h1) = img2.dl().textDimensions(text)
                if skip_this_one:
                    img2.clearLayers()
                    newImg.clearLayers()
                    continue
                img2.drawText(text, x, y, colors[i], fontSize)
                if random.randint(0, 10) < 2:
                    img = newImg + img2.applyLayers()
                    train_csv.write(text + "," + ",".join(str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 1:
                    img = newImg + img2.applyLayers().warp(clockwise_quadrilateral(w, h))
                    train_csv.write(text + "," + ",".join(str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 1:
                    img = newImg + img2.applyLayers().rotate(random.randint(-90, 90))
                    train_csv.write(text + "," + ",".join(str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 8:  # warp
                    img = newImg + img2.applyLayers().rotate(random.randint(-90, 90)).warp(
                            clockwise_quadrilateral(w, h))
                    train_csv.write(text + "," + ",".join(str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 5:  # salt&pepper
                    img = add_noise(img, random.random() * 0.0025)
                    train_csv.write(text + "," + ",".join(str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                # img=img.smooth(sigma=random.random()*3,spatial_sigma=random.random()*3)
                # if random.randint(0,10)<1:
                #    img=add_noise(img,random.random()*0.001)
                img2.clearLayers()

                if random.randint(0, 10) < 11:  # always true
                    img = img.blur((random.randint(1, random.randint(2, fs // 5 + 4)),
                                    random.randint(1, random.randint(2, fs // 5 + 4))))
                    if random.randint(0, 10) < 7:
                        img = img.invert()
                    train_csv.write(text + "," + ",".join(str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                    # img.show()
                    # time.sleep(0.5)
                    # file_to_save = fname[:-4:] + "_" + text + str(font.replace("/", "_")) + fname[-4::]
                    # print(file_to_save)
                    # img.save(path + file_to_save)  # str(font.replace("/","_"))+fname[-4::])

                    # Popen(['/home/agp/Desktop/scenetext/code/DetectText/./DetectText',
                    #       file_to_save,
                    #       '/home/agp/Desktop/scenetext/code/DetectText/result.png', '1'])
                    # Popen('rm temp.png',shell=True,stdin=PIPE,stdout=PIPE)
                    # print("swt:")
                    # Image('SWT.png').show()
                    # time.sleep(5)

                # else:
                #    img.save(sys.argv[1][:-4:]+"_"+str(font)+sys.argv[1][-4::])
                # newImg.clearLayers()
                i += 1
예제 #10
0
def generate_letter_images():
    """generate a new data of warped characters [a-zA-Z0-9]"""
    if "(" in sys.argv[1]:  # assume a dimension input and an empty bg
        [w, h] = sys.argv[1][1:-1:].split(',')
        print(int(w) * int(h))
    with open(
            "/home/agp/workspace/deep_learning/streetView/new_dataset/train_with_inverted.csv",
            "w") as train_csv:
        train_csv.write(
            "label," +
            ",".join(["pixel" + str(i)
                      for i in range(0,
                                     int(w) * int(h))]) + "\n")
        for text in string.ascii_letters + string.digits:  # for all alphanumeric
            print(text)
            if "(" in sys.argv[1]:  # assume a dimension input and an empty bg
                img = Image(
                    tuple([int(i) for i in sys.argv[1][1:-1:].split(',')]))
                fname = "test_input.Bmp"
            else:
                img = Image(
                    sys.argv[1]).invert()  # background e.g. Image((150,100))#
                fname = sys.argv[1]
            path = "/home/agp/workspace/deep_learning/streetView/new_dataset/"
            sys.stdout.flush()
            # text = sys.argv[2] # e.g. "A.G.Polat"
            w = img.width
            h = img.height

            # extended fonts: wont work in windows!
            if False and "Linux" in platform.system():  # skip them for now
                fonts = Popen('find /usr/share/fonts | grep ttf',
                              shell=True,
                              stdin=PIPE,
                              stdout=PIPE).stdout.read().strip().split(
                                  '\n') + img.dl().listFonts()
            else:
                fonts = img.dl().listFonts()
            # print(fonts)
            # fonts=xfonts+fonts
            colors = [(random.randint(0, 255), random.randint(0, 255),
                       random.randint(0, 255)) for _ in range(0, len(fonts))]
            i = 0
            newImg = img.copy()
            img2 = Image(img.size())
            for font in fonts:
                if font == "" or font in [
                        "qbicle1brk", "dblayer1brk", "dblayer3brk",
                        "droidsanshebrew", "mrykacstqurn", "zoetropebrk",
                        "lohitpunjabi", "loopybrk", "kacstqurn",
                        "droidsansgeorgian", "mallige", "webdings", "3dletbrk",
                        "scalelinesmazebrk", "kacstpen", "nucleusbrk",
                        "unresponsivebrk", "kacstart", "kacstdecorative",
                        "dblayer2brk", "kacstoffice", "binaryxbrk",
                        "binaryx01sbrk", "lklug", "symmetrybrk",
                        "linedingsbrk", "droidsansarmenian",
                        "zurklezoutlinebrk", "entangledlayerbbrk",
                        "kacstdigital", "skullcapzbrk", "kacstbook",
                        "konectoro1brk", "yourcomplexobrk", "opensymbol",
                        "18holesbrk", "droidsansjapanese", "qbicle4brk",
                        "qbicle3brk", "lohitdevanagari", "doublebogeybrk",
                        "binary01sbrk", "kacstscreen", "lohitbengali",
                        "kacstposter", "kedage", "kacstone", "kacstnaskh",
                        "droidsansethiopic", "qbicle2brk", "vemana2000",
                        "kacstfarsi", "xmaslightsbrk", "headdingmakerbrk",
                        "kacstletter", "binarybrk", "ori1uni", "90starsbrk",
                        "codeoflifebrk", "saab", "zurklezsolidbrk",
                        "pothana2000", "kacsttitle", "fauxsnowbrk",
                        "lohitgujarati", "tetricidebrk", "lohittamil",
                        "kacsttitlel", "droidsansthai", "dblayer4brk",
                        "bitblocksttfbrk", "pindownbrk", "droidarabicnaskh"
                ]:  # font is no good
                    continue
                # if text == "":
                #    text=font.split("/")[-1]
                img2.dl().selectFont(font)
                y = random.randint(0, h)
                x = random.randint(0, w)
                fs = int(w / 1.1)
                fontSize = random.randint(random.randint(fs, w), int(w * 1.1))
                img2.dl().setFontSize(fontSize)  # font size between 2 and 100
                (w1, h1) = img2.dl().textDimensions(text)
                k = 0
                skip_this_one = False
                while x + w1 > w * 1.1 or y + h1 > h * 1.1:  # 1.2 for allowing text to be partly outside the box
                    k += 1
                    y = random.randint(0, h)
                    x = random.randint(0, w)
                    # if k>5:
                    #    y=random.randint(0,h//3+int(random.random()*0.6*h))
                    #    x=random.randint(0,int(w*random.random()*0.4))
                    if k > 500:
                        skip_this_one = True  # the font is too big for the image dimensions
                        print("skipping the big font:" + str(font))
                        break
                    fontSize = int(0.95 * fontSize)
                    if fontSize < w // 5:
                        fontSize = random.randint(random.randint(fs, w - 1), w)
                    fs -= 2
                    if fs < 3:
                        fs = random.randint(2, w - 1)
                    img2.dl().setFontSize(fontSize)
                    (w1, h1) = img2.dl().textDimensions(text)
                if skip_this_one:
                    img2.clearLayers()
                    newImg.clearLayers()
                    continue
                img2.drawText(text, x, y, colors[i], fontSize)
                if random.randint(0, 10) < 2:
                    img = newImg + img2.applyLayers()
                    train_csv.write(text + "," + ",".join(
                        str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 1:
                    img = newImg + img2.applyLayers().warp(
                        clockwise_quadrilateral(w, h))
                    train_csv.write(text + "," + ",".join(
                        str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 1:
                    img = newImg + img2.applyLayers().rotate(
                        random.randint(-90, 90))
                    train_csv.write(text + "," + ",".join(
                        str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 8:  # warp
                    img = newImg + img2.applyLayers().rotate(
                        random.randint(-90, 90)).warp(
                            clockwise_quadrilateral(w, h))
                    train_csv.write(text + "," + ",".join(
                        str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                if random.randint(0, 10) < 5:  # salt&pepper
                    img = add_noise(img, random.random() * 0.0025)
                    train_csv.write(text + "," + ",".join(
                        str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                # img=img.smooth(sigma=random.random()*3,spatial_sigma=random.random()*3)
                # if random.randint(0,10)<1:
                #    img=add_noise(img,random.random()*0.001)
                img2.clearLayers()

                if random.randint(0, 10) < 11:  # always true
                    img = img.blur(
                        (random.randint(1, random.randint(2, fs // 5 + 4)),
                         random.randint(1, random.randint(2, fs // 5 + 4))))
                    if random.randint(0, 10) < 7:
                        img = img.invert()
                    train_csv.write(text + "," + ",".join(
                        str(x) for x in misc.img_to_1d_gray(img)) + "\n")
                    # img.show()
                    # time.sleep(0.5)
                    # file_to_save = fname[:-4:] + "_" + text + str(font.replace("/", "_")) + fname[-4::]
                    # print(file_to_save)
                    # img.save(path + file_to_save)  # str(font.replace("/","_"))+fname[-4::])

                    # Popen(['/home/agp/Desktop/scenetext/code/DetectText/./DetectText',
                    #       file_to_save,
                    #       '/home/agp/Desktop/scenetext/code/DetectText/result.png', '1'])
                    # Popen('rm temp.png',shell=True,stdin=PIPE,stdout=PIPE)
                    # print("swt:")
                    # Image('SWT.png').show()
                    # time.sleep(5)

                # else:
                #    img.save(sys.argv[1][:-4:]+"_"+str(font)+sys.argv[1][-4::])
                # newImg.clearLayers()
                i += 1