Exemple #1
0
 def test_image(self):
     """Test SetImage and GetUTF8Text."""
     self._api.Init()
     self._api.SetImage(self._image)
     text = self._api.GetUTF8Text()
     self.assertIn('quick', text)
     text2 = tesserocr.image_to_text(self._image)
     self.assertEqual(text, text2)
Exemple #2
0
 def ocr_page(self, page_no):
     if tesserocr is None:
         return ''
     with self.get_image(page_no, resolution=300) as img:
         pil_image = PILImage.frombytes('RGB', img.size, img.make_blob('RGB'))
         return tesserocr.image_to_text(
             pil_image,
             lang=TESSERACT_LANGUAGE[self.language],
             path=self.config.get('TESSERACT_DATA_PATH', '')
         )
Exemple #3
0
def run_ocr(jid):
    """Perform OCR using Tesseract on the :class:`~toja.models.image.Image` with the id ``jid`` and the type ``'joke'``.

    Creates a new :class:`~toja.models.transcription.Transcription` for the image with the OCRed text set as the
    text and a ``status`` of ``'ocr'``. If such a :class:`~toja.models.transcription.Transcription` already exists,
    then the text is updated instead.

    This is a dramatiq Actor, so can be run in the background.
    """
    storage_path = ConfigMiddleware.config_setting('app.images.storage.path')
    if storage_path:
        dbsession = DBSessionMiddleware.dbsession()
        joke = dbsession.query(Image).filter(
            and_(Image.id == jid, Image.type == 'joke')).first()
        if joke:
            transcription = dbsession.query(Transcription).filter(
                and_(Transcription.status == 'ocr',
                     Transcription.source == joke)).first()
            if not transcription:
                transcription = Transcription(source=joke,
                                              text={
                                                  'type':
                                                  'doc',
                                                  'content': [{
                                                      'type':
                                                      'paragraph',
                                                      'content': [{
                                                          'type': 'text',
                                                          'text': ''
                                                      }]
                                                  }]
                                              },
                                              status='ocr',
                                              attributes={})
            img = PILImage.open(os.path.join(storage_path, *joke.padded_id()))
            raw_text = tesserocr.image_to_text(img).strip().replace('--', '—')
            transcription.text = {
                'type':
                'doc',
                'content': [{
                    'type':
                    'paragraph',
                    'content': [{
                        'type': 'text',
                        'text': para.replace('\n', ' ')
                    }]
                } for para in raw_text.split('\n\n') if para.strip() != '']
            }
            dbsession.add(transcription)
        else:
            raise OCRException("Joke not found in the database")
    else:
        raise OCRException(
            "Missing configuration setting app.images.storage.path")
Exemple #4
0
def getTextTest():
    image = cv2.imread("okey-check6.jpg", 0)
    api = PyTessBaseAPI()
    api.SetImage(Image.fromarray(image))
    # print api.GetUTF8Text()
    image = api.GetThresholdedImage()
    image.save("rus.new.exp6.tiff")
    text = image_to_text(image, lang="rus")
    # text = image_to_string(image, lang="rus")
    print text
    return receiptParser.getJson(text)
Exemple #5
0
def ocr_run(image_path):
    ocr_image = Image.open(image_path)
    value_text = tesserocr.image_to_text(ocr_image)
    value_text = value_text.lower()  # because letters are no fun
    value_text = value_text.replace("l", "1")
    value_text = value_text.replace("o", "0")
    value_text = value_text.replace("s", "5")
    value_text = value_text.strip()
    value_text = "SO" + value_text
    print("Value Text: " + str(value_text))
    return value_text
Exemple #6
0
def get_text(loc, typ):
    im = ImageGrab.grab(bbox=loc)
    #im.show()
    txt = tesserocr.image_to_text(im)
    #print(txt)
    # print("="*20)
    if typ == 'o':
        txt = get_opt(txt)
    else:
        txt = get_query(txt)
    return txt
