Skip to content

Commit 4e60b03

Browse files
import local easyocr module
1 parent 83762d2 commit 4e60b03

1 file changed

Lines changed: 89 additions & 98 deletions

File tree

Framework/Built_In_Automation/Desktop/Windows/BuiltInFunctions.py

Lines changed: 89 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def install_ocr():
955955
if is_pytesseract_exe:
956956
CommonUtil.ExecLog(
957957
sModuleInfo,
958-
"Pytesseract executable file is alreday installed",
958+
"Pytesseract executable file is already installed",
959959
5,
960960
)
961961
else:
@@ -1144,7 +1144,6 @@ def image_search(step_data_set):
11441144
import numpy as np
11451145
import cv2
11461146
from pytesseract import pytesseract
1147-
import matplotlib.pyplot as plt
11481147
from difflib import SequenceMatcher
11491148

11501149
pytesseract.tesseract_cmd = os.environ["PROGRAMFILES"] + r"\Tesseract-OCR\tesseract.exe"
@@ -1255,107 +1254,99 @@ def new_image_text(step_data_set):
12551254
install_ocr()
12561255
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
12571256

1258-
import easyocr
1259-
import numpy as np
1260-
import cv2
1261-
from pytesseract import pytesseract
1262-
import matplotlib.pyplot as plt
1263-
from difflib import SequenceMatcher
1264-
1265-
pytesseract.tesseract_cmd = os.environ["PROGRAMFILES"] + r"\Tesseract-OCR\tesseract.exe"
1266-
1267-
file_name = ""
1268-
resolution = ""
1269-
idx = 0
1270-
confidence = 0.85
1271-
parent_dataset = []
1272-
image_text = ""
1273-
left_width = ""
1274-
top_height = ""
1275-
colour_state = ""
1276-
method_image = ""
1277-
language = ""
1278-
t_conf = 0.9
1279-
text_screenshot = ''
1280-
easyocr_paragraph = ''
1281-
1282-
for left, mid, right in step_data_set:
1283-
left = left.strip().lower()
1284-
mid = mid.strip().lower()
1285-
if mid == "element parameter":
1286-
if "index" in left:
1287-
idx = int(right.strip())
1288-
elif "ntext" in left:
1289-
image_text = right
1290-
elif 'language' in left:
1291-
language = right
1292-
elif 't_conf' in left:
1293-
t_conf = float(right)
1294-
elif 't_screenshot' in left:
1295-
text_screenshot = right
1296-
elif 'easyocr_paragraph' in left:
1297-
easyocr_paragraph = right
1298-
1299-
PIL.ImageGrab.grab().save("sample.png")
1300-
reader = easyocr.Reader([language])
1301-
if easyocr_paragraph == 'true':
1302-
output = reader.readtext("sample.png", paragraph=True)
1303-
else:
1304-
output = reader.readtext("sample.png", paragraph=False)
1257+
try:
1258+
from Framework import easyocr
1259+
import numpy as np
1260+
import cv2
1261+
from pytesseract import pytesseract
1262+
from difflib import SequenceMatcher
13051263

1306-
item = []
1307-
count = 0
1308-
crop_counter = 0
1309-
for text in output:
1310-
def seq(a, b):
1311-
c = SequenceMatcher(a=a, b=b).ratio()
1312-
print(c)
1313-
if c > 0.8:
1314-
return c
1315-
else:
1316-
return .00004
1317-
1318-
rslt = seq(image_text, text[1])
1319-
if rslt >= float(t_conf):
1320-
# if image_text in text[1]:
1321-
item.append([text])
1322-
print(text)
1323-
CommonUtil.ExecLog(sModuleInfo, "Found %s text. Returning element of index %s" % (image_text, count), 1)
1324-
count = count + 1
1325-
else:
1326-
print(text)
1327-
continue
1264+
pytesseract.tesseract_cmd = os.environ["PROGRAMFILES"] + r"\Tesseract-OCR\tesseract.exe"
13281265

