def main(argv): del argv c = 0 # to count how many images it's gone through s = 0 # to count how many images it's skipped data = json.load(open(flags.ann_dir, 'r')) processor = preprocess.Processor(data, flags.pickle, flags.is_training) number_of_data = len(processor.selected_caption) number_of_data_per_shard = number_of_data // flags.shards save_dir = './train_tfrecords/' if flags.is_training else './eval_tfrecords/' if not tf.gfile.IsDirectory(save_dir): tf.gfile.MakeDirs(save_dir) writers = [tf.io.TFRecordWriter(save_dir +'tfrecord_'+ str(i)) for i in range(flags.shards)] print('Processing %d images' % number_of_data) for idx, items in enumerate(zip(processor.selected_caption, processor.selected_image_id)): caption, image_id = items img_path = os.path.join(flags.img_dir, processor.img_idx2file[image_id]) try: img = tf.io.gfile.GFile(img_path, 'rb').read() except: s += 1 print(img_path, "has been skipped") continue c += 1 converted_sentence = processor.converter(caption) caption_data = processor.padding(converted_sentence) features = {'image': _bytes_feature([img]), 'caption': _int_feature(caption_data)} example = tf.train.Example(features=tf.train.Features(feature=features)) shard_idx = idx // number_of_data_per_shard if shard_idx >= flags.shards: shard_idx -= 1 writers[shard_idx].write(example.SerializeToString()) print('%d images in total' % c) print('%d images have been skipped' % s)
def get_image_and_answer(annotation_path): data = json.load(open(annotation_path, 'r')) processor = preprocess.Processor(data, flags.pickle, False) converted_captions = [ processor.converter(words) for words in processor.selected_caption ] image_names = [ processor.img_idx2file[idx] for idx in processor.selected_image_id ] img_name2answers = collections.defaultdict(list) for img_name, answer in zip(image_names, converted_captions): img_name2answers[img_name].append(answer) img_names, answers = list(img_name2answers.keys()), list( img_name2answers.values()) return img_names, answers
def startGUI(): cap = capture.Capture(0) print('before run') cap.start() t = time.time() while cap.img is None: print('waiting for camera to connect',time.time()-t) time.sleep(.25) proc = preprocess.Processor() proc.start(cap) while proc.img is None: print('waiting for processed image',time.time()-t) time.sleep(.25) ocr = OCR.OCR() ocr.start(proc) while ocr.txt is None: print('waiting for OCR',time.time()-t) time.sleep(.25) root.cap = cap root.proc = proc root.mainloop()
from io import RawIOBase import capture, preprocess, OCR import time, cv2 cap = capture.Capture(1) print('before run') cap.start() t = time.time() while cap.img is None: print('waiting for camera to connect',time.time()-t) time.sleep(.25) proc = preprocess.Processor() proc.start(cap) while proc.img is None: print('waiting for processed image',time.time()-t) time.sleep(.25) ocr = OCR.OCR() ocr.start(proc) while ocr.txt is None: print('waiting for OCR',time.time()-t) time.sleep(.25) t = time.time() while True: raw = cap.img