Exemple #7
0
def image2text(image_name: str) -> str:
    image = Image.open(image_name)
    image = image.convert('L')
    """
    new_size = tuple(2 * x for x in image.size)             # enlarge the image size
    image = image.resize(new_size, Image.ANTIALIAS)
    """
    # image.show()
    return tesserocr.image_to_text(image,
                                   lang='eng+chi_sim',
                                   psm=tesserocr.PSM.SINGLE_BLOCK)
Exemple #8
0
def same_old_solve():
    """
    识别率依旧很低, 不提了...
    """
    for i in range(10001):
        picture_path = "cap1/im{}.png".format(i)
        picture = Image.open(picture_path)
        result1 = tesserocr.image_to_text(picture.convert('L'), lang='eng')
        result2 = tesserocr.file_to_text(picture_path, psm=tesserocr.PSM.AUTO)
        print(result1)
        print(result2)
Exemple #9
0
    def img_ocr(self, context, to_key, *args):
        """Optical Character Recognition(OCR) using tesseract

            Args:
                context(dict): dictionary of context info
                to_key(str): key to store ocr results
                *args:

            Returns:
                dict
        """
        # set PSM (Page Segmentation Mode) to 6 to handle
        # images containing only numerals
        psm = 6
        with open_image(context["uuid"], context["key"], self.redis_conn,
                        self.binary_r) as img:
            logger.info(image_to_text(img, psm=psm))
            # r redis conn basically global
            self.redis_conn.hset(context["uuid"], to_key,
                                 image_to_text(img, psm=psm).strip())
        return context
Exemple #10
0
def lambda_handler(event, context):
    supported_formats = ['jpeg', 'png', 'tiff']
    for record in event['Records']:
        size = record['s3']['object']['size']
        s3_name = record['s3']['bucket']['name']
        s3_key = record['s3']['object']['key']
        s3_path = s3_name + '/' + s3_key
        file_local = '/tmp/' + os.path.basename(s3_key)

        if size > 15728640:
            print('S3 object s3://' + s3_path +
                  ' larger than 50MB, hence did not process')

        try:
            s3_vid = record['s3']['object']['versionId']
        except KeyError:
            s3_vid = ''

        try:
            if s3_vid:
                s3.download_file(s3_name,
                                 s3_key,
                                 file_local,
                                 ExtraArgs={'VersionId': s3_vid})
            else:
                s3.download_file(s3_name, s3_key, file_local)
        except:
            print('Failed to download the S3 object: s3://' + s3_path)
            traceback.print_exc()
            continue

        try:
            ftype = imghdr.what(file_local)
            if ftype not in supported_formats:
                print('S3 object s3://' + s3_path +
                      ' is not one of the supported image format.')
                continue
            print('S3 object s3://' + s3_path + ' is of file type ' + ftype)
        except:
            print('Failed to check the format of the S3 object: s3://' +
                  s3_path)
            traceback.print_exc()
            continue

        try:
            print('Going to recognize ' + file_local)
            rec_text = tesserocr.image_to_text(PIL.Image.open(file_local))
            update_status(s3_path, s3_vid, rec_text)

        except:
            print('OCR failed to recognize the S3 object: s3://' + s3_path)
            traceback.print_exc()
            continue
Exemple #11
0
 def dist_captcha_code():
     img = Image.open('code.png')
     img_l = img.convert('L')
     threshold = 180
     table = []
     for j in range(256):
         if j < threshold:
             table.append(0)
         else:
             table.append(1)
     image = img_l.point(table, '1')
     return tesserocr.image_to_text(image)
Exemple #12
0
def getImage(filepath):
    image = Image.open(filepath)
    image = image.convert('L')
    image = image.point(lambda x: 0 if x < 159 else 255, '1')
    image.save("result.png")

    f = open('extractedTestFromRec.txt', 'w')
    a = tesserocr.image_to_text(image)
    print a
    #print type(a)
    f.write(a.encode("UTF-8"))
    f.close()
Exemple #13
0
    def get_market_change(self):
        area = serpent.cv.extract_region_from_image(
            self.current_frame.grayscale_frame,
            self.game.screen_regions["MARKET_CHANGE"])

        text = tesserocr.image_to_text(Image.fromarray(area))
        matches = re.findall("\d*", text)
        for match in matches:
            if len(match) > 0:
                return int(match.strip())

        return -1
