def recognize_text(self): self.text_lines, images = [], [] for (x0, y0), (x1, y1), (x2, y2), (x3, y3) in self.text_areas['boxes']: center = round((x0 + x1) / 2), round((y0 + y2) / 2) region = self.bgr[round(y0):round(y2), round(x0):round(x2)] d = pytesseract.image_to_data(region, output_type=pytesseract.Output.DICT) words = [] for text, conf, x, y, w, h in zip(d['text'], map(float, d['conf']), d['left'], d['top'], d['width'], d['height']): if conf > 0 and text: words.append({ "text": text.strip(), "conf": conf, "center": (round(x0 + x + w / 2), round(y0 + y + h / 2)) }) if words: line = { "center": center, "top": round(y0), "bottom": round(y2), "left": round(x0), "right": round(x1), "text": " ".join([word["text"] for word in words]), "words": words, "conf": sum([word["conf"] for word in words]) // len(words) } self.text_lines.append(line) if self.debug: is_success, im_buf_arr = cv2.imencode(".png", region) byte_im = im_buf_arr.tobytes() images.append((byte_im, line["text"])) if self.debug: display(create_gallery(images))
def notebook_image_marked(image, *marks, comment=""): image = image.copy() for center in marks: image = cv2.circle(image, center, 20, (0, 0, 255), 3) is_success, im_buf_arr = cv2.imencode(".png", image) byte_im = im_buf_arr.tobytes() display(create_gallery([(byte_im, comment)]))
def notebook_image(temp, file, comment=None): if comment is None: comment = file with open(os.path.join(temp, file), 'rb') as f: image = f.read() display(create_gallery([(image, comment)]))
def get_child_windows_from_xid(xid): ''' Given an X window id, return the xid's of its children ''' try: with display() as d: root = d.screen().root xw = find_xwindow_by_id(xid, root) children = [] for c in xw.query_tree().children: children.append(xid_to_str(c.id)) return children except Exception: return []
def get_child_windows_from_xid(xid): ''' Given an X window id, return the xid's of its children ''' try: with display() as d: root = d.screen().root xw = find_xwindow_by_id(xid, root) children = [] for c in xw.query_tree().children: children.append(xid_to_str(c.id)) return children except: return []
def type_string(s, interval=0): if interval: interval = float(interval) / 1000 def i(): time.sleep(interval) else: def i(): pass for c in s: sym = Xlib.XK.string_to_keysym(c) if sym: shifted = c.isupper() kc = display().keysym_to_keycode(sym) if shifted: key_down("SHIFT") key_down(kc) key_up(kc) if shifted: key_up("SHIFT") else: key_down("CONTROL") key_down("SHIFT") key_down("U") key_up("U") key_up("SHIFT") key_up("CONTROL") u = ord(c) u = format(u, "04X") for d in u: key_down(d) key_up(d) key_down("SPACE") key_up("SPACE")
def key_up(vk): if isinstance(vk, str): vk = KEY_CODES[vk.upper()] Xlib.ext.xtest.fake_input(display(), Xlib.X.KeyRelease, vk) display().sync()