Exemple #1
0
def main():

    dataDir = sys.argv[1]
    dataType = "train2014"

    plt.switch_backend("TkAgg")
    pylab.rcParams['figure.figsize'] = (15.0, 10.0)

    ct = coco_text.COCO_Text(sys.argv[2])
    ct.info()

    # get all images containing at least one instance of legible text
    imgIds = ct.getImgIds(imgIds=ct.train, catIds=[('legibility', 'legible')])

    while True:
        # pick one at random
        img = ct.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]

        I = io.imread('%s/images/%s/%s' %
                      (dataDir, dataType, img['file_name']))
        print '/images/%s/%s' % (dataType, img['file_name'])
        plt.figure()
        annIds = ct.getAnnIds(imgIds=img['id'])
        anns = ct.loadAnns(annIds)
        ct.showAnns(anns)
        plt.imshow(I)
        plt.show()
Exemple #2
0
 def __init__(self, ann_path, dataDir):
     super(ImageDataset, self).__init__()
     # link annotation
     self.ct = coco_text.COCO_Text(ann_path)
     # save the path to picture data
     self.dataDir = dataDir
     self.add_class("cocotext", 1, "text")
     imgIds = self.ct.getImgIds(imgIds=self.ct.train,
                                catIds=[('legibility', 'legible')])
     for imgId in imgIds:
         image_array = self.ct.loadImgs(imgId)[0]
         # '%s/%s'%(self.dataDir,img['file_name']) is to get the file path
         self.add_image("cocotext",
                        image_id=imgId,
                        path=self.dataDir + "/" + image_array['file_name'],
                        height=image_array["height"],
                        width=image_array["width"])
Exemple #3
0
def ablate(imgIds=[],
           mode='destroy',
           out_path="tmp",
           coco=coco,
           ct=None,
           **args):
    """[ablation entry point 2.0]
    Created to accomodate background-destroying ablation. Will dispatch all
    old ablations (gaussian, blackout, & median) to gen_ablation."""

    if ct is None:
        ct = coco_text.COCO_Text(os.path.join(CD, 'COCO_Text.json'))
    if imgIds == []:
        imgIds = ct.getImgIds(imgIds=ct.train,
                              catIds=[('legibility', 'legible')])
        imgIds = [imgIds[np.random.randint(0, len(imgIds))]]

    #dispatch to old ablation entry point
    if mode in ['gaussian', 'blackout', 'median']:
        return gen_ablation(imgIds, mode, ct, out_path=out_path, **args)

    #else do destroy_bg
    if coco is None:
        coco = COCO('%s/annotations/instances_%s.json' %
                    (DATA_PATH, DATA_TYPE))
    imgs = coco.loadImgs(imgIds)
    results = []
    for idx, img in enumerate(imgs):
        print("Ablating image {}/{} with id {} ".format(
            idx + 1, len(imgIds), img['id']))
        ori_file_name = os.path.join(CD, DATA_PATH, DATA_TYPE,
                                     img['file_name'])
        orig = io.imread(ori_file_name)

        if mode == 'destroy':
            ablt = destroy_bg(orig, img['id'], coco, **args)
        elif mode == 'median_bg':
            ablt = median_bg(orig, img['id'], coco, **args)

        out_file_name = os.path.join(CD, "..", out_path,
                                     "%s_%s" % (mode, img['file_name']))
        io.imsave(out_file_name, ablt)

        results.append((img['id'], ori_file_name, out_file_name))
    return results
