Package: image-ocr
image-ocrNOTE :image-ocris an updated version ofkeras-ocrto work with the latest versions of python and tensorflow.It works exactly the same as keras-ocr, just dopip install image-ocrand replaceimport image_ocrin your project.It supports new Google Colaboratorypython 3.10backendInteractive examples-Detector Training-Recognizer Training-Recognizer Training - Custom set-UsingInformationsThis is a slightly polished and packaged version of theKeras CRNN implementationand the publishedCRAFT text detection model. It provides a high level API for training a text detection and OCR pipeline.Please seethe documentationfor more examples, including for training a custom model.Getting StartedInstallationimage-ocrsupports Python >= 3.9# To install from PyPi
pip install image-ocrUsingThe package ships with an easy-to-use implementation of the CRAFT text detection model fromthis repositoryand the CRNN recognition model fromthis repository.Tryimage-ocr on Colabimportmatplotlib.pyplotaspltimportimage_ocr# image-ocr will automatically download pretrained# weights for the detector and recognizer.pipeline=image_ocr.pipeline.Pipeline()# Get a set of three example imagesimages=[image_ocr.tools.read(url)forurlin['https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Kali_Linux_2.0_wordmark.svg/langfr-420px-Kali_Linux_2.0_wordmark.svg.png','https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Enseigne_de_pharmacie_lumineuse.jpg/180px-Enseigne_de_pharmacie_lumineuse.jpg','https://upload.wikimedia.org/wikipedia/commons/thumb/a/a6/Boutique_Christian_Lacroix.jpg/330px-Boutique_Christian_Lacroix.jpg',]]# Each list of predictions in prediction_groups is a list of# (word, box) tuples.prediction_groups=pipeline.recognize(images)# Plot the predictionsfig,axs=plt.subplots(nrows=len(images),figsize=(20,20))forax,image,predictionsinzip(axs,images,prediction_groups):image_ocr.tools.drawAnnotations(image=image,predictions=predictions,ax=ax)TrainingDetector training example :Detector Training ColabRecognizer training exampleRecognizer Training ColabRecognizer training with custom assetsRecognizer Training ColabComparing image-ocr and other OCR approachesYou may be wondering how the models in this package compare to existing cloud OCR APIs. We provide some metrics below andthe notebookused to compute them using the first 1,000 images in the COCO-Text validation set. We limited it to 1,000 because the Google Cloud free tier is for 1,000 calls a month at the time of this writing. As always, caveats apply:No guarantees apply to these numbers -- please beware and compute your own metrics independently to verify them. As of this writing, they should be considered a very rough first draft. Please open an issue if you find a mistake. In particular, the cloud APIs have a variety of options that one can use to improve their performance and the responses can be parsed in different ways. It is possible that I made some error in configuration or parsing. Again, please open an issue if you find a mistake!We ignore punctuation and letter case because the out-of-the-box recognizer in image-ocr (provided bythis independent repository) does not support either. Note that both AWS Rekognition and Google Cloud Vision support punctuation as well as upper and lowercase characters.We ignore non-English text.We ignore illegible text.modellatencyprecisionrecallAWS719ms0.450.48GCP388ms0.530.58image-ocr(scale=2)417ms0.530.54image-ocr(scale=3)699ms0.50.59Precision and recall were computed based on an intersection over union of 50% or higher and a text similarity to ground truth of 50% or higher.keras-ocrlatency values were computed using a Tesla P4 GPU on Google Colab.scalerefers to the argument provided toimage_ocr.pipelines.Pipeline()which determines the upscaling applied to the image prior to inference.Latency for the cloud providers was measured with sequential requests, so you can obtain significant speed improvements by making multiple simultaneous API requests.Each of the entries provides a link to the JSON file containing the annotations made on each pass. You can use this with the notebook to compute metrics without having to make the API calls yourself (though you are encoraged to replicate it independently)!Why not compare to Tesseract?In every configuration I tried, Tesseract did very poorly on this test. Tesseract performs best on scans of books, not on incidental scene text like that in this dataset.Advanced ConfigurationBy default if a GPU is available Tensorflow tries to grab almost all of the available video memory, and this sucks if you're running multiple models with Tensorflow and Pytorch. Setting any value for the environment variableMEMORY_GROWTHwill force Tensorflow to dynamically allocate only as much GPU memory as is needed.You can also specify a limit per Tensorflow process by setting the environment variableMEMORY_ALLOCATEDto any float, and this value is a float ratio of VRAM to the total amount present.To apply these changes, callimage_ocr.config.configure()at the top of your file where you importimage_ocr.ContributingTo work on the project, start by doing the following. These instructions probably do not yet work for Windows but if a Windows user has some ideas for how to fix that it would be greatly appreciated (I don't have a Windows machine to test on at the moment).# Install local dependencies for# code completion, etc.makeinit# Build the Docker container to run# tests and such.makebuildYou can get a JupyterLab server running to experiment with usingmake lab.To run checks before committing code, you can usemake format-check type-check lint-check test.To view the documentation, usemake docs.To implement new features, please first file an issue proposing your change for discussion.To report problems, please file an issue with sample code, expected results, actual results, and a complete traceback.TroubleshootingThis package is installingopencv-python-headlessbut I would prefer a differentopencvflavor.This is due toaleju/imgaug#473. You can uninstall the unwanted OpenCV flavor after installingimage-ocr. We apologize for the inconvenience.