def plate_recognition(plate, visualize=True):
    """Performs OCR with Tesseract to a plate image.

    Args:
        plate (nparray): License plate cropped from an image (OpenCV).
        visualize (bool): Processing images displayed or not (default True).
    
    Returns:
        plate_text (str): Text that Tesseract's been able to detect.

    """
    # Process the current plate crop to make it easier to recognize
    plate_processed = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)
    plate_processed = cv2.bilateralFilter(plate_processed, 11, 17, 17)
    plate_processed = cv2.threshold(plate_processed, 177, 255,
                                    cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    if visualize:
        cv2.imshow('Plate crop', plate)
        cv2.moveWindow('Plate crop', 0, 0)
        cv2.imshow('Processed plate', plate_processed)
        cv2.moveWindow('Processed plate', 0, plate.shape[1])
        cv2.waitKey(1000)

    # Pass the plate crop through an OCR system
    print("Without preprocessing: ")
    print("Pytesseract: {}".format(pytesseract.image_to_string(plate)))
    img = Image.fromarray(plate)
    print("OCR: {}".format(tesserocr.image_to_text(img)))

    print("With preprocessing: ")
    plate_text = pytesseract.image_to_string(plate_processed)
    print("Pytesseract: {}".format(plate_text))
    img = Image.fromarray(plate)
    print("OCR: {}".format(tesserocr.image_to_text(img)))
    #cv2.waitKey(0)
    plate_text = ''.join(ch for ch in plate_text if ch.isalnum())
    print('Recognized text into the license plate: {}'.format(plate_text))
    cv2.destroyAllWindows()

    return plate_text
Exemple #15
0
    def login(self):
        # 创建会话
        session = requests.Session()
        # 访问登录界面
        login_url = 'https://sso.ckcest.cn/portal/signin?locale=zh_CN&device=pc&type=account&service=http%3A%2F%2Fwww.ckcest.cn%2Fcas'
        login_view = session.get(url=login_url)
        res = Selector(text=login_view.text)
        # 获取请求数据
        app_key = res.xpath('//input[@name="app_key"]/@value').get()

        # 获取验证码
        code = token = ''
        while len(code) != 4:
            self.logger.info('********* 识别验证码 *********')
            url_image_json = 'https://sso.ckcest.cn/api/auth_web/captcha?width=120&height=51&type=char&app_key={}&timestamp={}'

            response_imgjson = session.get(
                url_image_json.format(app_key,
                                      time.time() * 1000)).json()
            token = response_imgjson.get('data').get('token')
            img_str = response_imgjson.get('data').get('base64Image').split(
                ',', 1)[-1]
            img_byte = base64.b64decode(img_str)
            img_file = io.BytesIO(img_byte)
            # 识别验证码
            img = Image.open(img_file)
            code = tesserocr.image_to_text(img).strip()
            # self.logger.info(f'********* {code}*********')

        # 访问登录API
        login_api = 'https://sso.ckcest.cn/api/auth_web/login_code_token'
        data = dict()
        data['account'] = '17109324122'
        data['password'] = '******'
        data['code'] = code
        data['app_key'] = app_key
        data[
            'service'] = 'https://sso.ckcest.cn/portal/signin?locale=zh_CN&device=pc&type=account&service=http%3A%2F%2Fwww.ckcest.cn%2Fcas&_s=http%3A%2F%2Fwww.ckcest.cn%2Fcas'
        data['_eventId'] = 'submit'
        data['token'] = token
        login = session.post(url=login_api, data=data)
        res = Selector(text=login.text)
        result = res.xpath('//title[contains(text(), "登录-中国工程科技知识")]').get()
        if result:
            # 循环登录
            return False

        # 请求表格数据  获取JSESSIONID和ASP.NET_SessionId 字段
        session.get(url=self.table_url)
        self.JSESSIONID = session.cookies.get('JSESSIONID',
                                              domain='zhcy.app.ckcest.cn')
        return True
Exemple #16
0
def main():
    image = Image.open('test.jpg')
    image = image.convert('L')
    threshold = 120
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table, '1')
    result = tesserocr.image_to_text(image)
    print(result)