Exemple #4
0
def main():
    global id2word
    global lda_model
    global num_topics

    logging.basicConfig(stream=sys.stdout, level=logging.INFO)

    # Initialize tensorflow params.
    tensorflow_init()
    # Write the results in the .txt file.
    f = open('results_50.txt', 'w')
    # First, read the dictionary and make a dictionary.
    id2word = gensim.corpora.Dictionary.load_from_text(
        '/home/avinash/Desktop/SMAI/DynamicLexiconGenerationCVC/DynamicLexiconGeneration/train_gensim_dict.txt'
    )

    # Specify the number of topics present in the LDA_model.
    num_topics = 50

    # Initiate COCO-Text API instance.
    coco_txt = coco_text.COCO_Text(
        '/home/avinash/Desktop/SMAI/DynamicLexiconGenerationCVC/DynamicLexiconGeneration/Dataset/COCO_Text.json'
    )
    im_ids_txt = coco_txt.getImgIds(imgIds=coco_txt.train)

    # Image base_url.
    base_url_image = '/home/avinash/Desktop/SMAI/images/train2014/'

    # Parse the dictionary and Pre-process.
    iterator = id2word.iteritems()
    keys = id2word.keys()
    dict_natural = {}
    for i in range(0, len(keys)):
        temp_item = iterator.next()
        try:
            dict_natural[str(temp_item[1]).lower()] = []
        except:
            pass

    # Now, read the LDA model built on Natural dictionary.
    lda_model = gensim.models.ldamodel.LdaModel.load(
        '/home/avinash/Desktop/SMAI/DynamicLexiconGenerationCVC/DynamicLexiconGeneration/lda_model_50.lda',
        mmap='r')

    # Read, all the Validation set captions.
    # Combine each of them and make a document
    # Project the document on the LDA model.
    # Please verify with the CNN validation labels.

    # NOTE :The validation set of CNN is used here as
    # Evaluation set for word-ranking.
    eval_file = open(
        '/home/avinash/Desktop/SMAI/DynamicLexiconGenerationCVC/DynamicLexiconGeneration/dataset_corpus_train.json',
        'r')
    eval_json = json.load(eval_file)

    # Get all the topics only once.
    # YOU FETCH ONLY ONCE B-).
    # I'm bond! James Bond B-).

    # Iterate over each instance of eval set.
    counter_image = 0
    counter_word_instance = 0
    for ind in im_ids_txt:
        # Keep printing the counter values.
        counter_image += 1
        if counter_image == 5000:
            break
        #print ("Total 20K : " + str(counter_image))
        words_present = []
        try:
            value = eval_json[str(ind)]
        except:
            continue
        captions = value['caption']
        url = value['url']
        final_url = base_url_image + str(url)
        ann_ids_txt = coco_txt.getAnnIds(imgIds=ind)
        ann_txt = coco_txt.loadAnns(ann_ids_txt)
        for j in ann_txt:
            try:
                if j['utf8_string'] != '':
                    counter_word_instance += 1
                    words_present.append(j['utf8_string'])
            except KeyError:
                pass
        if not words_present:
            pass
        else:
            # For a given image now we have prob distribution
            # and words_present in the image. Now, we need to
            # make all ranked dictionary and make inference
            # for each and everyword.
            # REST ALL THE SHIT GOES HERE.
            try:
                query_out = make_query(final_url)
            except:
                continue

            for topic in query_out:
                topic_words = [
                    x for x in lda_model.show_topic(
                        topic[0], topn=len(dict_natural.keys()))
                ]
                for word in topic_words:
                    try:
                        p_topic = topic[1]
                        p_word = word[1]
                        p_final = p_topic * p_word
                        dict_natural[str(word[0])].append(p_final)
                    except:
                        pass
            # Choose the highest values for each of the words.
            topic_word_ranks = []
            for j in dict_natural.keys():
                try:
                    topic_word_ranks.append(
                        (str(j), sum(dict_natural[str(j)])))
                except:
                    pass
            # Sort the dictionary and make final ranking prediction.
            final_ranking = sorted(topic_word_ranks,
                                   key=lambda x: x[1],
                                   reverse=True)
            final_ranks = []
            for i in final_ranking:
                final_ranks.append(i[0].lower())
            text_ranks = []
            for text in words_present:
                try:
                    text_ranks.append(
                        str(text) + " : " +
                        str(final_ranks.index(str(text.lower()))))
                except ValueError:
                    try:
                        text_ranks.append(str(text) + " : NA")
                    except:
                        text_ranks.append("Unicode error")
            f.write(str(text_ranks) + "\n")
            f.write("Image url : " + str(url) + "\n")
            f.write("Total words : " + str(len(final_ranks)) + "\n")
            f.write("---------------------------------------\n")
            print(str(text_ranks))
            print("Image url : " + str(url) + "\n")
            print("Total words : " + str(len(final_ranks)))
            print("---------------------------------------")
        # At the end make sure to make each word probs zero.
        # For next iteration.
        for i in dict_natural.keys():
            dict_natural[i] = []
    print("counter_word_instances : " + str(counter_word_instance))
    f.close()
Exemple #5
0
import os
import sys

ROOT_DIR = os.getcwd()
#ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
COCO_DIR = os.path.join(ROOT_DIR, 'COCO')
TEXT_DIR = os.path.join(COCO_DIR, 'coco-text')

sys.path.append(COCO_DIR)
sys.path.append(TEXT_DIR)

from ctpn_crnn_pytorch import *

import coco_text
ct = coco_text.COCO_Text(os.path.join(TEXT_DIR, 'COCO_Text.json'))
imgs_val = ct.val
imgs_all = ct.imgs.keys()

dataType = 'val'
imgs_used = imgs_val


def make_result_dic(utf8_string, image_id, bbox):
    result = {"utf8_string": utf8_string, "image_id": image_id, "bbox": bbox}
    return result


def infer_on_coco():
    total_results = []
    for i, img_id in enumerate(imgs_used):
Exemple #6
0
 def __init__(self):
     self.ct = coco_text.COCO_Text("../dataset/annotation/COCO_Text.json")
     print(self.ct.info())
Exemple #7
0
win_unicode_console.enable()
import os
import sys
import numpy as np
import cv2

