def SetData(self, issorted=False): ind1 = self.PageNum * FIGURES_PER_PAGE ind2 = min((self.PageNum + 1) * FIGURES_PER_PAGE, len(self.picPaths)) self.grid._table.data = [] for i in range(ind1, ind2): try: width, height = get_image_size.get_image_size(self.picPaths[i]) except get_image_size.UnknownImageFormat: width, height = -1, -1 print('Warning: Failed to load figure %sn' % self.picPaths[i]) self.totalImages -= 1 continue ratio = float(width) / float(height) new_height = self.COLWIDTH / ratio if new_height < self.MAX_ROWHEIGHT: width = self.COLWIDTH - 10 height = self.COLWIDTH / ratio else: width = self.MAX_ROWHEIGHT * ratio height = self.MAX_ROWHEIGHT d = (str(i + 1), { 'video': self.picPaths[i], 'dims': (width, height, ratio), 'text': self.vidDuration[i] }) self.grid._table.data.append(d)
def convert(args): with open( os.path.join(args.output_dir, args.data_split + ".txt.tmp"), "w", encoding="utf8", ) as fw, open( os.path.join(args.output_dir, args.data_split + "_box.txt.tmp"), "w", encoding="utf8", ) as fbw: for file in os.listdir(args.data_dir): file_path = os.path.join(args.data_dir, file) with open(file_path, "r", encoding="utf8") as f: data = json.load(f) image_path = file_path.replace("annotations", "images") image_path = image_path.replace("json", "png") #image = Image.open(image_path) #width, length = image.size width, length = get_image_size.get_image_size(image_path) for item in data["form"]: words, label = item["words"], item["label"] words = [w for w in words if w["text"].strip() != ""] if len(words) == 0: continue if label == "other": for w in words: fw.write(w["text"] + "\tO\n") fbw.write(w["text"] + "\t" + bbox_string(w["box"], width, length) + "\n") elif label == "other_group": for w in words: fw.write(w["text"] + "\tO\n") fbw.write(w["text"] + "\t" + bbox_string(w["box"], width, length) + "\n") else: if len(words) == 1: fw.write(words[0]["text"] + "\tS-" + label.upper() + "\n") fbw.write(words[0]["text"] + "\t" + bbox_string(words[0]["box"], width, length) + "\n") else: fw.write(words[0]["text"] + "\tB-" + label.upper() + "\n") fbw.write(words[0]["text"] + "\t" + bbox_string(words[0]["box"], width, length) + "\n") for w in words[1:-1]: fw.write(w["text"] + "\tI-" + label.upper() + "\n") fbw.write(w["text"] + "\t" + bbox_string(w["box"], width, length) + "\n") fw.write(words[-1]["text"] + "\tE-" + label.upper() + "\n") fbw.write( words[-1]["text"] + "\t" + bbox_string(words[-1]["box"], width, length) + "\n") fw.write("\n") fbw.write("\n")
def get_image_size(path): import get_image_size try: width, height = get_image_size.get_image_size(path) except get_image_size.UnknownImageFormat: width, height = -1, -1 return width, height
def filter_images(image_list, image_width, image_height): filtered_list = [] for image_path in image_list: try: width, height = get_image_size(image_path) except UnknownImageFormat: width, height = -1, -1 if width == image_width and height == image_height: filtered_list.append(image_path) return filtered_list
def _load_images(self, images): fieldmodule = self._region.getFieldmodule() for image in images: width, height = get_image_size.get_image_size(image) if width != -1 or height != -1: cache = fieldmodule.createFieldcache() self._modelScaleField.assignReal( cache, [width / 1000.0, height / 1000.0, 1.0]) image_field = createImageField(fieldmodule, image) material = createMaterialUsingImageField(self._region, image_field) surface = self._scene.findGraphicsByName('plane-surfaces') surface.setMaterial(material) break
def _load_images(self, images): fieldmodule = self._region.getFieldmodule() self._frame_count = len(images) if self._frame_count > 0: # Assume all images have the same dimensions. width, height = get_image_size.get_image_size(images[0]) if width != -1 or height != -1: cache = fieldmodule.createFieldcache() self._modelScaleField.assignReal( cache, [width / 1000.0, height / 1000.0, 1.0]) image_field = createVolumeImageField(fieldmodule, images) material = createMaterialUsingImageField(self._region, image_field) surface = self._scene.findGraphicsByName('plane-surfaces') surface.setMaterial(material)
def sort_by_width_height(dir_path, sort_path, verbFlag, folder, width, height, flagSort): # Initialise image width and height width = -1 height = -1 # Search the dir_path for images for root, dirs, files in os.walk(dir_path): # Skip the folder where sorted images will be moved to if root == sort_path: if verbFlag: print("Skipping folder " + folder) continue if verbFlag: print("Searching " + root) for file in files: valid = False # Get current file's path file_path = os.path.join(root, file) # Get current file's width and height, if it's an image try: width, height = get_image_size.get_image_size(file_path) except get_image_size.UnknownImageFormat: if verbFlag: print("Can't get size of " + file + " Propably not an image.") else: # Check the image's size based on the sort mode used if flagSort == "height": if height < min_height: valid = True elif flagSort == "width": if width < min_width: valid = True elif flagSort == "either": if (width < min_width) or (height < min_height): valid = True elif flagSort == "both": if (width < min_width) and (height < min_height): valid = True if valid: # Move the image to the predefined folder if verbFlag: print("Moving " + file) shutil.move(file_path, os.path.join(sort_path, file))
def dataset_filepath(root, get_masks=True): ''' input: a full path to root of dataset output: a list of filepath of images and their masks ''' root += '/*' dir_list = [] path_list = glob.glob(root) for subdir in tqdm(path_list, total=len(path_list)): image = {'image': str(glob.glob(subdir+'/images/*.png')[0])} width, height = get_image_size(image['image']) image['width'] = width image['height']= height if get_masks: image['masks'] = [] for mask_path in glob.glob(subdir+'/masks/*.png'): image['masks'].append(mask_path) # get bounding box dir_list.append(image) return dir_list
def get_imgs(folder, bad_imgs): imgs = {} count = 0 corrupted = [] files = [ f.name for f in os.scandir(folder) if (f.is_file() and f.name not in bad_imgs) ] for i, f in enumerate(files): if i % 1000 == 0: print('progress', i / float(len(files)) * 100.0, '% ', i, '/', len(files), ' ', ' no. of corrupted:', count) path = os.path.join(folder, f) try: width, height = get_image_size.get_image_size(path) imgs[f.split('.')[-2]] = (width, height, f) except Exception as e: print('problem with', path) print(e) count += 1 corrupted.append(f) # im = cv2.imread(path) # try: # width, height, _ = im.shape # imgs[f.split('.')[-2]] = (width, height) # except: # print('problem with', path) # count += 1 print(len(imgs.keys())) print('problems with', count, 'imgs') print(corrupted) # print(type(imgs.pop())) return imgs
def sort_by_aspect_ratio(dir_path, sort_path, verbFlag, folder): # Search the dir_path for images for root, dirs, files in os.walk(dir_path): # Skip the folder where sorted images will be moved to if root == sort_path: if verbFlag: print("Skipping folder " + folder) continue if verbFlag: print("Searching " + root) for file in files: valid = False # Get current file's path file_path = os.path.join(root, file) # Get current file's width and height, if it's an image try: width, height = get_image_size.get_image_size(file_path) except get_image_size.UnknownImageFormat: if verbFlag: print("Can't get size of " + file + " Propably not an image.") else: aspect_ratio = calc_apsect_ratio(width, height) aspect_ratio_folder = os.path.join(sort_path, aspect_ratio) if verbFlag: print("Moving " + file + "to folder \"" + aspect_ratio + "\"") try: os.mkdir(aspect_ratio_folder) except OSError: # Raises error if folder already exists pass shutil.move(file_path, os.path.join(aspect_ratio_folder, file))
def read_data_kitti_ssd(params, subset): # subset = 'train' / 'test / 'both' # min_conf = 0.1 min_conf = -1 print('Loading objects and detections') # load splits with open(params['train_split_fname']) as f: content = f.readlines() train_imgs = [int(x.strip()) - 1 for x in content] # remove '\n' and convert to int with open(params['test_split_fname']) as f: content = f.readlines() test_imgs = [int(x.strip()) - 1 for x in content] # remove '\n' and convert to int # choose working split if subset == 'train': img_nums = train_imgs elif subset == 'test': img_nums = test_imgs elif subset == 'both': img_nums = train_imgs + test_imgs else: raise RuntimeError('Wrong subset') # read test detections, objects, etc imgs = {} objects = [] # list of all objects objects_pi = defaultdict(list) # objects arrange per image obj_id = 0 dets = [] dets_pi = defaultdict(list) det_id = 0 for img_num in img_nums: # get image size img_name_base = str(img_num).rjust(6, '0') img_name = img_name_base + '.png' fname = params['imgs_folder'] + img_name width, height = get_image_size.get_image_size(fname) # index image imgs[img_num] = {'id': img_num, 'width': width, 'height': height, 'file_name': img_name} # read objects fname = params['objects_folder'] + img_name_base + '.txt' with open(fname) as f: content = f.readlines() content = [x.strip() for x in content] # remove '\n' for line in content: l = line.split(' ') # Handle labels and dont_cares label = l[0].lower() if label in params['object_labels']: dont_care = False elif label == 'dontcare' or label == 'van': dont_care = True label = 'dontcare' else: continue x1 = int(round(float(l[4]))) y1 = int(round(float(l[5]))) x2 = int(round(float(l[6]))) y2 = int(round(float(l[7]))) obj_new = {'label': label, 'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2, 'dont_care': dont_care, 'id': obj_id, 'img_id': img_num} objects.append(obj_new) objects_pi[img_num].append(obj_new) obj_id += 1 # read detections fname = params['dets_folder'] + str(img_num).rjust(6, '0') + '.txt' with open(fname) as f: content = f.readlines() content = [x.strip() for x in content] # remove '\n' for line in content: l = line.split(' ') conf = float(l[15]) bbox = np.array([int(l[4]), int(l[5]), int(l[6]), int(l[7])]) bbox[bbox < 0] = 0 if bbox[2] >= width: bbox[2] = width - 1 if bbox[3] >= height: bbox[3] = height - 1 if conf > min_conf: det_new = {'label': l[0], 'x1': bbox[0], 'y1': bbox[1], 'x2': bbox[2], 'y2': bbox[3], 'conf': conf, 'id': det_id, 'img_id': img_num} dets.append(det_new) dets_pi[img_num].append(det_new) det_id += 1 data = {'imgs': imgs, 'objects': objects, 'objects_pi': objects_pi, 'dets': dets, 'dets_pi': dets_pi} return data
num_workers=2) kuzu_keep = ImageFolder(kuzu, transform=torchvision.transforms.Compose(transforms_keep)) img_batch_keep = torch.utils.data.DataLoader(kuzu_keep, batch_size=batch_size, shuffle=True, num_workers=2) img_sizes = {} all_image_lst = [] #This loop is for getting file sizes and a list of all image names for _,(image,label,file_loc) in enumerate(img_batch_keep): #img_sizes[file_loc] = image.size() #print('file loc', file_loc) size2 = get_image_size(file_loc[0]) new_size = torch.Size([1,3,size2[1],size2[0]]) print(file_loc,new_size) img_sizes[file_loc] = new_size all_image_lst.append(file_loc[0]) print("all image list", all_image_lst) #This randomly takes 50 images for the "test set". test_images = random.sample(all_image_lst, 50) del kuzu_keep del img_batch_keep # initiate Generator
def kitti2AU(kittyAnnotationList, imsize=None, filename=None, cropbboxtoimage=False): # AU format: # a JSON-string of the following format # { # 'label':<classlabel>, # a label without spaces # 'imagepath':<imagepath>, # relative path to image # 'imagewidth':<image width>, # width of image # 'imageheight':<image height>, # height of image # 'xmin': <xmin>, # minimum column coordinate staring from upper left corner # 'ymin': <ymin>, # maximum column coordinate staring from upper left corner # 'xmax': <xmax>, # minimum row coordinate staring from upper left corner # 'ymax': <ymax>, # maximum row coordinate staring from upper left corner # 'orientation':<rotation [0:1]>, # 'occlusion':<occlusion> # } ############# Arguments: # kittyAnnotationList list of rows from kitti annotation # imsize: size of image AUannotation= { 'label':'', # a label without spaces 'imagepath':'', # relative path to image 'imagewidth':0, # width of image 'imageheight':0, # height of image 'xmin': 0, # minimum column coordinate staring from upper left corner 'ymin': 0, # maximum column coordinate staring from upper left corner 'xmax': 0, # minimum row coordinate staring from upper left corner 'ymax': 0, # maximum row coordinate staring from upper left corner 'orientation':0.0, 'occlusion':0 } AUannotationlist = [] for KittiAnnotation in kittyAnnotationList: #splitannotation = KittiAnnotation.split() #Split the string at spaces, but respect quotes in specie's names if type(KittiAnnotation) is str: KittiAnnotation = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', KittiAnnotation) if not imsize: if not filename: #defaults to [2048,2448,3] imsize=[2048,2448,3] #height, witdh, channels else: width, height = get_image_size.get_image_size(filename) imsize = [height, width, 3] objectclass=KittiAnnotation[0] xmin=KittiAnnotation[4] ymin=KittiAnnotation[5] xmax=KittiAnnotation[6] ymax=KittiAnnotation[7] if cropbboxtoimage: xmin = float(max(0,int(xmin))) ymin = float(max(0,int(ymin))) xmax = float(min(imsize[1]-1,int(xmax))) ymax = float(min(imsize[0]-1,int(ymax))) assert(xmin>=0) assert(ymin>=0) assert(xmax<=imsize[1]) assert(ymax<=imsize[0]) anno_tmp = AUannotation.copy() anno_tmp['label']=objectclass anno_tmp['xmin']=xmin anno_tmp['xmax']=xmax anno_tmp['ymin']=ymin anno_tmp['ymax']=ymax anno_tmp['imagepath']=filename AUannotationlist.append(anno_tmp) return AUannotationlist
def imageFetchDimension(rawImage): try: width, height = get_image_size.get_image_size(rawImage) except get_image_size.UnknownImageFormat: width, height = -1, -1 return width, height
def __init__(self, directory, transforms=None, max_dataset_size=float('inf'), ignore_gt=False, seed=0): """XMLDataset. The sample images of this dataset must be all inside one directory. Inside the same directory, there must be one XML file as described by https://communityhub.purdue.edu/groups/phenosorg/wiki/APIspecs (minimum XML API version is v.0.4.0). If there is no XML file, metrics will not be computed, and only estimations will be provided. :param directory: Directory with all the images and the XML file. :param transform: Transform to be applied to each image. :param max_dataset_size: Only use the first N images in the directory. :param ignore_gt: Ignore the GT in the XML file, i.e, provide samples without plant locations or counts. :param seed: Random seed. """ self.root_dir = directory self.transforms = transforms # Get list of files in the dataset directory, # and the filename of the XML listfiles = os.listdir(directory) xml_filenames = [f for f in listfiles if f.endswith('.xml')] if len(xml_filenames) == 1: xml_filename = xml_filenames[0] elif len(xml_filenames) == 0: xml_filename = None else: print(f"E: there is more than one XML file in '{directory}'") exit(-1) # Ignore files that are not images listfiles = [ f for f in listfiles if any(f.lower().endswith(ext) for ext in IMG_EXTENSIONS) ] # Shuffle list of files np.random.seed(seed) random.shuffle(listfiles) if len(listfiles) == 0: raise ValueError(f"There are no images in '{directory}'") if xml_filename is None: print('W: The dataset directory %s does not contain ' 'a XML file with groundtruth. Metrics will not be evaluated.' 'Only estimations will be returned.' % directory) self.there_is_gt = (xml_filename is not None) and (not ignore_gt) # Read all XML as a string with open(os.path.join(directory, xml_filename), 'r') as fd: xml_str = fd.read() # Convert to dictionary # (some elements we expect to have multiple repetitions, # so put them in a list) xml_dict = xmltodict.parse( xml_str, force_list=['field', 'panel', 'plot', 'plant']) # Check API version number try: api_version = xml_dict['fields']['@apiversion'] except: # An unknown version number means it's the very first one # when we did not have api version numbers api_version = '0.1.0' major_version, minor_version, _ = parse('{}.{}.{}', api_version) major_version = int(major_version) minor_version = int(minor_version) if not (major_version == 0 and minor_version == 4): raise ValueError('An XML with API v0.4 is required.') # Create the dictionary with the entire dataset dictt = {} for field in xml_dict['fields']['field']: for panel in field['panels']['panel']: for plot in panel['plots']['plot']: if self.there_is_gt and \ not('plant_count' in plot and \ 'plants' in plot): # There is GT for some plots but not this one continue filename = plot['orthophoto_chop_filename'] if 'plot_number' in plot: plot_number = plot['plot_number'] else: plot_number = 'unknown' if 'subrow_grid_location' in plot: subrow_grid_x = \ int(plot['subrow_grid_location']['x']['#text']) subrow_grid_y = \ int(plot['subrow_grid_location']['y']['#text']) else: subrow_grid_x = 'unknown' subrow_grid_y = 'unknown' if 'row_number' in plot: row_number = plot['row_number'] else: row_number = 'unknown' if 'range_number' in plot: range_number = plot['range_number'] else: range_number = 'unknown' img_abspath = os.path.join(self.root_dir, filename) orig_width, orig_height = \ get_image_size.get_image_size(img_abspath) with torch.no_grad(): orig_height = torch.tensor( orig_height, dtype=torch.get_default_dtype()) orig_width = torch.tensor( orig_width, dtype=torch.get_default_dtype()) dictt[filename] = { 'filename': filename, 'plot_number': plot_number, 'subrow_grid_location_x': subrow_grid_x, 'subrow_grid_location_y': subrow_grid_y, 'row_number': row_number, 'range_number': range_number, 'orig_width': orig_width, 'orig_height': orig_height } if self.there_is_gt: count = int(plot['plant_count']) locations = [] for plant in plot['plants']['plant']: for y in plant['location']['y']: if y['@units'] == 'pixels' and \ y['@wrt'] == 'plot': y = float(y['#text']) break for x in plant['location']['x']: if x['@units'] == 'pixels' and \ x['@wrt'] == 'plot': x = float(x['#text']) break locations.append([y, x]) dictt[filename]['count'] = count dictt[filename]['locations'] = locations # Use an Ordered Dictionary to allow random access dictt = OrderedDict(dictt.items()) self.dict_list = list(dictt.items()) # Make dataset smaller new_dataset_length = min(len(dictt), max_dataset_size) dictt = { key: elem_dict for key, elem_dict in self.dict_list[:new_dataset_length] } self.dict_list = list(dictt.items())
def imageFetchDimension(rawImage): try: width, height = get_image_size.get_image_size(rawImage) except get_image_size.UnknownImageFormat: width, height = -1, -1 return width, height
def kitti2voc(kittyAnnotationList, imsize=None, filename=None): annotation = ET.Element("annotation") ET.SubElement(annotation, "folder").text = "VOC2007" for KittiAnnotation in kittyAnnotationList: #splitannotation = KittiAnnotation.split() #Split the string at spaces, but respect quotes in specie's names splitannotation = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', KittiAnnotation) imageobject = ET.SubElement(annotation, "object") ET.SubElement(imageobject, "name").text = splitannotation[0].replace('"','') ET.SubElement(imageobject, "pose").text = 'frontal' ET.SubElement(imageobject, "truncated").text = 0 ET.SubElement(imageobject, "difficult").text = 0 if not imsize: if not filename: #defaults to [2048,2448,3] imsize=[2048,2448,3] #height, witdh, channels else: #height, width = get_image_size(filename) width, height = get_image_size.get_image_size(filename) imsize = [height, width, 3] sz = ET.SubElement(annotation, "size") ET.SubElement(sz, "width").text = str(imsize[1]) ET.SubElement(sz, "height").text = str(imsize[0]) ET.SubElement(sz, "depth").text = str(imsize[2]) bndbox = ET.SubElement(imageobject, "bndbox") xmin=splitannotation[4] ymin=splitannotation[5] xmax=splitannotation[6] ymax=splitannotation[7] if CROPBBOXTOIMAGE: xmin = str(max(0,int(xmin))) ymin = str(max(0,int(ymin))) xmax = str(min(imsize[1]-1,int(xmax))) ymax = str(min(imsize[0]-1,int(ymax))) assert(float(xmin)>=0) assert(float(ymin)>=0) assert(float(xmax)<=imsize[1]) assert(float(ymax)<=imsize[0]) ET.SubElement(bndbox, "xmin").text = xmin ET.SubElement(bndbox, "ymin").text = ymin ET.SubElement(bndbox, "xmax").text = xmax ET.SubElement(bndbox, "ymax").text = ymax tree = ET.ElementTree(annotation) #xmlstr = ET.ElementTree.tostring(annotation, encoding='utf8', method='xml') return tree
def convert(data_path, max_slope=None, polygon=False): gt_path = os.path.join(data_path, 'gt.mat') classes = ['Background', 'Text'] print('loding ground truth data...') data = scipy.io.loadmat(gt_path) num_samples = data['imnames'].shape[1] print('processing samples...') image_names = [] bboxes = [] texts = [] for image_name, text, boxes in zip(data["imnames"][0], data["txt"][0], data["wordBB"][0]): image_name = image_name[0] text = [s.strip().split() for s in text] text = [w for s in text for w in s] # read image size only when size changes if image_name.split('_')[-1].split('.')[0] == '0': img_width, img_height = get_image_size( os.path.join(data_path, image_name)) # data['wordBB'][0,i] has shape (x + y, points, words) = (2, 4, n) if len(boxes.shape) == 2: boxes = boxes[:, :, None] boxes = boxes.transpose(2, 1, 0) boxes[:, :, 0] /= img_width boxes[:, :, 1] /= img_height boxes = boxes.reshape(boxes.shape[0], -1) # fix some bugs in the SynthText dataset eps = 1e-3 p1, p2, p3, p4 = boxes[:, 0:2], boxes[:, 2:4], boxes[:, 4:6], boxes[:, 6:8] # fix twisted boxes (897 boxes, 0.012344 %) if True: mask = np.linalg.norm(p1 + p2 - p3 - p4, axis=1) < eps boxes[mask] = np.concatenate( [p1[mask], p3[mask], p2[mask], p4[mask]], axis=1) # filter out bad boxes (528 boxes, 0.007266 %) if True: mask = np.ones(len(boxes), dtype=np.bool) # filter boxes with zero width (173 boxes, 0.002381 %) boxes_w = np.linalg.norm(p1 - p2, axis=1) boxes_h = np.linalg.norm(p2 - p3, axis=1) mask = np.logical_and(mask, boxes_w > eps) mask = np.logical_and(mask, boxes_h > eps) # filter boxes that are too large (62 boxes, 0.000853 %) mask = np.logical_and(mask, np.all(boxes > -1, axis=1)) mask = np.logical_and(mask, np.all(boxes < 2, axis=1)) # filter boxes with all vertices outside the image (232 boxes, 0.003196 %) boxes_x = boxes[:, 0::2] boxes_y = boxes[:, 1::2] mask = np.logical_and( mask, np.sum(np.logical_or(np.logical_or(boxes_x < 0, boxes_x > 1), np.logical_or(boxes_y < 0, boxes_y > 1)), axis=1) < 4) # filter boxes with center outside the image (336 boxes, 0.004624 %) boxes_x_mean = np.mean(boxes[:, 0::2], axis=1) boxes_y_mean = np.mean(boxes[:, 1::2], axis=1) mask = np.logical_and( mask, np.logical_and(boxes_x_mean > 0, boxes_x_mean < 1)) mask = np.logical_and( mask, np.logical_and(boxes_y_mean > 0, boxes_y_mean < 1)) boxes = boxes[mask] text = np.asarray(text)[mask] # only boxes with slope below max_slope if not max_slope == None: angles = np.arctan( np.divide(boxes[:, 2] - boxes[:, 0], boxes[:, 3] - boxes[:, 1])) angles[angles < 0] += np.pi angles = angles / np.pi * 180 - 90 boxes = boxes[np.abs(angles) < max_slope] # only images with boxes if len(boxes) == 0: continue if not polygon: xmax = np.max(boxes[:, 0::2], axis=1) xmin = np.min(boxes[:, 0::2], axis=1) ymax = np.max(boxes[:, 1::2], axis=1) ymin = np.min(boxes[:, 1::2], axis=1) boxes = np.array([xmin, ymin, xmax, ymax]).T # append classes boxes = np.concatenate([boxes, np.ones([boxes.shape[0], 1])], axis=1) image_names.append(image_name) bboxes.append(boxes) texts.append(text) return image_names, bboxes, texts
files = [f for f in listdir(SpotlightPath) if isfile(join(SpotlightPath, f))] if not os.path.exists('Spotlight-Images'): os.makedirs('Spotlight-Images') for file_name in files: full_file_name = os.path.join(SpotlightPath, file_name) if os.path.isfile(full_file_name): # print(os.path.join(dest_directory, file_name + '.' + filename_suffix)) if not os.path.isfile( os.path.join(dest_directory, file_name + '.' + filename_suffix)): new_file = shutil.copy(full_file_name, dest_directory) os.rename(new_file, new_file + '.' + filename_suffix) print('Image Found -> ' + os.path.join(file_name + '.' + filename_suffix)) print('All Images Can Be Found In -> ' + dest_directory) images = [ f for f in listdir(dest_directory) if isfile(join(dest_directory, f)) ] for image in images: full_image_name = os.path.join(dest_directory, image) try: width, height = get_image_size.get_image_size(full_image_name) if not width >= 1080: os.remove(full_image_name) except get_image_size.UnknownImageFormat: width, height = -1, -1
<EndPopup>: auto_dismiss: False title: '' size_hint: (0.4,0.3) BoxLayout: orientation: 'vertical' Label: id: message Button: id: start text: 'Start a new game!' on_release: root.start_button() ''') GRID_IMAGE_PATH = 'grid.png' image_size = get_image_size(GRID_IMAGE_PATH) IMAGE_RATIO = image_size[0] / image_size[1] class MyGrid(Widget): def draw_tab(self, tab): """ Draw a static image of the connect4, with coins placed according to tab. tab should be a numpy array with values 0,1,2 """ # RGB colors COLORS = { # 'red': (0.83,0,0), 'red': (0.66, 0.07, 0.07), 'yellow': (1, 0.8, 0), 'blue': (0, 0, 0.6),
def __init__(self, image_file): self.file_name = image_file self.background = load_image(self.file_name) self.size = width, height = image.get_image_size(graphics_path + self.file_name) self.screen = game.display.set_mode(self.size)
df_results = pd.read_csv(args.results) df_gt = pd.read_csv(args.gt) df_metrics = pd.DataFrame(columns=[ 'r', 'precision', 'recall', 'fscore', 'MAHD', 'MAPE', 'ME', 'MPE', 'MAE', 'MSE', 'RMSE', 'r', 'R2' ]) for j, judge in enumerate(tqdm(judges)): for idx, row_result in df_results.iterrows(): filename = row_result['filename'] row_gt = df_gt[df_gt['filename'] == filename].iloc()[0] w, h = get_image_size.get_image_size( os.path.join(args.dataset, filename)) diagonal = math.sqrt(w**2 + h**2) judge.feed_count(row_result['count'], row_gt['count']) judge.feed_points(ast.literal_eval(row_result['locations']), ast.literal_eval(row_gt['locations']), max_ahd=diagonal) df = pd.DataFrame(data=[[judge.r, judge.precision, judge.recall, judge.fscore, judge.mahd, judge.mape, judge.me, judge.mpe,
def genImg(net, res): roadnet_gen = getattr(pre_defined_road_nets, net) g = roadnet_gen(edglen=2) # convert to pygraphviz for better graphviz support g_pg = nx.nx_agraph.to_agraph(g) __loc_garbage = nx.nx_agraph.graphviz_layout(g, prog='neato', args='') # If I take this away then it segfaults when we use g_pg.graph_attr below... I don't want to use this because it doesn't take into account all of the scaling, and resolutions stuff. # add graph property for desired dpi g_pg.graph_attr['dpi'] = res # `dots per inch` default for a .png is 96 g_pg.graph_attr['size'] = 10.0 # in inches, given that there is only one value, both should be equal to this. g_pg.graph_attr['ratio'] = 'fill' # this means that the dimensions will need to be scaled in some way in order to match the size fmt = 'png' # fmt = 'svg' make_pbm = True fname = net if fmt is 'svg': # output graph file g_pg.draw(fname+'.'+fmt, format=fmt, prog='neato') elif fmt is 'png': # output graph file g_pg.layout(prog='neato',args='') g_pg.draw(fname+'.'+fmt, format=fmt) # get positions for use in Gazebo world creation pos = {} for i in g_pg.nodes_iter(): n = g_pg.get_node(i) pos[n.get_name()] = [ float(i) for i in n.attr['pos'].split(',')] # graphviz bounding box layout bb = g_pg.graph_attr['bb'] bb_num = [float(i) for i in bb.split(',')] # find the scaling between layout and png image_size = get_image_size(fname+"."+fmt) bb_x = bb_num[2]-bb_num[0] bb_y = bb_num[3]-bb_num[1] scale = np.divide(image_size, [bb_x, bb_y]) pixel_pos = {} for key in pos: pixel_pos[key] = [pos[key][0]*scale[0], pos[key][1]*scale[1]] node_atts = {} for key in g.nodes(): node_atts[key] = g.node[key]['feature'] with open(fname+".json","w") as outfile: json.dump({'pixel_positions':pixel_pos,'original_positions':pos,'feature':node_atts}, outfile, indent=4) if make_pbm and fmt is 'png': # load image to convert to .pbm g_img = misc.imread(fname+'.'+fmt) # sum layers, divide by 4 becasue we summed 4 =ers g_img_flat = np.sum(g_img, axis=2)/3 blk = g_img_flat <= 254 wht = g_img_flat > 254 g_img_flat[blk] = 0 g_img_flat[wht] = 1 writePBM(g_img_flat, fname)
def get_image_size_(image_path): try: width, height = get_image_size.get_image_size(image_path) return width, height except Exception: width, height = -1, -1
def image_to_latex(textbody, media_archive, figcounter, fws=['0.5', '0.4'], layout='optimal', wp_blocks=False): """Replaces image references in textbody with LaTeX figure/subfigure code. Original image string looks like this: <img class="alignnone size-full wp-image-24" src="http://assets.mysite.com/img/dsc04657-edited.jpg" alt="DSC04657 - Edited.jpg" width="4912" height="3264" /> Parameters ----------- textbody : str Text from parsed XML. media_archive : str Path to exported blog image files. Download not supported. figcounter : int Counter for figures to keep track of references. figwidths : list, default=['0.5','0.4'] Provides in x*\textwidth the width of landscape, then portrait. layout : str String defining how the figures should be fitted into the text. Options for layout: - 'single': images are included individually, size determined by figwidth. The benefit is this should work without issues, but the figures take up a lot of space. - 'paired': images are paired into subplots if they match in orientation, but not otherwise - 'optimal': occasionally buggy approach that does the same as 'paired', then afterwards matches up the images that haven't been paired. Returns -------- textbody, figcounter : str, int Textbody with LaTeX figures replaced, current figcounter to continue to next post. """ img_paths = [] # r=root, d=directories, f= files for r, d, f in os.walk(media_archive): for file in f: img_paths.append(os.path.join(r, file)) landscape = [-1] replacestr = {} allimgstr, fignums, figpaths, figcaptions, orient = [], [], [], [], [] str_fig, figpath, figcaption = '', '', '' lastimgstr = '----' figsetup = [] if wp_blocks == False: re_img_str = '<img(.+?)>' else: re_img_str = '<!-- wp:image (.+?)-->\n(.+?)\n<!-- /wp:image -->' for s in re.finditer(re_img_str, textbody): laststr, lastfigpath, lastfigcaption = str_fig, figpath, figcaption img_exists = False try: n = re.search('src="(.+?)"', s.group(0)).group(1) nfile = os.path.split(n)[-1].split('?')[0] figpath = [x for x in img_paths if nfile in x][0] img_exists = True except: print("ERROR finding image regex: {}".format(s.group(0))) replacestr[s.group(0)] = "" if img_exists: allimgstr.append(s.group(0)) fignums.append(figcounter) figpaths.append(figpath) try: figwidth, figheight = get_image_size.get_image_size(figpath) except: figwidth, figheight = jpeg_res(figpath) try: figcaption = re.search('<figcaption>(.+?)</figcaption>', s.group(0)).group(1) except: # no captions figcaption = '' figcaptions.append(figcaption) # SINGLE FIGURE LAYOUT # -------------------- if layout == 'single': if figwidth > figheight: sfigtw = fws[0] else: sfigtw = fws[1] str_fig = _include_figure(figpath, figcounter, figcaption, figwidth=sfigtw) figsetup.append(1) # OPTIMAL/PAIRED FIGURE LAYOUT # ---------------------------- if layout in ['optimal', 'paired']: if figwidth > figheight: landscape.append(True) orient.append(True) else: landscape.append(False) orient.append(False) if landscape[-2] == landscape[-1]: if orient[-1] == True: figtw = '0.45' else: figtw = fws[1] str_fig = _include_subfigures(lastfigpath, figpath, figcounter - 1, figcounter, lastfigcaption, figcaption, figtw, figtw) landscape[-1] = -1 if nl + nl + lastimgstr in textbody: replacestr[nl + nl + lastimgstr] = " [\\ref{fig:" + str( figcounter - 1) + "}]" else: replacestr[lastimgstr] = " [\\ref{fig:" + str( figcounter - 1) + "}]" figsetup[-1] = 0 figsetup.append(2) else: if landscape[-1] == True: sfigtw = fws[0] else: sfigtw = fws[1] str_fig = _include_figure(figpath, figcounter, figcaption, figwidth=sfigtw) figsetup.append(1) # Defining the fig strings to replace img strings with: lastimgstr = s.group(0) if nl + nl + lastimgstr in textbody: replacestr[nl + nl + lastimgstr] = " [\\ref{fig:" + str( figcounter) + "}]" + nl + nl + str_fig else: replacestr[lastimgstr] = " [\\ref{fig:" + str( figcounter) + "}]" + nl + nl + str_fig #print('-------------'+str(figcounter)+'-'+str(figsetup[-1])) #print(str_fig) figcounter = figcounter + 1 # This is the last fix to pair up leftover images in optimal: if layout == 'optimal': if len(replacestr) != 0: loopfigs = figsetup + [-1] for ifig, fignum in enumerate(loopfigs): if loopfigs[ifig] == 1 and loopfigs[ifig + 1] == 1: if orient[ifig] == True: figtw1, figtw2 = fws[0], fws[1] else: figtw1, figtw2 = fws[1], fws[0] #print('********', fignum, figpaths[ifig], figpaths[ifig+1], fignums[ifig], ifig) str_fig = _include_subfigures( figpaths[ifig], figpaths[ifig + 1], fignums[ifig], fignums[ifig + 1], figcaptions[ifig], figcaptions[ifig + 1], figtw1, figtw2) if nl + nl + lastimgstr in textbody: replacestr[nl + nl + allimgstr[ifig]] = " [\\ref{fig:" + str( fignums[ifig]) + "}]" else: replacestr[allimgstr[ifig]] = " [\\ref{fig:" + str( fignums[ifig]) + "}]" replacestr[nl + nl + allimgstr[ifig + 1]] = " [\\ref{fig:" + str( fignums[ifig + 1]) + "}]" + nl + nl + str_fig loopfigs[ifig] = 0 loopfigs[ifig + 1] = 2 elif loopfigs[ifig + 1] == -1: break for key in replacestr: textbody = textbody.replace(key, replacestr[key]) if len(replacestr) != 0: # Correct labels that have ended up beneath figures rather in in textbody: for s in re.finditer('end{figure} \[\\\\ref{fig:(.+?)}]', textbody): num = s.group(1) textbody = textbody.replace( '[\\ref{fig:' + str(int(num) - 1) + '}]', '[\\ref{fig:' + str(int(num) - 1) + '}] ' + '[\\ref{fig:' + str(int(num)) + '}] ') textbody = textbody.replace('[\\ref{fig:' + str(int(num)) + '}]', '') for s in re.finditer('end{figure}\\n \[\\\\ref{fig:(.+?)}]', textbody): num = s.group(1) textbody = textbody.replace( '[\\ref{fig:' + str(int(num) - 1) + '}]', '[\\ref{fig:' + str(int(num) - 1) + '}] ' + '[\\ref{fig:' + str(int(num)) + '}] ') textbody = textbody.replace('[\\ref{fig:' + str(int(num)) + '}]', '') return textbody, figcounter