1329-
if item == []:
1330-
CommonUtil.ExecLog(sModuleInfo, 'Could not find text "%s"' % image_text, 3)
1331-
return "zeuz_failed"
1332-
cord = np.array(item[idx][0])
1333-
cord1 = cord.tolist()
1334-
#
1335-
x_min, y_min = [min(cord_val) for cord_val in zip(*cord1[0])]
1336-
x_max, y_max = [max(cord_val) for cord_val in zip(*cord1[0])]
1337-
1338-
if text_screenshot != '':
1339-
img = cv2.imread("sample.png")
1340-
cropping = img[y_min:y_max, x_min:x_max]
1341-
cv2.imwrite('cropped_image.png', cropping)
1342-
from PIL import Image
1343-
1344-
image = Image.open('cropped_image.png')
1345-
gray_image = image.convert('L')
1346-
output_2 = pytesseract.image_to_string(gray_image)
1347-
print(output_2)
1348-
1349-
if output_2 in image_text or image_text in output_2:
1350-
element = x_min, y_min, x_max - x_min, y_max - y_min
1351-
crop_counter = 1
1352-
return _Element(element)
1266+
idx = 0
1267+
image_text = ""
1268+
language = ""
1269+
t_conf = 0.9
1270+
text_screenshot = ''
1271+
easyocr_paragraph = ''
1272+
1273+
for left, mid, right in step_data_set:
1274+
left = left.strip().lower()
1275+
mid = mid.strip().lower()
1276+
if mid == "element parameter":
1277+
if "index" in left:
1278+
idx = int(right.strip())
1279+
elif "ntext" in left:
1280+
image_text = right
1281+
elif 'language' in left:
1282+
language = right
1283+
elif 't_conf' in left:
1284+
t_conf = float(right)
1285+
elif 't_screenshot' in left:
1286+
text_screenshot = right
1287+
elif 'easyocr_paragraph' in left:
1288+
easyocr_paragraph = right
1289+
1290+
PIL.ImageGrab.grab().save("sample.png")
1291+
reader = easyocr.Reader([language])
1292+
if easyocr_paragraph == 'true':
1293+
output = reader.readtext("sample.png", paragraph=True)
13531294
else:
1295+
output = reader.readtext("sample.png", paragraph=False)
1296+
1297+
item = []
1298+
count = 0
1299+
for text in output:
1300+
def seq(a, b):
1301+
c = SequenceMatcher(a=a, b=b).ratio()
1302+
print(c)
1303+
if c > 0.8:
1304+
return c
1305+
else:
1306+
return .00004
1307+
1308+
rslt = seq(image_text, text[1])
1309+
if rslt >= float(t_conf):
1310+
# if image_text in text[1]:
1311+
item.append([text])
1312+
print(text)
1313+
CommonUtil.ExecLog(sModuleInfo, "Found %s text. Returning element of index %s" % (image_text, count), 1)
1314+
count = count + 1
1315+
else:
1316+
print(text)
1317+
continue
1318+
1319+
if item == []:
13541320
CommonUtil.ExecLog(sModuleInfo, 'Could not find text "%s"' % image_text, 3)
13551321
return "zeuz_failed"
1356-
else:
1357-
element = x_min, y_min, x_max - x_min, y_max - y_min
1358-
return _Element(element)
1322+
cord = np.array(item[idx][0])
1323+
cord1 = cord.tolist()
1324+
#
1325+
x_min, y_min = [min(cord_val) for cord_val in zip(*cord1[0])]
1326+
x_max, y_max = [max(cord_val) for cord_val in zip(*cord1[0])]
1327+
1328+
if text_screenshot != '':
1329+
img = cv2.imread("sample.png")
1330+
cropping = img[y_min:y_max, x_min:x_max]
1331+
cv2.imwrite('cropped_image.png', cropping)
1332+
from PIL import Image
1333+
1334+
image = Image.open('cropped_image.png')
1335+
gray_image = image.convert('L')
1336+
output_2 = pytesseract.image_to_string(gray_image)
1337+
print(output_2)
1338+
1339+
if output_2 in image_text or image_text in output_2:
1340+
element = x_min, y_min, x_max - x_min, y_max - y_min
1341+
return _Element(element)
1342+
else:
1343+
CommonUtil.ExecLog(sModuleInfo, 'Could not find text "%s"' % image_text, 3)
1344+
return "zeuz_failed"
1345+
else:
1346+
element = x_min, y_min, x_max - x_min, y_max - y_min
1347+
return _Element(element)
1348+
except:
1349+
return CommonUtil.Exception_Handler(sys.exc_info())
13591350

13601351

13611352
def _scale_image(file_name, size_w, size_h):

0 commit comments

Comments
 (0)