ROOT_DIR = os.getcwd()
#ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(ROOT_DIR, 'coco-text'))
sys.path.append(os.path.join(ROOT_DIR, 'train2014'))
sys.path.append(os.path.join(ROOT_DIR, 'test2014'))
sys.path.append(os.path.join(ROOT_DIR, 'val2014'))

import coco_text

ct = coco_text.COCO_Text('coco-text/COCO_Text.json')

imgs_train = ct.getImgIds(imgIds=ct.train,
                          catIds=[('legibility', 'legible'),
                                  ('language', 'english')])
imgs_val = ct.getImgIds(imgIds=ct.val,
                        catIds=[('legibility', 'legible'),
                                ('language', 'english')])
imgs_test = ct.getImgIds(imgIds=ct.test,
                         catIds=[('legibility', 'legible'),
                                 ('language', 'english')])


def make_for_crnn(imgIds, dataType):
    csv_out = 'COCO_Text_' + dataType + '.csv'
    print('CSV opened', csv_out)
from pascal_voc_writer import Writer

import sys
sys.path.append('/media/weihao/DISK0/Object_detection/COCO/coco-text-master')
import coco_text

def has_digits(s):
    for a in range(len(s)):
        if s[a].isnumeric():
            return True
    return False

string_tag = 'utf8_string'

ct = coco_text.COCO_Text('/media/weihao/DISK0/Object_detection/COCO/COCO_Text.json')
ct.info()

# imgs = ct.getImgIds(imgIds=ct.train,
#                     catIds=[('legibility','legible'),('class','machine printed')])
# anns = ct.getAnnIds(imgIds=ct.val,
#                     catIds=[('legibility','legible'),('class','machine printed')],
#                     areaRng=[1000,200000])
dataDir='/home/weihao/Downloads'
dataType='train2017'
output_dir = '/home/weihao/tmp/data/'
if output_dir is not None:
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.mkdir(output_dir)
import coco_text
import os, json, sys
import coco_evaluation
ct = coco_text.COCO_Text('../COCO_Text.json')
test_path = sys.argv[1]
dataDir = '../train2014'
dataType = 'train2014'
test = []
print "test path: {}".format(test_path)
imgIds = []
for txt in os.listdir(test_path):
    if 'txt' in txt:
        im_id = int(txt.split('_')[-1].replace('.txt', ''))
        with open(os.path.join(test_path, txt)) as f:
            content = f.readlines()
        imgIds.append(im_id)
        for line in content:
            line = map(int, line.split(',')[:8])
            x_axis = [line[0], line[2], line[4], line[6]]
            y_axis = [line[1], line[3], line[5], line[7]]
            x_coord = min(x_axis)
            y_coord = min(y_axis)
            width = max(x_axis) - min(x_axis)
            height = max(y_axis) - min(y_axis)
            test.append({
                "image_id": im_id,
                "bbox": [x_coord, y_coord, width, height]
            })

print len(test)
os.system('touch results.json')
Exemple #10
0
#--------------rectify the file_name of the imgs in 'val'--------
# import coco_text
# ct = coco_text.COCO_Text(gtJson)
# imgIds = ct.getImgIds(imgIds = ct.val)
# ct.changeImgNames(imgIds)
# import json
# with open(gtJsonNew, 'w') as jsonFile:
#     json.dump(ct.dataset, jsonFile)
# print 'ok!'

#-------------create the new dir for the coco-text image ------------
import coco_text
import os
import shutil

ct = coco_text.COCO_Text(gtJson)
imgIdVal = ct.getImgIds(imgIds=ct.val)
if not os.path.exists(imgDestValDir):
    os.mkdir(imgDestValDir)
for num, eachId in enumerate(imgIdVal):
    imgName = ct.imgs[eachId]['file_name']
    sourceDir = '{}/{}'.format(imgSourceDir, imgName)
    targetDir = '{}/{}'.format(imgDestValDir, imgName)
    shutil.copy(sourceDir, targetDir)
    if not (num % 500):
        print num

imgIdTrain = ct.getImgIds(imgIds=ct.train)
if not os.path.exists(imgDestTrainDir):
    os.mkdir(imgDestTrainDir)
for num, eachId in enumerate(imgIdTrain):
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab as pl
import pylab
from matplotlib.pyplot import *
import cv2
import sys
from PIL import Image

