{ "info": { "author": "Fausto Morales", "author_email": "faustomorales@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Operating System :: MacOS", "Operating System :: Microsoft :: Windows", "Operating System :: POSIX :: Linux", "Operating System :: Unix", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3 :: Only" ], "description": "# keras-ocr\nThis is a slightly polished and packaged version of the [Keras OCR example](https://github.com/keras-team/keras/blob/master/examples/image_ocr.py) and the published [CRAFT text detection model](https://github.com/clovaai/CRAFT-pytorch). It provides a high level API for training a text detection and OCR pipeline.\n\n## Getting Started\n\n### Installation\nYou must have the [Cairo library](https://cairographics.org/) installed to use the built in image generator. Then you can install the package.\n\n\n```bash\npip install git+https://github.com/faustomorales/keras-ocr.git#egg=keras-ocr\n```\n\n### Using\nThe following trains a simple OCR and shows how the different components work.\n\n#### Using pretrained text detection model\nThe package ships with an easy-to-use implementation of the CRAFT text detection model with the original weights.\n\n```python\nimport keras_ocr\n\ndetector = keras_ocr.detection.Detector(pretrained=True)\nimage = keras_ocr.tools.read('path/to/image.jpg')\n\n# Boxes will be an Nx4x2 array of box quadrangles\n# where N is the number of detected text boxes.\nboxes = detector.detect(images=[image])[0]\n```\n\n#### Complete end-to-end training\nYou may wish to train your own end-to-end OCR pipeline! Here's an example for how you might do it. Note that the image generator has many options not documented here (such as adding backgrounds and image augmentation). Check the documentation for the `keras_ocr.tools.get_image_generator` function for more details.\n\n```python\nimport string\n\nimport keras_ocr\n\n# The alphabet defines which characters\n# the OCR will be trained to detect.\nalphabet = string.digits + \\\n string.ascii_lowercase + \\\n string.ascii_uppercase + \\\n string.punctuation + ' '\n\n# For each text sample, the text generator provides\n# a list of (category, string) tuples. The category\n# is used to select which fonts the image generator\n# should choose from when rendering those characters \n# (see the image generator step below) this is useful\n# for cases where you have characters that are only\n# available in some fonts. You can replace this with\n# your own generator, just be sure to match\n# that function signature if you are using\n# recognizer.get_image_generator. Alternatively,\n# you can provide your own image_generator altogether.\n# The default text generator uses the DocumentGenerator\n# from essential-generators.\ndetection_text_generator = keras_ocr.get_text_generator(\n max_string_length=32,\n alphabet=alphabet\n)\n\n# We first need to build and train a text detector.\ndetector = keras_ocr.detection.Detector(pretrained=True)\ndetector.model.compile(\n loss='mse',\n optimizer='adam'\n)\n\n# The image generator generates (image, sentence)\n# tuples where image is a HxWx1 image (grayscale)\n# and sentence is a string using only letters\n# from the selected alphabet. You can replace\n# this with your own generator, just be sure to match\n# that function signature.\ndetection_image_generator = keras_ocr.tools.get_image_generator(\n height=256,\n width=256,\n x_start=(10, 30),\n y_start=(10, 30),\n single_line=False,\n text_generator=detection_text_generator\n font_groups={\n 'characters': [\n 'Century Schoolbook',\n 'Courier',\n 'STIX',\n 'URW Chancery L',\n 'FreeMono'\n ]\n }\n)\ndetection_batch_generator = detector.get_batch_generator(\n image_generator=detection_image_generator,\n batch_size=2,\n)\ndetector.model.fit_generator(\n generator=detection_batch_generator,\n steps_per_epoch=100,\n epochs=10,\n workers=0\n)\ndetector.model.save_weights('v0_detector.h5')\n\n# Great, now we need a recogizer.\nrecognizer = keras_ocr.Recognizer(\n alphabet=alphabet,\n width=128,\n height=64\n)\n\n# Create a training model (requires you to set\n# a maximum string length).\nrecognizer.create_training_model(max_string_length=16)\n\n# This next part is similar to before but now\n# we adjust the image generator to provide only\n# single lines of text.\nrecognition_image_generator = keras_ocr.tools.convert_multiline_generator_to_single_line(\n multiline_generator=detection_image_generator,\n max_string_length=recognizer.training_model.input_shape[1][1],\n target_width=recognizer.model.input_shape[2],\n target_height=recognizer.model.input_shape[1]\n)\nrecognition_batch_generator = recognizer.get_batch_generator(\n image_generator=recognition_image_generator,\n batch_size=8\n)\nrecognizer.training_model.fit_generator(\n generator=recognition_batch_generator,\n steps_per_epoch=100,\n epochs=100\n)\n\n# You can save the model weights to use later.\nrecognizer.model.save_weights('v0_recognizer.h5')\n\n# Once training is done, you can use recognize\n# to extract text.\nimage, _, _ = next(detection_image_generator)\nboxes = detector.detect(images=[image])[0]\npredictions = recognizer.recognize_from_boxes(boxes=boxes, image=image)\nprint(predictions)\n```\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/faustomorales/keras-ocr", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "keras-ocr", "package_url": "https://pypi.org/project/keras-ocr/", "platform": "", "project_url": "https://pypi.org/project/keras-ocr/", "project_urls": { "Homepage": "https://github.com/faustomorales/keras-ocr" }, "release_url": "https://pypi.org/project/keras-ocr/0.2/", "requires_dist": [ "essential-generators", "keras", "imgaug" ], "requires_python": "", "summary": "A packaged and flexible version of the CRAFT text detector and Keras OCR example.", "version": "0.2" }, "last_serial": 5921425, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "aafe42bb4340d58f4bdfa231b7a51066", "sha256": "ba11645c906fc291cf43fdae6963bef91c6b5a955cac84ac21b62f30d6b6bfcb" }, "downloads": -1, "filename": "keras_ocr-0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "aafe42bb4340d58f4bdfa231b7a51066", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 19982, "upload_time": "2019-10-03T02:43:53", "url": "https://files.pythonhosted.org/packages/a7/60/5d457d075229a5433e1252ec6923dfb0d0607d073ddce89e0988e856ec2b/keras_ocr-0.1-py3-none-any.whl" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "236dbe0d191056dc4cd3df9d8abeb95b", "sha256": "60b8f86254418002863dfab626212a6eaa24d4b246ed2054233882d350faa7da" }, "downloads": -1, "filename": "keras_ocr-0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "236dbe0d191056dc4cd3df9d8abeb95b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 19987, "upload_time": "2019-10-03T02:46:19", "url": "https://files.pythonhosted.org/packages/76/1b/cd8dfc0ad3ed90c7c7ac782bccac9adcedf3c0542c081a4027a07762b4cd/keras_ocr-0.2-py3-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "236dbe0d191056dc4cd3df9d8abeb95b", "sha256": "60b8f86254418002863dfab626212a6eaa24d4b246ed2054233882d350faa7da" }, "downloads": -1, "filename": "keras_ocr-0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "236dbe0d191056dc4cd3df9d8abeb95b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 19987, "upload_time": "2019-10-03T02:46:19", "url": "https://files.pythonhosted.org/packages/76/1b/cd8dfc0ad3ed90c7c7ac782bccac9adcedf3c0542c081a4027a07762b4cd/keras_ocr-0.2-py3-none-any.whl" } ] }