def Load(self, path: str = 'data', patterns: List[str] = ['jpg', 'jpeg', 'png'], name="images", input=None, in_memory=False): '''load data from folders, where each folder represents separate class''' global res if self.model.mode == 'train': return if self.model.mode == "design": if input is None: input = name folders = glob.glob(path + '/*/') #print(folders) image_names = [] image_names = reduce(lambda x, y: x + y, [[(glob.glob(x + '*.' + _type), x) for x in folders] for _type in patterns]) image_list: NodeList = NodeList(self.graph) for iname in image_names: images, image_class = iname image_class = image_class.split('/')[-2] image_list = image_list + [ Node( self.graph, { "image": mvImage(x, in_memory=in_memory), "target_class": image_class.lower() }) for x in images ] random.shuffle(image_list) self.model.metadic[name] = NodeList.from_list( image_list, self.graph) self.model.capactity = len(self.model.metadic) print("Loaded " + str(len(image_list)) + " images") res = image_list self.model.class_source = name self.model.last_call = name self.model.mtype = "Normal" self.model.start_point = name self.model.record('Image.Load', ['name', 'input'], [name, input]) if self.model.mode == "predict": if type(self.model.metadic[input]) is str: path = self.model.metadic[input] images = (glob.glob(path)) image_list = [ Node( self.graph, { "image": mvImage(x, in_memory=in_memory), "target_class": "unknown" }) for x in images ] self.model.metadic[name] = NodeList.from_list( image_list, self.graph) self.model.capactity = len(self.model.metadic) return self
def LoadPDF(path, in_memory=False): ''' this is special function for loading images from pdf file it actuall creates png images of pages on disk! requires WAND and ImageMagik''' try: from wand.image import Image #another local import to avoid proliferation of auxiliary dependencies except: print( "ERROR:Can not import Wand library, that is needed for reading PDF. Please make sure wand http://docs.wand-py.org/ is installed" ) return import neuthink.metagraph as m # can't import before, will get circular import diag = path graph = Graph() image_list: m.dNodeList = m.dNodeList(graph) #if you see not authorized error, run this from console: sudo sed -i '/PDF/s/none/read|write/' /etc/ImageMagick-6/policy.xml #also see here https://stackoverflow.com/questions/42928765/convertnot-authorized-aaaa-error-constitute-c-readimage-453 with (Image(filename=diag, resolution=300)) as source: images = source.sequence pages = len(images) for i in range(pages): Image(images[i]).save(filename=str(i) + '.png') image = mvImage(path=str(i) + '.png', in_memory=in_memory) imnode = Node(graph, {'image': image, 'type': 'image'}) image_list.append(imnode) return image_list
def UnVectorize(self, source=None, target=None): if self.model.mode == "train": return if source in self.model.metatensor: data = self.model.metatensor[source] if self.model.device is not None: data = data.cpu() data = data.detach() if len(data.shape) == 4: data = data.squeeze(1) data = data.numpy() minrange = min(self.model.batch_size, len(self.model) - self.model.index) for i in range(self.model.index, self.model.index + minrange): self.model[i][target] = mvImage( "", source=Image.fromarray( (data[i - self.model.index] * 255).astype('float32')), in_memory=True) self.res = [ x[target] for x in self.model[self.model.index:(self.model.index + minrange)] ] self.model.result = self.res if self.model.mode == 'design': self.model.record("Image.UnVectorize", ['source', 'target'], [source, target]) return self.model
def MakeSubimages(image: mvImage, stride: int, size: int) -> List[mvImage]: '''takes one big image and splits it into subimages of given size using specified stride''' newimages = [] # print(image.width) for i in range(0, int(image.width), stride): for j in range(0, int(image.height), stride): im2 = mvImage("", in_memory=True, source=image.content.crop( (i, j, i + size, j + size))) newimages.append(im2) return newimages
def make_list(image_names, image_list, patterns): for iname in image_names: # print(iname) images, image_class = iname if len(image_class) > 0: image_class = image_class.split('/')[-2] #image_name = image_class. image_list = image_list + [ Node( graph, { "image": mvImage(x, in_memory=in_memory), target_class: image_class.lower(), 'type': 'image' }) for x in images ] return image_list
def JoinBoundingBoxes(scene: NodeList, min_distance=None, comp_class=None): '''joins all components of type comp_class that are closer then mindistance or if they bounding boxes intersect ''' #TODO: Code really needs cleanup and simplification #TODO: Need to have an ability to specify vertical and horizontal distance separtely def center(component1): cx = (component1[0][0] + component1[1][0]) / 2 cy = (component1[0][1] + component1[1][1]) / 2 return (cx, cy) def distance(component1, component2): c1 = center(component1) c2 = center(component2) return math.sqrt((c1[0] - c2[0])**2 + (c1[1] - c2[1])**2) def boundary_distance(x, y): left_right = sorted([x, y], key=lambda x: x[0][0]) top_down = sorted([x, y], key=lambda x: x[1][1]) left_d = left_right[1][0][0] - left_right[0][1][0] top_d = top_down[1][1][1] - top_down[0][1][1] return (left_d, top_d) is_finished = False while not is_finished: is_finished = True newnodes = NodeList(scene.parent_graph) for x in scene: if not "_state_delete" in x: for y in scene: if x != y and not "_state_delete" in y: left_d, top_d = boundary_distance( x['location'], y['location']) print(left_d, top_d) if (left_d < min_distance and top_d < min_distance ) and x['target_class'] == comp_class and y[ 'target_class'] == comp_class and not "_state_delete" in x and not "_state_delete" in y: p1 = x.Parents({}) p2 = y.Parents({}) if len(p1) > 0 and len(p2) > 0: if p1[0] != p2[0]: continue left_right = sorted( [x, y], key=lambda x: x['location'][0][0]) top_down = sorted( [x, y], key=lambda x: x['location'][0][1]) print("---------------") print(x, y) print(left_right, top_down) newbox = (left_right[0]['location'][0][0], top_down[0]['location'][0][1]), ( (left_right[1]['location'][1][0], top_down[1]['location'][1][1])) print(newbox) print("---------------") new_image = mvImage( "", in_memory=True, source=x['base_image']['image'].content.crop( (newbox[0][0], newbox[0][1], newbox[1][0], newbox[1][1]))) new_image_node = Node( scene.parent_graph, { 'image': new_image, 'location': newbox, 'base_image': x['base_image'], 'target_class': comp_class, 'type': 'image' }) newnodes.append(new_image_node) x.Parents({}).First().Connect(new_image_node) x['_state_delete'] = 'yes' y['_state_delete'] = 'yes' is_finished = False scene = scene + newnodes #remove scene.Match({"_state_delete": 'yes'}).Delete() return