if __name__ == '__main__':

    for loop in range(0, 2):  # loop and pick random pictures

        #API offers some basic infos of the dataset.
        ct = coco_text.COCO_Text('/COCO_Data_set/COCO_Text.json')
        ct.info()

        #Select annotations and images based on filter criteria
        # get all images containing at least one instance of legible text
        #retrieve some images. We want to get a list of all image ids from the training set,
        #where the image contains at least one text instance that is legilbe and is machine printed.

        imgs = ct.getImgIds(imgIds=ct.train,
                            catIds=[('legibility', 'legible'),
                                    ('class', 'machine printed')])

        #all annotation ids from the validation set that are legible, machine printed
        #and have an area between 0 and 200 pixels.
        anns = ct.getAnnIds(imgIds=ct.val,
                            catIds=[('legibility', 'legible'),
import coco_text
import json

ct = coco_text.COCO_Text('COCO_Text.json')
ct.info()

# Some image retreival...To get a list of all those image ids from training set where at least one text instance is  there, i.e, all legible images and are machine printed
imgs = ct.getImgIds(imgIds=ct.train,
                    catIds=[('legibility', 'legible'),
                            ('class', 'machine printed')])
# imgs = ct.getImgIds(imgIds=ct.val, catIds=[('legibility','legible'), ('class','machine printed')])
# imgs = ct.getImgIds(imgIds=ct.train, catIds=[('legibility','legible')])
# imgs = ct.getImgIds(imgIds=ct.train)
# imgs = ct.getImgIds(imgIds=ct.val)
# imgs = ct.getImgIds(imgIds=ct.test)

print(type(imgs))
print(imgs)
print(len(imgs))

# Annotation Ids retreival form the validation set that are legible, machine printed and have an area between 0 and 200 pixel
anns = ct.getAnnIds(imgIds=ct.val,
                    catIds=[('legibility', 'legible'),
                            ('class', 'machine printed')],
                    areaRng=[0, 200])
# anns = ct.getAnnIds(imgIds=ct.train, catIds=[('legibility','legible'),('class','machine printed')], areaRng=[0,200])
# anns = ct.getAnnIds(imgIds=ct.train, catIds=[('legibility','legible'),('class','machine printed')])

# print(type(anns))
# print(anns)
# print(len(anns))
Exemple #13
0
    for img_path in tqdm(img_paths):
        raw_pred, text = recognition.recognize_cropped(Image.open(img_path))
        file_num = img_path.split('/')[-1].split('.')[0]
        result = '{},{}'.format(file_num, text)
        print(result, file=task2_f)
    task2_f.close()


def force_mkdirs(path):
    if os.path.exists(path):
        shutil.rmtree(path)
    os.makedirs(path)


ct = coco_text.COCO_Text(COCO_DATA + 'COCO_Text.json')
#force_mkdirs(TASK1_PATH)
#force_mkdirs(TASK3_PATH)

thresholds = [0.6, 0.0]

for threshold in thresholds:
    path = str(int(threshold * 100)) + VAL_RESULT
    print(
        'Preparing validation results for Task 1 at {}\n Task 3 at {}'.format(
            TASK1_PATH + path, TASK3_PATH + path))
    force_mkdirs(TASK1_PATH + path)
    force_mkdirs(TASK3_PATH + path)
    valid_img_ids = ct.getImgIds(imgIds=ct.val)
    valid_imgs = ct.loadImgs(valid_img_ids)
    coco_competition_result(valid_imgs, INPUT_PATH, TASK1_PATH + path,
import coco_text
from nltk.corpus import stopwords
import json

# This script is to make dictionary from all the
# words in the dataset. Thus this include both 
# training and validation images from COCO-Text.

dataset_dictionary_all = []
stop_words = set(stopwords.words('english'))
stop_words_str = [str(i).lower() for i in stop_words]
stop_words_post_space = [str(i) + " " for i in stop_words_str]
stop_words_pre_space = [" " + str(i) for i in stop_words_str]
print str(stop_words_str)
cntr = 0
coco_txt = coco_text.COCO_Text('/home/avinash/Desktop/SMAI/DynamicLexiconGenerationCVC/DynamicLexiconGeneration/Dataset/COCO_Text.json')

# First make on Training set.
im_ids_train = coco_txt.getImgIds(imgIds=coco_txt.train)  # I think it should be coco_txt.train not coco_txt.val.. Modifying it
#im_ids_train = coco_txt.getImgIds(imgIds=coco_txt.val)
for i in im_ids_train:
    ann_ids_txt = coco_txt.getAnnIds(imgIds = i)
    ann_txt = coco_txt.loadAnns(ann_ids_txt)
    for j in ann_txt:
        try:
            if j['utf8_string'] != '':
                try:
                    given_word = str(j['utf8_string']).lower()
                    if ((given_word not in dataset_dictionary_all) and (given_word not in stop_words_str) and (given_word not in stop_words_post_space) and (given_word not in stop_words_pre_space)):
                        cntr += 1
                        dataset_dictionary_all.append(given_word)
Exemple #15
0
INPUT_PATH = os.path.join(FD, 'input')
INPUT_FILE = 'test.pkl'  # contains imgIds to compute the score for
OUTPUT_PATH = os.path.join(FD, 'output')
IMG_PATH = os.path.join(FD, 'data', 'coco')
IMG_TYPE = 'train2014'  # input directory to sample from
CAPTION_PATH = os.path.join(FD, 'neuraltalk2')  # captioning code folder
MODEL_PATH = os.path.join(FD, 'model', 'neuraltalk2',
                          'model_id1-501-1448236541.t7')

COCO_PATH = os.path.join(FD, 'data', 'coco')
COCO_ANNO_PATH = os.path.join(COCO_PATH, 'annotations')
COCO_TEXT_PATH = os.path.join(FD, 'coco-text')
sys.path.insert(0, COCO_TEXT_PATH)
import coco_text as ct
ct = ct.COCO_Text(os.path.join(COCO_TEXT_PATH, 'COCO_Text.json'))

sys.path.insert(0, os.path.join(FD, 'coco', 'PythonAPI'))
from pycocotools.coco import COCO
coco = ablation.coco


def run(amode='gaussian',
        input_file=INPUT_FILE,
        output_file=INPUT_FILE,
        batch_size=1,
        category=''):

    # Clean up the previous results.
    print("Cleaning up")
    abs_tmp_dir = os.path.join(FD, "tmp_%s" % output_file)
def format_coco_text():
    print 'format coco_text dataset: 80percent training, 10percent valing, 10percent testing'
    # read annotations
    # in : annotate_id imagename    bbox(xmin,ymin,w,h);
    # out: imgprefix label(text)    bbox1(xmin,ymin,xmax,ymax)
    #      imgprefix label(text)    bbox2
    #  import the annotations of coco-text
    if not os.path.exists('train2014'):
        print 'train2014/ not found, please unzipping'
        return -1
    if not os.path.exists('COCO_Text.json'):
        print 'COCO_Text.json not found, please unzipping'
        return -1

    train_file = open('formatted_dataset/ImageSets/Main/train.txt', 'w')
    trainval_file = open('formatted_dataset/ImageSets/Main/trainval.txt', 'w')
    test_file = open('formatted_dataset/ImageSets/Main/test.txt', 'w')
    val_file = open('formatted_dataset/ImageSets/Main/val.txt', 'w')

    annotation_in = coco_text.COCO_Text('COCO_Text.json')
    annotation_out = open('formatted_dataset/images.annotations', 'w')

    # select training image
    ann_ids = annotation_in.getAnnIds(imgIds=annotation_in.train,
                                      catIds=[('legibility', 'legible'),
                                              ('class', 'machine printed')])
    print 'train annotations:' + str(len(ann_ids))
    anns = annotation_in.loadAnns(ann_ids)
    imgid_set = set()
    for ann in anns:
        im_id_str = str(ann['image_id'])
        imgprefix = im_id_str
        for i in xrange(0, 12 - len(im_id_str)):
            imgprefix = '0' + imgprefix
        imgprefix = 'COCO_train2014_' + imgprefix
        img_name = imgprefix + '.jpg'
        # images.annotations
        bbox = ann['bbox']
        xmin = int(round(bbox[0]))
        ymin = int(round(bbox[1]))
        xmax = int(round(bbox[0] + bbox[2]))
        ymax = int(round(bbox[1] + bbox[3]))
        annotation_out.write(img_name + ' text ' + str(xmin) + ' ' +
                             str(ymin) + ' ' + str(xmax) + ' ' + str(ymax) +
                             '\n')
        if not ann['image_id'] in imgid_set:
            # ImageSets train
            train_file.write(imgprefix + '\n')
            trainval_file.write(imgprefix + '\n')
            # JPEGImages train
            if not os.path.isfile('formatted_dataset/JPEGImages/' + img_name):
                os.system('mv train2014/' + img_name +
                          ' formatted_dataset/JPEGImages')
        imgid_set.add(ann['image_id'])

    # select valing and testing image
    ann_ids = annotation_in.getAnnIds(imgIds=annotation_in.val,
                                      catIds=[('legibility', 'legible'),
                                              ('class', 'machine printed')])
    print 'val annotations:' + str(len(ann_ids))
    anns = annotation_in.loadAnns(ann_ids)
    imgid_set = set()
    cnt = 0
    for ann in anns:
        cnt += 1
        im_id_str = str(ann['image_id'])
        imgprefix = im_id_str
        for i in xrange(0, 12 - len(im_id_str)):
            imgprefix = '0' + imgprefix
        imgprefix = 'COCO_train2014_' + imgprefix
        img_name = imgprefix + '.jpg'
        # images.annotations
        bbox = ann['bbox']
        xmin = int(round(bbox[0]))
        ymin = int(round(bbox[1]))
        xmax = int(round(bbox[0] + bbox[2]))
        ymax = int(round(bbox[1] + bbox[3]))
        annotation_out.write(img_name + ' text ' + str(xmin) + ' ' +
                             str(ymin) + ' ' + str(xmax) + ' ' + str(ymax) +
                             '\n')
        if not ann['image_id'] in imgid_set:
            # ImageSets train or test
            if cnt % 4 == 1:
                test_file.write(imgprefix + '\n')
            else:
                val_file.write(imgprefix + '\n')
                trainval_file.write(imgprefix + '\n')
            # JPEGImages val or test
            if not os.path.isfile('formatted_dataset/JPEGImages/' + img_name):
                os.system('mv train2014/' + img_name +
                          ' formatted_dataset/JPEGImages')
        imgid_set.add(ann['image_id'])
Exemple #17
0
def whole_split(opt):
    imgCls = [ct.train, ct.val, ct.test]
    split_div = ["train", "val", "test"]
    # Load COCO_Text.json here
    ct = coco_text.COCO_Text(opt.COCO_path)
    sp = 0
    for i in imgCls:
        now_path = os.getcwd()
        ann_path = now_path + "/" + split_div[sp] + "_anns"
        set_path = now_path + "/coco_" + split_div[sp]
        # Start creating new folders to save annotations and images
        try:
            os.mkdir(ann_path)
        except OSError:
            print("Creation of the directory %s failed" % ann_path)
        else:
            print("Successfully created the directory %s " % ann_path)
        try:
            os.mkdir(set_path)
        except OSError:
            print("Creation of the directory %s failed" % set_path)
        else:
            print("Successfully created the directory %s " % set_path)
        # Load image ids for different set here
        imgs = ct.getImgIds(imgIds=i)
        anns = ct.getAnnIds(imgIds=i)

        cnt = 0
        total_img_num = len(imgs)
        for j in imgs:
            ind = 0
            img_info = {}
            ind_info = {}
            img = ct.loadImgs(j)[0]
            fName = img['file_name']
            iId = img['id']
            img_info["img_id"] = iId
            img_info["file_name"] = fName
            # remember to extract width, height
            #img_info["width"]=img['width']
            #img_info["height"]=img['height']

            already_saved_this_img = False
            have_text = False

            for k in anns:
                ann = ct.loadAnns(k)[0]
                if (ann["image_id"] == iId):
                    aBbox = ann["bbox"]
                    img_info["bbox"] = aBbox
                    if "utf8_string" in ann:
                        have_text = True
                        aTran = ann["utf8_string"]
                        img_info["transcription"] = aTran
                        ind_info[ind] = img_info
                        ind += 1
                        if (not already_saved_this_img):
                            trg_path = set_path + "/"
                            src_path = opt.img_path + fName
                            try:
                                copy(src_path, trg_path)
                            except IOError as e:
                                print("Unable to copy file. %s" % fName)
                            except:
                                print("Unexpected error:", sys.exc_info())
                            cnt += 1
                            already_saved_this_img = True

            if have_text:
                savepath = ann_path + "/" + fName[:-3] + "json"
                saveAnn = open(savepath, "w")
                json.dump(ind_info, saveAnn)
                saveAnn.close()
        sp += 1

        if cnt == total_img_num: output = "Copying Finished Successfully!"
        elif cnt == 0: output = "No one get copied."
        else:
            output = "Copying Finished Successfully! But some images are not copied."
Exemple #18
0
def yolo_split(opt):
    # Load COCO_Text.json here
    ct = coco_text.COCO_Text(opt.COCO_path)
    imgCls = [ct.val, ct.test, ct.train]
    split_div = ["valid", "test", "train"]

    now_path = os.getcwd()
    cls_path = now_path + "/classes.names"
    clspath = open(cls_path, "w")
    classes = []
    cls_dict = {}
    cls_num = 0
    sp = 0
    for i in imgCls:
        ann_path = now_path + "/labels/" + split_div[sp]
        set_path = now_path + "/images/" + split_div[sp]
        lst_path = now_path + "/" + split_div[sp] + ".txt"

        # Start creating new folders to save annotations and images
        try:
            os.mkdir(ann_path)
        except OSError:
            print("Creation of the directory %s failed" % ann_path)
        else:
            print("Successfully created the directory %s " % ann_path)

        try:
            os.mkdir(set_path)
        except OSError:
            print("Creation of the directory %s failed" % set_path)
        else:
            print("Successfully created the directory %s " % set_path)

        # Load image ids for different set here
        imgs = ct.getImgIds(imgIds=i)
        anns = ct.getAnnIds(imgIds=i)
        # count how many images do we select
        cnt = 0
        total_img_num = len(imgs)

        lstpath = open(lst_path, "a")

        for j in imgs:
            # get image's description
            img = ct.loadImgs(j)[0]
            iId = img['id']
            fName = img['file_name']

            # save annotation for each image
            savepath = ann_path + "/" + fName[:-3] + "txt"

            # remember to extract width, height
            iWidth = img['width']
            iHeight = img['height']

            already_saved_this_img = False
            """    
            # this is for test set
            lstpath.write(set_path+"/"+fName+"\n")

            trg_path = set_path+"/"           
            src_path = opt.img_path+fName
            try:
                copy(src_path, trg_path)
            except IOError as e:
                print("Unable to copy file. %s" % fName)
            except:
                print("Unexpected error:", sys.exc_info())
            """

            for k in anns:
                # get ann's description
                ann = ct.loadAnns(k)[0]

                if (ann["image_id"] == iId):
                    aBbox = ann["bbox"]

                    if "utf8_string" in ann:
                        aTran = ann["utf8_string"].strip()
                        saveAnn = open(savepath, "a")

                        # start write
                        if aTran not in classes:
                            classes.append(aTran)
                            clspath.write(aTran + "\n")
                            cls_dict[cls_num] = aTran
                            #max_val = 640
                            #saveAnn.write(str(cls_num)+" "+str(aBbox[0]/max_val)+" "+str(aBbox[1]/max_val)+" "+str(aBbox[2]/max_val)+" "+str(aBbox[3]/max_val)+"\n")
                            saveAnn.write(
                                str(cls_num) + " " + str(aBbox[0] / iWidth) +
                                " " + str(aBbox[1] / iHeight) + " " +
                                str(aBbox[2] / iWidth) + " " +
                                str(aBbox[3] / iHeight) + "\n")
                            if cls_num <= 0 and cls_num > 26239: print(cls_num)
                            cls_num += 1
                        else:
                            cls_key = get_key(aTran, cls_dict)
                            #saveAnn.write(str(cls_num)+" "+str(aBbox[0]/max_val)+" "+str(aBbox[1]/max_val)+" "+str(aBbox[2]/max_val)+" "+str(aBbox[3]/max_val)+"\n")
                            saveAnn.write(
                                str(cls_key) + " " + str(aBbox[0] / iWidth) +
                                " " + str(aBbox[1] / iHeight) + " " +
                                str(aBbox[2] / iWidth) + " " +
                                str(aBbox[3] / iHeight) + "\n")
                        saveAnn.close()
                        if (not already_saved_this_img):
                            #save image directory and name for different division
                            lstpath.write(set_path + "/" + fName + "\n")

                            trg_path = set_path + "/"
                            src_path = opt.img_path + fName

                            try:
                                copy(src_path, trg_path)
                            except IOError as e:
                                print("Unable to copy file. %s" % fName)
                            except:
                                print("Unexpected error:", sys.exc_info())
                            cnt += 1
                            already_saved_this_img = True

                    #elif "utf8_string" not in ann:
                    #saveAnn.write("NULL "+str(aBbox[0])+" "+str(aBbox[1])+" "+str(aBbox[2])+" "+str(aBbox[3])+" "+str(iWidth)+" "+str(iHeight)+"\n")
                        """
                        if (not already_saved_this_img):
                            #save image directory and name for different division
                            lstpath.write(set_path+"/"+fName+"\n")
                            
                            trg_path = set_path+"/"
                            src_path = opt.img_path+fName
                            try:
                                copy(src_path, trg_path)
                            except IOError as e:
                                print("Unable to copy file. %s" % fName)
                            except:
                                print("Unexpected error:", sys.exc_info())
                            cnt+=1
                            already_saved_this_img = True
                       """
            #saveAnn.close()
        lstpath.close()

        sp += 1

        if cnt == total_img_num: output = "Copying Finished Successfully!"
        elif cnt == 0: output = "No one get copied."
        else:
            output = "Copying Finished Successfully! But some images are not copied."
        print(output)

    clspath.close()
    print("The class number is: ", cls_num)
    return print("Only One more step to finish")
Exemple #19
0
def add_negative_sampling_data(activ_D_folder,
                               COCO_folder,
                               total_negative_samples=1000,
                               testing_samples=100):

    # Create a folder to store negative sampling images
    negative_folder = join(activ_D_folder, "Negative")
    if not isdir(negative_folder):
        os.mkdir(negative_folder)
    if not isdir(join(negative_folder, "trainingFiles")):
        os.mkdir(join(negative_folder, "trainingFiles"))
    if not isdir(join(negative_folder, "testFiles")):
        os.mkdir(join(negative_folder, "testFiles"))

    # Get list of images with text from COCO-Text dataset
    ct = coco_text.COCO_Text('COCO_Text.json')
    negative_images = ct.loadImgs(
        ct.getImgIds(imgIds=ct.train,
                     catIds=[('legibility', 'legible'),
                             ('class', 'machine printed'),
                             ('language', 'english')
                             ])[:total_negative_samples + testing_samples])

    modes = ["training", "test"]

    for mode in modes:
        if mode == 'training':
            negative_images_subset = negative_images[:total_negative_samples]
        elif mode == 'test':
            negative_images_subset = negative_images[
                total_negative_samples:total_negative_samples +
                testing_samples]
        else:
            print("unknown mode; quitting")
            quit()

        # Start XML file text
        xml_file_output = '''<?xml version="1.0" encoding="UTF-8"?>\n\n<Protocol4 channel="Negative">\n\n'''

        counter = 0

        for negative_image_dict in negative_images_subset:
            #xml_file_output += '''<frame source="vd00" id="{0}">'''.format(str(counter))

            if not os.path.isdir(COCO_folder):
                print("COCO_folder not found at {0}".format(COCO_folder))
                quit()

            # Make path to COCO train2014 folder to load image
            negative_image = cv2.imread(
                join(COCO_folder, negative_image_dict['file_name']))
            suffix = negative_image_dict['file_name'][
                negative_image_dict['file_name'].rfind(".") + 1:]
            xml_file_output += '''<frame source="vd00" id="{0}" ext="{1}">\n'''.format(
                str(counter), suffix)

            # Get annotations
            annIds = ct.getAnnIds(imgIds=negative_image_dict['id'])
            anns = ct.loadAnns(annIds)
            rectangle_num = 0

            for ann in anns:
                rectangle_num += 1
                # Per COCO-Text “bbox” : [x,y,width,height],
                x, y, width, height = ann['bbox']
                xml_file_output += '''<rectangle id="{4}" height="{0}" width="{1}" y="{2}" x="{3}"/>\n'''.format(
                    int(height), int(width), int(y), int(x), rectangle_num)

            xml_file_output += '''</frame>\n'''

            # Save in Negative folder under AcTiV-D
            cv2.imwrite(
                join(negative_folder, mode + "Files",
                     "Negative_vd00_frame_" + str(counter) + "." + suffix),
                negative_image)

            counter += 1
            if counter % 200 == 0 and counter != total_negative_samples and counter != testing_samples:
                print("Added {0} negative {1} examples".format(counter, mode))

        # End XML file text
        xml_file_output += "\n</Protocol4>"

        #print(xml_file_output)
        with open(join(negative_folder, "g" + mode + "_Ne.xml"), 'w') as f:
            f.write(xml_file_output)

        # Print out final count
        if mode == 'training':
            print("Added {0} negative {1} examples".format(
                total_negative_samples, mode))
        elif mode == 'test':
            print("Added {0} negative {1} examples".format(
                testing_samples, mode))
    "i.e. COCO_Text.json")
parser.add_argument(
    "-i",
    "--imagedir",
    type=str,
    required=True,
    help="Directory containing the train images - coco text uses"
    "coco 2014 train images.")
parser.add_argument("-t",
                    "--targetdir",
                    type=str,
                    required=True,
                    help="Directory that will contain the yolo dataset.")
args = parser.parse_args()

ct = coco_text.COCO_Text(args.annotationfile)
ct.info()

#in contrast to other coco scripts i don't feel like it should be possible to specify labels to use for now
labels = ["machine printed legible", "machine printed illegible"]
dsTypes = ["train", "valid"]

if (not os.path.exists(args.targetdir)):
    os.makedirs(args.targetdir)

backUpDir = os.path.join(args.targetdir, "backup")
if (not os.path.exists(backUpDir)):
    os.makedirs(backUpDir)

with open(os.path.join(args.targetdir, "coco_text.data"), 'w') as dataFile:
    dataFile.write("classes = " + str(len(labels)) + "\n")
Exemple #21
0
                'image/object/bbox/ymax':
                dataset_util.float_list_feature(ymaxs),
                'image/object/class/text':
                dataset_util.bytes_list_feature(classes_text),
                'image/object/class/label':
                dataset_util.int64_list_feature(classes),
            }))
        writer.write(tf_example.SerializeToString())
        num_examples += 1
    return num_examples


