Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ocr(players, img, directory=''):
"""Use OCR to find postion of the playernames."""
cfg = '--psm 3 --oem 0'
if directory:
directory = os.path.join(os.path.dirname(directory), 'tessdata')
cfg = f'{cfg} --tessdata-dir "{directory}"'
crop_text = pytesseract.image_to_string(img, config=cfg)
items = re.split(r'\s+', crop_text)
threshold = 0.35
ratios = [0.0, 0.0]
positions = [None, None]
regex = re.compile(r"<[^>]+>")
for item_idx, item in enumerate(items):
item = regex.sub("", item)
for player_idx, player in enumerate(players):
ratio = SequenceMatcher(None, item.lower(), player.lower()).ratio()
if(ratio >= max(threshold, ratios[player_idx])):
positions[player_idx] = item_idx
ratios[player_idx] = ratio
found = ratios[0] > 0.0 and ratios[1] > 0.0
if found:
def captcharead(self, imagepath=None):
if imagepath is None:
print('Please give Image path in the function defined in sample_captcha.py file')
sys.exit(1)
#Resampling the Image
check_output(['convert', imagepath, '-resample', '600', imagepath])
#Returning the captcha text in the form of string.
return pytesseract.image_to_string(Image.open(imagepath))
def char_to_num(self, keyword, year, filepath):
jpgzoom = Image.open(filepath)
(x, y) = jpgzoom.size
x_s = 2 * x
y_s = 2 * y
out = jpgzoom.resize((x_s, y_s), Image.ANTIALIAS)
file = filepath.split('/')[-1].split('.')[0]
out.save('%s/%s_tmp3/%s.jpg' % (keyword, year, file), "JPEG", quality=100)
num = pytesseract.image_to_string(out)
if num:
num = num.lower().replace("'", '').replace('!', '').replace('.', '').replace(',', '').replace('?', '7').replace("S", '5').replace(" ","").replace("E", "8").replace("B", "8").replace("I", "1").replace("$", "8").replace("a", "8").replace('n', "11").replace('o', '0')
print(num)
return num
else:
return 'error'
def update_ocr(self, test_set=None):
"""
Update the stats by parsing and extracting the text from the games stats page using the
tesseract OCR engine to perform text parsing.
Note that the current screen should be the stats page before calling this method.
"""
for key, region in STATS_COORDS.items():
if test_set:
image = Image.open(test_set[key])
else:
self.grabber.snapshot(region=region)
image = self._process()
text = pytesseract.image_to_string(image, config='--psm 7')
self.logger.debug("OCR result: {key} -> {text}".format(key=key, text=text))
# The images do not always parse correctly, so we can attempt to parse out our expected
# value from the STATS_COORD tuple being used.
# Firstly, confirm that a number is present in the text result, if no numbers are present
# at all, safe to assume the OCR has failed wonderfully.
if not any(char.isdigit() for char in text):
self.logger.warning("No digits found in OCR result, skipping key: {key}".format(key=key))
continue
# Otherwise, attempt to parse out the proper value.
try:
if len(text.split(':')) == 2:
value = text.split(':')[-1].replace(" ", "")
else:
for i in "_" + string.digits + string.letters + "}{!@#$%^&*()-_=+[]:;?|\\/.,><":
move(settings)
click()
move(inputs)
click()
move(flaginp)
doubclick()
typeflag(flag + i)
move(settings)
click()
move(start)
click()
time.sleep(5)
prev = pyautogui.screenshot(region=(325, 900, 721-400, 1019-855))
prev.save("temp.jpg")
text = pytesseract.image_to_string(Image.open("temp.jpg"))
print flag + i
move(xbutton)
click()
time.sleep(0.1)
if "flag" in text.encode("UTF-8") or "Your" in text.encode("UTF-8"):
flag += i
break
exit()
pyautogui.moveTo(100, 150)
pyautogui.click
#MeepwnCTF{W3llc0m3_2_Th3_Bl4ck_P4r4d3}
def detectRaidTime(self, raidpic, hash, raidNo, radius):
zero = datetime.datetime.now()
unixnow = time.mktime(zero.timetuple())
log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectRaidTime: Reading Raidtimer')
height, width, channel = raidpic.shape
raidtimer = raidpic[int(round(radius*2*0.03)+(2*radius)+(radius*2*0.265)):int(round(radius*2*0.03)+(2*radius)+(radius*2*0.43)), 0:width]
raidtimer = cv2.resize(raidtimer, (0,0), fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
emptyRaidTempPath = os.path.join(self.tempPath, str(raidNo) + str(hash) + '_emptyraid.png')
cv2.imwrite(emptyRaidTempPath, raidtimer)
rt = Image.open(emptyRaidTempPath)
gray = rt.convert('L')
bw = gray.point(lambda x: 0 if x<200 else 255, '1')
raidtimer = pytesseract.image_to_string(bw, config='--psm 6 --oem 3').replace(' ', '').replace('~','').replace('o','0').replace('O','0').replace('"','').replace('-','').replace('.',':').replace('B','8').replace('—','').replace('_','').replace("'","").replace('U','0').replace('/','7')
log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectRaidTime: detect raidtimer text: %s' % raidtimer)
#cleanup
os.remove(emptyRaidTempPath)
raidFound = len(raidtimer) > 0
if raidFound:
if ':' in raidtimer:
log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectRaidTime: found raidtimer %s' % raidtimer)
hatchTime = self.getHatchTime(raidtimer, raidNo)
if hatchTime:
log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectRaidTime: Hatchtime %s' % str(hatchTime))
#if hatchTime > unixnow + (int(args.raid_time) * 60) or hatchTime < unixnow:
#log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +') ] ' + 'detectRaidTime: Hatchtime not logical')
#return (raidFound, False, False, False)
#raidstart = getHatchTime(self, raidtimer) - self.timezone * (self.timezone*60*60)
raidstart = hatchTime #- (self.timezone * 60 * 60)
def tesser_image(image):
image = cv2.resize(image, (0,0), fx=2, fy=2)
ret,image = cv2.threshold(image,127,255,cv2.THRESH_BINARY)
#cv2.imshow('im', image)
#cv2.waitKey(0)
image = PIL.Image.fromarray(image, 'RGB')
txt = pytesseract.image_to_string(image)
txt = txt.replace(",", "")
txt = txt.replace(" ", "")
if len(txt) == 0:
txt = pytesseract.image_to_string(image, config='-psm 10')
try:
txt = int(txt)
except:
txt_list = list(txt)
for i in range(len(txt)):
if txt_list[i] == 'B':
txt_list[i] = '8'
elif txt_list[i] == 'l':
txt_list[i] = '1'
elif txt_list[i] == 'L':
txt_list[i] = '1'
elif txt_list[i] == 'i':
answer_two = os.system("screencapture -R \"{}\" ./answers_two.png".format(ans_two))
answer_thr = os.system("screencapture -R \"{}\" ./answers_thr.png".format(ans_thr))
question_img = Image.open("./question_screenshot.png")
answer_one_img = Image.open("./answers_one.png")
answer_two_img = Image.open("./answers_two.png")
answer_thr_img = Image.open("./answers_thr.png")
question_enh = getImageFromImageEnhanceForQuestion(question_img)
ans_one_enh = getImageFromImageEnhance(answer_one_img)
ans_two_enh = getImageFromImageEnhance(answer_two_img)
ans_thr_enh = getImageFromImageEnhance(answer_thr_img)
#使用简体中文解析图片
# print('OCR ' + datetime.datetime.now().strftime('%H:%M:%S'))
question_text = pytesseract.image_to_string(question_enh, lang='chi_sim')
# print(question_text)
ans_one_text = pytesseract.image_to_string(ans_one_enh, lang='chi_sim')
# print(ans_one_text)
ans_two_text = pytesseract.image_to_string(ans_two_enh, lang='chi_sim')
# print(ans_two_text)
ans_thr_text = pytesseract.image_to_string(ans_thr_enh, lang='chi_sim')
# print(ans_thr_text)
question = question_text
answers = [ans_one_text, ans_two_text, ans_thr_text]
# print(answers)
return question, answers
def index():
if 'image' not in request.files:
return jsonify(
error={
'image': 'This field is required.'
}
), 400
image = Image.open(request.files['image'])
image_text = pytesseract.image_to_string(
image,
lang='eng'
)
return jsonify({
'text': image_text
})