Exemple #17
0
 def ImageRecognition(self):
     pointArr = self.__RotatePictures()
     plt.imshow(pointArr)
     plt.show()
     # 使用tesserocr进行识别,lang选择自己训练的语言库
     result = tesserocr.image_to_text(pointArr, lang='fontyp')
     # 格式化输出结果
     result = [
         re.findall('[A-Za-z0-9\u4e00-\u9fa5]', result[i])
         for i in range(len(result))
     ]
     result = ''.join([i[0] if i else '' for i in result])
     return result
Exemple #18
0
    def decode_img(self, img_file):
        im = Image.open(img_file)
        reenforced_im = self.transform_img(im)

        expression = tesserocr.image_to_text(reenforced_im)

        code_value = None
        try:
            code_value = self.calculate(expression)
        except:
            pass

        return code_value
Exemple #19
0
def getText(rotation, image):
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    image = Image.open(image)
    image.save("loaded.jpg", dpi=(600, 600))
    image = cv2.imread("loaded.jpg", 0)
    if rotation != 0:
        image = np.rot90(image, (360 - rotation) / 90)
    # cv2.imwrite("rotated.jpg", image)
    api = PyTessBaseAPI()
    api.SetImage(Image.fromarray(image))
    image = api.GetThresholdedImage()
    text = image_to_text(image, lang="rus")
    return receiptParser.getJson(text)
Exemple #20
0
def img_ocr_key(context, key, to_key, *args):
    """Optical Character Recognition(OCR) using tesseract

        Args:
            context(dict): dictionary of context info
            key: key to use to ocr
            to_key(str): key to store ocr results
            *args:

        Returns:
            dict
    """
    # set PSM (Page Segmentation Mode) to 6 to handle
    # images containing only numerals
    psm = 6
    with open_image(context['uuid'], key) as img:
        logger.info(image_to_text(img, psm=psm))
        # r redis conn basically global
        # set PSM (Page Segmentation Mode) to 6 to handle
        # images containing only numerals
        r.hset(context['uuid'], to_key, image_to_text(img, psm=psm).strip())
    return context
Exemple #21
0
def solve():
    """
    先旋转再识别
    :return:
    """
    for i in range(10001):
        picture_path = "cap1/im{}.png".format(i)
        picture = Image.open(picture_path)
        # picture = rotate_picture(picture)
        picture = online_rotate_picture(picture)
        result1 = tesserocr.image_to_text(picture.convert("L"), lang="eng")
        # picture.show()
        print(result1.strip())
Exemple #22
0
def get_captcha():
    image = Image.open("1.jpg")
    image = image.convert('L')
    threshold = 220
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table, '1')
    result = tesserocr.image_to_text(image)
    print(result)
def test_image_orc():
    folder = './examples'
    for name in os.listdir(folder):
        if not name.endswith('.png'):
            continue

        path = os.path.join(folder, name)
        print(path)

        image = Image.open(path)
        result = tesserocr.image_to_text(image)

        print(result)
Exemple #24
0
def recognize_captcha(img_path):
    im = Image.open(img_path)
    # threshold = 140
    # table = []
    # for i in range(256):
    #     if i < threshold:
    #         table.append(0)
    #     else:
    #         table.append(1)
    #
    # out = im.point(table, '1')
    num = tesserocr.image_to_text(im)
    return num
Exemple #25
0
def main(input_path):
    try:
        image = Image.open(input_path)
        text = tesserocr.image_to_text(
            image,
            lang=
            'eng+rus+bul+grc+srp+srp_latn+Georgian+Greek+spa+Cyrillic+pol+nld+hun+fin+deu+tur'
        )
    # text = " ".join(text1.split())
    except:
        text = ""

    return text
