def detect(): upload = request.files.get("upload") right2left = request.forms.get("right2left") name, ext = os.path.splitext(upload.filename) print(ext.lower()) if ext.lower() not in (".png", ".jpg", ".jpeg"): return "File extension not allowed." timestamp = str(int(time.time() * 1000)) savedName = timestamp + ext save_path = "./uploaded/" if not os.path.exists(save_path): os.makedirs(save_path) file_path = "{path}/{file}".format(path=save_path, file=savedName) if os.path.exists(file_path) == True: os.remove(file_path) upload.save(file_path) if right2left == "true": kumiko = Kumiko({"rtl": "true"}) Panel.set_numbering("rtl") else: kumiko = Kumiko() Panel.set_numbering("ltr") infos = [] infos = kumiko.parse_images([file_path]) os.remove(file_path) return {"infos": infos}
def parse_image_with_bgcol(self, infos, filename, bgcol, url=None): contours = self.get_contours(self.gray, filename, bgcol) infos["background"] = bgcol self.dbg.infos = infos.copy() # Get (square) panels out of contours self.dbg.contourSize = int(sum(infos["size"]) / 2 * 0.004) panels = [] for contour in contours: arclength = cv.arcLength(contour, True) epsilon = 0.001 * arclength approx = cv.approxPolyDP(contour, epsilon, True) self.dbg.draw_contours(self.img, [approx], Debug.colours["red"]) panels.append(Panel(polygon=approx)) self.dbg.add_image(self.img, "Initial contours") self.dbg.add_step("Panels from initial contours", panels) # Group small panels that are close together, into bigger ones panels = self.group_small_panels(panels, filename) # See if panels can be cut into several (two non-consecutive points are close) self.split_panels(panels) # Merge panels that shouldn't have been split (speech bubble diving in a panel) self.merge_panels(panels) # splitting polygons may result in panels slightly overlapping, de-overlap them self.deoverlap_panels(panels) # re-filter out small panels panels = list(filter(lambda p: not p.is_small(), panels)) self.dbg.add_step("Exclude small panels", panels) # get actual gutters before expanding panels actual_gutters = Kumiko.actual_gutters(panels) infos["gutters"] = [actual_gutters["x"], actual_gutters["y"]] panels.sort() # TODO: remove when panels expansion is smarter self.expand_panels(panels) if len(panels) == 0: panels.append(Panel([0, 0, infos["size"][0], infos["size"][1]])) # Number panels comics-wise (ltr/rtl) panels.sort() # Simplify panels back to lists (x,y,w,h) panels = list(map(lambda p: p.to_xywh(), panels)) infos["panels"] = panels return infos
def merge_panels(self, panels): panels_to_remove = [] for i in range(len(panels)): for j in range(i + 1, len(panels)): if panels[i].contains(panels[j]): panels_to_remove.append(j) panels[i] = Panel.merge(panels[i], panels[j]) elif panels[j].contains(panels[i]): panels_to_remove.append(i) panels[j] = Panel.merge(panels[i], panels[j]) for i in reversed(sorted(list(set(panels_to_remove)))): del panels[i] self.dbg.add_step("Merge panels", panels)
def __init__(self, options={}): self.dbg = Debug('debug' in options and options['debug']) for o in ['progress', 'rtl']: self.options[o] = o in options and options[o] if self.options['rtl']: Panel.set_numbering('rtl') self.options[ 'min_panel_size_ratio'] = Panel.DEFAULT_MIN_PANEL_SIZE_RATIO if 'min_panel_size_ratio' in options and options[ 'min_panel_size_ratio']: self.options['min_panel_size_ratio'] = options[ 'min_panel_size_ratio']
def get_files_diff(file_or_dir,json1,json2): files_diff = {} for p in range(len(json1)): # for each page # check both images' filename and size, should be the same if os.path.basename(json1[p]['filename']) != os.path.basename(json2[p]['filename']): print('error, filenames are not the same',json1[p]['filename'],json2[p]['filename']) continue if json1[p]['size'] != json2[p]['size']: print('error, image sizes are not the same',json1[p]['size'],json2[p]['size']) continue Panel.img_size = json1[p]['size'] Panel.small_panel_ratio = Panel.DEFAULT_MIN_PANEL_SIZE_RATIO panels_v1 = list(map(lambda p: Panel(p), json1[p]['panels'])) panels_v2 = list(map(lambda p: Panel(p), json2[p]['panels'])) known_panels = [[],[]] j = -1 for p1 in panels_v1: j += 1 if p1 in panels_v2: known_panels[0].append(j) j = -1 for p2 in panels_v2: j += 1 if p2 in panels_v1: known_panels[1].append(j) images_dir = 'urls' if file_or_dir != 'urls': images_dir = file_or_dir if os.path.isdir(file_or_dir) else os.path.dirname(file_or_dir) images_dir = os.path.relpath(images_dir,'tests/results')+'/' if len(known_panels[0]) != len(panels_v1) or len(known_panels[1]) != len(panels_v2): files_diff[json1[p]['filename']] = { 'jsons': [[json1[p]],[json2[p]]], 'images_dir': images_dir, 'known_panels': [json.dumps(known_panels[0]),json.dumps(known_panels[1])] } return files_diff
def group_small_panels(self, panels, filename): i = 0 panels_to_add = [] while i < len(panels): p1 = panels[i] if not p1.is_small(): i += 1 continue # build up a group of panels that are close to one another big_panel = p1 grouped = [i] for j in range(i + 1, len(panels)): p2 = panels[j] if j == i or not p2.is_small(): continue if p2.is_close(big_panel): grouped.append(j) # build up bigger panel for current group big_panel = Panel.merge(big_panel, p2) if len(grouped) <= 1: del panels[i] continue # continue from same index i, which is a new panel (previous panel at index i has just been removed) else: # add new grouped panel, if not small if not big_panel.is_small(): panels_to_add.append(big_panel) tmp_img = self.dbg.draw_panels( self.img, list(map(lambda k: panels[k], grouped)), Debug.colours["lightblue"], ) tmp_img = self.dbg.draw_panels(tmp_img, [big_panel], Debug.colours["green"]) self.dbg.add_image(tmp_img, "Group small panels") # remove all panels in group for k in reversed(grouped): del panels[k] i += 1 for p in panels_to_add: panels.append(p) self.dbg.add_step("Group small panels", panels) return panels
def __init__(self, options: T.Optional[T.Dict[str, T.Any]] = None): self.options = {} if options is None: options = {} self.dbg = Debug("debug" in options and options["debug"]) for o in ["progress", "rtl"]: self.options[o] = o in options and options[o] if self.options["rtl"]: Panel.set_numbering("rtl") self.options[ "min_panel_size_ratio"] = Panel.DEFAULT_MIN_PANEL_SIZE_RATIO if "min_panel_size_ratio" in options and options[ "min_panel_size_ratio"]: self.options["min_panel_size_ratio"] = options[ "min_panel_size_ratio"]
def get_files_diff(file_or_dir, json1, json2): files_diff = {} for p in range(len(json1)): # for each page # check both images' filename and size, should be the same if os.path.basename(json1[p]["filename"]) != os.path.basename( json2[p]["filename"] ): print( "error, filenames are not the same", json1[p]["filename"], json2[p]["filename"], ) continue if json1[p]["size"] != json2[p]["size"]: print( "error, image sizes are not the same", json1[p]["size"], json2[p]["size"], ) continue Panel.img_size = json1[p]["size"] Panel.small_panel_ratio = Panel.DEFAULT_MIN_PANEL_SIZE_RATIO panels_v1 = list(map(lambda p: Panel(p), json1[p]["panels"])) panels_v2 = list(map(lambda p: Panel(p), json2[p]["panels"])) known_panels = [[], []] j = -1 for p1 in panels_v1: j += 1 if p1 in panels_v2: known_panels[0].append(j) j = -1 for p2 in panels_v2: j += 1 if p2 in panels_v1: known_panels[1].append(j) images_dir = "urls" if file_or_dir != "urls": images_dir = ( file_or_dir if os.path.isdir(file_or_dir) else os.path.dirname(file_or_dir) ) images_dir = os.path.relpath(images_dir, "tests/results") + "/" if len(known_panels[0]) != len(panels_v1) or len(known_panels[1]) != len( panels_v2 ): files_diff[json1[p]["filename"]] = { "jsons": [[json1[p]], [json2[p]]], "images_dir": images_dir, "known_panels": [ json.dumps(known_panels[0]), json.dumps(known_panels[1]), ], } return files_diff