Package: detextify
DetextifyWhat is this?TL;DR: A Python library to remove unwanted pseudo-text from images generated by your favorite generative AI models (Stable Diffusion, Midjourney, DALL·E).BeforeAfterSo, why should I care?We all know generative AI is the coolest thing since sliced bread 🍞.But try using any off-the-shelf generative vision model and you'll quickly see that these systems can get... creative with interpreting
your prompts.Specifically, you'll observe all kinds of weird artifacts on your images from extra fingers on hands, to arms coming out of chests,
to alien text written in random places.For generative systems to actually be usable in downstream applications, we need to better control these outputs
and mitigate unwanted effects.We believe the next frontier for generative AI is aboutrobustnessandtrust. In other words, how can we architect
these systems to be controllable, relevant, and predictably consistent with our needs?Detextifyis the first phase in our vision of robustifying generative AI.If we get this right, we will unlock slews of new applications for generative systems that will change the landscape of human-AI collaboration. 🌎Cute, but what are you actually doing?Detextifyruns text detection on your image, masks the text boxes, and in-paints the masked regions
until your image is text-free.Detextifycan be run entirely on your local machine (usingTesseractfor text detection andStable Diffusionfor in-painting), or can call existing APIs
(Azurefor text detection andOpenAIorReplicatefor in-painting).Installationpip install detextifyAdditionally:To run text detection locally (as opposed to using the Azure API), you need toinstall Tesseract.To run in-painting locally (as opposed to using the OpenAI or Replicate APIs), you need a GPU with CUDA and cuDNN installed.UsageYou can remove unwanted text from your image in just a few lines 💪:fromdetextify.text_detectorimportTesseractTextDetectorfromdetextify.inpainterimportLocalSDInpainterfromdetextify.detextifierimportDetextifiertext_detector=TesseractTextDetector("/path/to/tesseract/installation")detextifier=Detextifier(text_detector,LocalSDInpainter())detextifier.detextify("/my/input/image/path.png","/my/output/image/path.png")and 💣💥, just like that, your image is cleared of any bizarre text artifacts.Or if you want to clean up a directory of PNG images, just wrap it in a for-loop:importglobfromdetextify.text_detectorimportTesseractTextDetectorfromdetextify.inpainterimportLocalSDInpainterfromdetextify.detextifierimportDetextifiertext_detector=TesseractTextDetector("/path/to/tesseract/installation")detextifier=Detextifier(text_detector,LocalSDInpainter())forimg_fileinglob.glob("/path/to/dir/*.png"):detextifier.detextify(img_file,img_file.replace(".png","_detextified.png"))We provide multiple implementations for text detection and in-painting (both local and API-based), and you are also free to add your own.Text DetectorsTesseractTextDetector(based onTesseract) runs locally.
Followthis guideto install thetesseractlibrary locally. On Ubuntu:sudo apt install tesseract-ocr
sudo apt install libtesseract-devTo find the path where it was installed (and pass it to theTesseractTextDetectorconstructor):whereis tesseractAzureTextDetectorcalls a computer vision API from Microsoft Azure. You will first need to create aComputer Vision resourcevia the Azure
portal. Once created, take note of the endpoint and the key.AZURE_CV_ENDPOINT="https://your-endpoint.cognitiveservices.azure.com"AZURE_CV_KEY="your-azure-key"text_detector=AzureTextDetector(AZURE_CV_ENDPOINT,AZURE_CV_KEY)Our evaluation shows that the two text detectors produce comparable results.In-paintersLocalSDInpainter(implemented via Huggingface'sdiffuserslibrary) runs locally and requires a GPU. Defaults toStable Diffusion v2 for in-painting.ReplicateSDInpaintercalls theReplicateAPI. Defaults to Stable Diffusion v2 for
in-painting (and requires an API key).DalleInpaintercalls theDALL·E 2API from OpenAI (and requires an API key).# You only need to instantiate one of the following:local_inpainter=LocalSDInpainter()replicate_inpainter=ReplicateSDInpainter("your-replicate-key")dalle_inpainter=DalleInpainter("your-openai-key")ContributingTo contribute, clone the repository, make your changes, commit and push to your clone, and submit a pull request.To build the library, you need to installpoetry:curl -sSL https://install.python-poetry.org | python3 -
# Add poetry to your PATH. Note the specific path will differ depending on your system.
export PATH="/home/ubuntu/.local/bin:$PATH"
# Check the installation was successful:
poetry --versionInstall dependencies fordetextify:poetry installTo execute a script, run:poetry run python your_script.pyPlease run the unit tests to make sure that your changes are not breaking the codebase:poetry run pytestAuthorsThis project was authored byMihail EricandJulia Turc. If you are building in the generative AI space, we want to hear from you!