Exemple #26
0
def zhihulogin(username, password):
    # 获取页面
    driver = webdriver.Chrome()
    driver.get('https://www.zhihu.com/#signin')
    driver.find_element_by_xpath(
        '//*[@class="SignContainer-switch"]/span').click()
    driver.find_element_by_name('username').send_keys(username)
    driver.find_element_by_name('password').send_keys(password)
    driver.find_element_by_class_name('SignFlow-submitButton').click()
    # 截取当前页面
    driver.save_screenshot("1.png")
    time.sleep(3)
    try:
        # 验证码所在的位置
        element = driver.find_element_by_class_name('Captcha-englishImg')
        location = element.location
        print(location)
        size = element.size
        print(size)

        # 计算出元素上下左右位置
        left = element.location['x']
        top = element.location['y']
        right = element.location['x'] + element.size['width']
        bottom = element.location['y'] + element.size['height']

        im = Image.open('1.png')
        im = im.crop((left, top, right, bottom))
        im.save('2.png')

        # 打开图片
        image = Image.open('2.png')
        image = image.convert('L')
        threshod = 127
        table = []
        for i in range(256):
            if i < threshod:
                table.append(0)
            else:
                table.append(1)
        image = image.point(table, '1')
        result = tesserocr.image_to_text(image)
        print('验证码是:', result)
        driver.find_element_by_class_name('SignFlowInput-errorMask').send_keys(
            result)
        driver.find_element_by_class_name('SignFlow-submitButton').click()
        time.sleep(10)

    except Exception as e:
        print('出现错误了~')
        print(e)
Exemple #27
0
    def get_working_buys(self):
        area = serpent.cv.extract_region_from_image(
            self.current_frame.grayscale_frame,
            self.game.screen_regions["WORKING_BUYS"])

        self.visual_debugger.store_image_data(area, area.shape, 3)

        text = tesserocr.image_to_text(Image.fromarray(area))
        matches = re.findall("\(\d\)", text)
        for match in matches:
            if len(match) > 0:
                return int(match.strip(' ()'))

        return -1
Exemple #28
0
def getText(rotation, image):
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    image = Image.open(image).convert("RGB")
    b, g, r = image.split()
    image = Image.merge("RGB", (r, g, b))
    image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
    if rotation != 0:
        image = np.rot90(image, (360 - rotation) / 90)
    api = PyTessBaseAPI()
    api.SetImage(Image.fromarray(image))
    image = api.GetThresholdedImage()
    text = image_to_text(image, lang="rus")
    # print text
    return receiptParser.getJson(text)
def image_test():
    image = Image.open('CheckCode.jpg')
    image = image.convert('L')
    threshold = 140
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table, '1')
    image.show()
    result = tesserocr.image_to_text(image)
    print(result)
Exemple #30
0
 def check_code(self, img_url):
     img = Image.open(img_url)
     threshold = 80
     img = img.convert('L')
     table = []
     for i in range(256):
         if i < threshold:
             table.append(0)
         else:
             table.append(1)
     img = img.point(table, '1')
     img.show()
     result = tesserocr.image_to_text(img)
     print(result)
def book_clear(image, threshold):
    image = image.convert("L")
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    img = image.point(table, "1")
    # img.save("./photo/img1.png")
    # img.show()
    result = tesserocr.image_to_text(img)
    print('灰度二值化之后:' + result)
    return img
Exemple #32
0
def get_code(url):
    flag = url.split("/")[-2]
    fn = flag + '.png'
    with open(fn, 'wb+') as sw:
        sw.write(requests.get(url).content)

    img = Image.open(fn)
    img = img.convert('L')
    # table = [0 if i < 127 else 1 for i in range(256)]
    # img = img.point(table, '1')
    result = tesserocr.image_to_text(img).strip()
    print(flag, result)
    if re.match('^[A-Za-z0-9]{4}$', result):
        return flag, result
Exemple #33
0
import tesserocr
from PIL import Image

image = Image.open('code2.jpg')

image = image.convert('L')
threshold = 127
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)

image = image.point(table, '1')
image.show()

result = tesserocr.image_to_text(image)
print(result)