def test(): os.chdir(_BASEDIR) print("recognize", tr.recognize("imgs/line.png")) img_path = "imgs/id_card.jpeg" # img_path = "imgs/name_card.jpg" img_pil = Image.open(img_path) print(img_pil.size) color_pil = img_pil.convert("RGB") gray_pil = img_pil.convert("L") img_draw = ImageDraw.Draw(color_pil) colors = ['red', 'green', 'blue', "purple"] t = time.time() n = 1 for _ in range(n): tr.detect(gray_pil, flag=tr.FLAG_RECT) print("time", (time.time() - t) / n) results = tr.run(gray_pil, flag=tr.FLAG_ROTATED_RECT) for i, rect in enumerate(results): cx, cy, w, h, a = tuple(rect[0]) print(i, "\t", rect[1], rect[2]) box = cv2.boxPoints(((cx, cy), (w, h), a)) box = np.int0(np.round(box)) for p1, p2 in [(0, 1), (1, 2), (2, 3), (3, 0)]: img_draw.line(xy=(box[p1][0], box[p1][1], box[p2][0], box[p2][1]), fill=colors[i % len(colors)], width=2) color_pil.show()
def test(): print("recognize", tr.recognize("imgs/line.png")) img_path = "imgs/id_card.jpeg" # img_path = "imgs/name_card.jpg" img_pil = Image.open(img_path) try: if hasattr(img_pil, '_getexif'): # from PIL import ExifTags # for orientation in ExifTags.TAGS.keys(): # if ExifTags.TAGS[orientation] == 'Orientation': # break orientation = 274 exif = dict(img_pil._getexif().items()) if exif[orientation] == 3: img_pil = img_pil.rotate(180, expand=True) elif exif[orientation] == 6: img_pil = img_pil.rotate(270, expand=True) elif exif[orientation] == 8: img_pil = img_pil.rotate(90, expand=True) except: pass MAX_SIZE = 1600 if img_pil.height > MAX_SIZE or img_pil.width > MAX_SIZE: scale = max(img_pil.height / MAX_SIZE, img_pil.width / MAX_SIZE) new_width = int(img_pil.width / scale + 0.5) new_height = int(img_pil.height / scale + 0.5) img_pil = img_pil.resize((new_width, new_height), Image.ANTIALIAS) color_pil = img_pil.convert("RGB") gray_pil = img_pil.convert("L") img_draw = ImageDraw.Draw(color_pil) colors = ['red', 'green', 'blue', "purple"] t = time.time() n = 1 for _ in range(n): tr.detect(gray_pil, flag=tr.FLAG_RECT) print("time", (time.time() - t) / n) results = tr.run(gray_pil, flag=tr.FLAG_ROTATED_RECT) for i, rect in enumerate(results): cx, cy, w, h, a = tuple(rect[0]) print(i, "\t", rect[1], rect[2]) box = cv2.boxPoints(((cx, cy), (w, h), a)) box = np.int0(np.round(box)) for p1, p2 in [(0, 1), (1, 2), (2, 3), (3, 0)]: img_draw.line(xy=(box[p1][0], box[p1][1], box[p2][0], box[p2][1]), fill=colors[i % len(colors)], width=2) color_pil.show()
import tr # detect text lines, return list of (x, y, w, h) print(tr.detect("imgs/web.png")) # recognize text line, return (text, confidence) print(tr.recognize("imgs/line.png")) # detect and recognize, return list of ((x, y, w, h), text, confidence) print(tr.run("imgs/name_card.jpg"))
scale = max(img_pil.height / MAX_SIZE, img_pil.width / MAX_SIZE) new_width = int(img_pil.width / scale + 0.5) new_height = int(img_pil.height / scale + 0.5) img_pil = img_pil.resize((new_width, new_height), Image.ANTIALIAS) color_pil = img_pil.convert("RGB") gray_pil = img_pil.convert("L") img_draw = ImageDraw.Draw(color_pil) colors = ['red', 'green', 'blue', "purple"] t = time.time() n = 1 for _ in range(n): tr.detect(gray_pil, flag=tr.FLAG_RECT) print("time", (time.time() - t) / n) results = tr.run(gray_pil, flag=tr.FLAG_ROTATED_RECT) for i, rect in enumerate(results): cx, cy, w, h, a = tuple(rect[0]) print(i, "\t", rect[1], rect[2]) box = cv2.boxPoints(((cx, cy), (w, h), a)) box = np.int0(np.round(box)) for p1, p2 in [(0, 1), (1, 2), (2, 3), (3, 0)]: img_draw.line(xy=(box[p1][0], box[p1][1], box[p2][0], box[p2][1]), fill=colors[i % len(colors)], width=2) color_pil.show()
import tr # detect text lines, return list of (x, y, width, height) print(tr.detect("imgs/web.png")) # detect text lines with angle, return list of (cx, cy, width, height, angle) print(tr.detect("imgs/id_card.jpeg", tr.FLAG_ROTATED_RECT)) # recognize text line, return (text, confidence) print(tr.recognize("imgs/line.png")) # detect and recognize, return list of ((x, y, width, height), text, confidence) print(tr.run("imgs/name_card.jpg"))