Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_image_to_data_common_output(test_file, output):
"""Test and compare the type of the result."""
result = image_to_data(test_file, output_type=output)
expected_keys = [
'level', 'page_num', 'block_num', 'par_num', 'line_num', 'word_num',
'left', 'top', 'width', 'height', 'conf', 'text'
]
if output is Output.BYTES:
assert isinstance(result, bytes)
elif output is Output.DICT:
assert isinstance(result, dict)
assert bool(set(result.keys()).intersection(expected_keys))
elif output is Output.STRING:
assert isinstance(result, unicode if IS_PYTHON_2 else str)
for key in expected_keys:
assert key in result
def show_ocr_result(frame):
start_time = time.time()
text = pytesseract.image_to_string(frame, lang="eng", config="--psm 8")
print(text)
util.display_total_time(start_time)
start_time = time.time()
pytess_result = pytesseract.image_to_boxes(frame, lang="eng",
config="--psm 8", output_type=pytesseract.Output.DICT)
print(pytess_result)
util.display_total_time(start_time)
bbox_list = list()
for i, _ in enumerate(pytess_result['bottom']):
tl = (pytess_result['left'][i], pytess_result['bottom'][i])
br = (pytess_result['right'][i], pytess_result['top'][i])
bbox_list.append((tl, br))
util.show_frame(frame, bbox_list=bbox_list, wait_flag=True)
start_time = time.time()
pytess_data = pytesseract.image_to_data(frame, lang="eng",
config="--psm 8", output_type=pytesseract.Output.DICT)
print(pytess_data)
util.display_total_time(start_time)
def __internal_get_screen_text(self, screenpath: str, identifier) -> Optional[dict]:
origin_logger = get_origin_logger(logger, origin=identifier)
returning_dict: Optional[dict] = {}
origin_logger.debug("get_screen_text: Reading screen text")
try:
with Image.open(screenpath) as frame:
frame = frame.convert('LA')
try:
returning_dict = pytesseract.image_to_data(frame, output_type=Output.DICT, timeout=40,
config='--dpi 70')
except Exception as e:
origin_logger.error("Tesseract Error: {}. Exception: {}", returning_dict, e)
returning_dict = None
except (FileNotFoundError, ValueError) as e:
origin_logger.error("Failed opening image {} with exception {}", screenpath, e)
return None
if isinstance(returning_dict, dict):
return returning_dict
else:
origin_logger.warning("Could not read text in image: {}", returning_dict)
return None
import cv2
import pytesseract
from pytesseract import Output
img = cv2.imread('c:\a\screenshot_MotoG4.jpg')
d = pytesseract.image_to_data(img, output_type=Output.DICT)
n_boxes = len(d['level'])
for i in range(n_boxes):
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
origin_logger.debug("Screensize: W:{} x H:{}", width, height)
if width < 1080:
origin_logger.info('Resize screen ...')
frame_org = frame_org.resize([int(2 * s) for s in frame_org.size], Image.ANTIALIAS)
diff: int = 2
texts = [frame_org]
for thresh in [200, 175, 150]:
fn = lambda x : 255 if x > thresh else 0
frame = frame_org.convert('L').point(fn, mode='1')
texts.append(frame)
for text in texts:
try:
globaldict = pytesseract.image_to_data(text, output_type=Output.DICT, timeout=40,
config='--dpi 70')
except Exception as e:
origin_logger.error("Tesseract Error: {}. Exception: {}", globaldict, e)
globaldict = None
origin_logger.debug("Screentext: {}", globaldict)
if globaldict is None or 'text' not in globaldict:
continue
n_boxes = len(globaldict['level'])
for i in range(n_boxes):
if returntype != ScreenType.UNDEFINED:
break
if len(globaldict['text'][i]) > 3:
for z in self._ScreenType:
heightlimit = 0 if z == 21 else height / 4
if globaldict['top'][i] > heightlimit and globaldict['text'][i] in \
self._ScreenType[z]:
start_time = time.time()
pytess_result = pytesseract.image_to_boxes(frame, lang="eng",
config="--psm 8", output_type=pytesseract.Output.DICT)
print(pytess_result)
util.display_total_time(start_time)
bbox_list = list()
for i, _ in enumerate(pytess_result['bottom']):
tl = (pytess_result['left'][i], pytess_result['bottom'][i])
br = (pytess_result['right'][i], pytess_result['top'][i])
bbox_list.append((tl, br))
util.show_frame(frame, bbox_list=bbox_list, wait_flag=True)
start_time = time.time()
pytess_data = pytesseract.image_to_data(frame, lang="eng",
config="--psm 8", output_type=pytesseract.Output.DICT)
print(pytess_data)
util.display_total_time(start_time)
bbox_list = list()
for i, conf in enumerate(pytess_data['conf']):
if int(conf) != -1:
print("\tconf: {}".format(conf))
tl = (pytess_data['left'][i], pytess_data['top'][i])
br = (tl[0]+pytess_data['width'][i], tl[1]+pytess_data['height'][i])
bbox_list.append((tl, br))
util.show_frame(frame, bbox_list=bbox_list, wait_flag=True)