if __name__ == "__main__":
    args = parse_arguments()
    train_or_val = args.train_or_val.lower()
    ct = coco_text.COCO_Text(args.cocotext_json)
    img_ids = ct.getImgIds(imgIds=ct.train, catIds=[('legibility', 'legible')]) \
        if train_or_val == 'train' else ct.getImgIds(imgIds=ct.val, catIds=[('legibility', 'legible')])

    seen = set()
    num_examples = 0
    writer = tf.python_io.TFRecordWriter(args.output_path)
    for img_id in img_ids:
        img = ct.loadImgs(img_id)[0]
        file_name = img['file_name']
        if file_name in seen:
            continue
        seen.add(file_name)
        train_val_dir = 'train2014'
        path = os.path.join(args.coco_imgdir, train_val_dir)
        pil_img = Image.open(os.path.join(path, file_name))
import coco_text
import cv2
import os

ct = coco_text.COCO_Text('cocotext.v2.json')
ct.info()
imgs = ct.getImgIds(imgIds=ct.val,
                    catIds=[('legibility', 'legible'),
                            ('class', 'machine printed')])
anns = ct.getAnnIds(imgIds=ct.val,
                    catIds=[('legibility', 'legible'),
                            ('class', 'machine printed')],
                    areaRng=[0, 200])

dataDir = '/Data3/data'
dataType = 'train2014'

import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab

pylab.rcParams['figure.figsize'] = (10.0, 8.0)

# get all images containing at least one instance of legible text
imgIds = ct.getImgIds(imgIds=ct.val, catIds=[('legibility', 'legible')])

#print(imgIds)
#print(len(imgIds))
# pick one at random
path = '/Data3/coco_val1/'