Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ Highlighting Image Text

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Highlighting Image Text


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Image processing and data extraction has become one of the most powerful features of Machine Learning now. But doing it from scratch is a pain in the a**. The one thing programming taught me that no one else did is not the reinvent the wheel every time and to prioritize getting the job done. Keeping that in mind, I have come across an easy solution for the problem at hand.

The Problem At Hand

Raw image before highlighting

For simplicity's sake, let us consider this to be a page from a book. We want to highlight the word comment wherever it occurs. This could be an intuitive feature for image search engines to direct the users' attention to their desired content.

Solution

We are going to be using an OCR (Optical Character Recognition) engine called Tesseract for the image-to-text recognition part. It is free software, released under the Apache License. Install the engine for your desired OS from their official website. I'm using Windows for this. Add the installation path to your environment variables.

Create a python project with a virtual environment set up on it. Install the necessary packages.

pip install opencv-python # for image processing
pip install pytesseract # to use the ocr engine in your project
pip install pandas # to conduct search queries

In your main.py import the necessary libraries and define the necessary variables. Read the image from the source using the imread method. Make a copy of the original image for the overlay. Extract text information from the image. It is important to set the output_type to be a pandas Dataframe object which will ease the filtering process.

import cv2
from pytesseract import pytesseract, Output

ALPHA = 0.4

filename = "devto.png"
query = "comment"

img = cv2.imread(filename)
# make a copy of the original image for the highlight overlay
overlay = img.copy()

# extract text data from the image as a pandas Dataframe object
boxes = pytesseract.image_to_data(img, lang="ben+eng", output_type=Output.DATAFRAME)

The dataframe object returned has the following structure:

level page_num block_num par_num line_num word_num left top width height conf text
5 1 5 1 1 4 169 537 99 14 96.276794 comments

We are only interested in the text, left, top, width, and height columns. We need to prepare the dataframe for this specific job by applying various filters. Drop the rows that have NaN or empty string in the text column to make our data error-proof and the computations more efficient. The text column usually contains single words. We can iterate through each row to find out if any of them matches our query string.

# drop rows that have NaN values in the text column
boxes = boxes.dropna(subset=["text"])
# remove empty text rows
boxes = boxes[boxes["text"].str.len() > 1]
# Search through the text column for matching words
boxes[boxes["text"].str.contains(query.strip(), case=False)]

Now we can get started with the highlighting part. We will draw rectangular highlight boxes around the matched positions.

for _, box in boxes.iterrows():
    left = box["left"]
    top = box["top"]
    width = box["width"]
    height = box["height"]

    # draw a yellow rectangle around the matched text
    cv2.rectangle(
        overlay,
        (left, top),
        (left + width, top + height),
        (0, 255, 255),
        -1,
    )

# Add the overlay on the original image
img_new = cv2.addWeighted(overlay, ALPHA, img, 1 - ALPHA, 0)
# Some more image processing to make the highlights more realistic
r = 1000.0 / img_new.shape[1]
dim = (1000, int(img_new.shape[0] * r))
resized = cv2.resize(img_new, dim, interpolation=cv2.INTER_AREA)

Show the modified image using opencv's imshow method.

cv2.imshow("Highlighted", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

The result is this modified image with every occurring comment highlighted in yellow.
Highlighted image

Bonus Tip

The search-through mechanism in this process can only detect and highlight a single word or full sentence with exact matches. If we want to highlight words that are not in a single sentence, we just need to filter the dataframe with a little bit of pandas magic.

+ from pandas import concat

- boxes[boxes["text"].str.contains(query.strip(), case=False)]
+ boxes = concat(
        [
            boxes[boxes["text"].str.contains(word.strip(), case=False)]
            for word in query.split()
        ]
  )

With this, the user can query "essential comments" and it will highlight essential and comments even though they are not together.

...



๐Ÿ“Œ Techotronic all-in-one-favicon Plugin 4.6 on WordPress Apple-Text/GIF-Text/ICO-Text/PNG-Text/JPG-Text Persistent cross site scripting


๐Ÿ“ˆ 40.33 Punkte

๐Ÿ“Œ Highlighting Image Text


๐Ÿ“ˆ 35.11 Punkte

๐Ÿ“Œ Ghidra 101: Cursor Text Highlighting


๐Ÿ“ˆ 28.07 Punkte

๐Ÿ“Œ Plain Text Editor 1.2.1 - Simple distraction-free text editor without any rich text nonsense.


๐Ÿ“ˆ 24.2 Punkte

๐Ÿ“Œ Facing an issue in froala text editor, style of the text is lost when the text is cut


๐Ÿ“ˆ 24.2 Punkte

๐Ÿ“Œ Extract Text From Password Protected PDF: macOS Image-to-Text Feature


๐Ÿ“ˆ 23.18 Punkte

๐Ÿ“Œ Extract Text From Password Protected PDF: macOS Image-to-Text Feature


๐Ÿ“ˆ 23.18 Punkte

๐Ÿ“Œ Meet Tune-A-Video: An AI Framework To Address The Text-To-Video Generation Problem Through Existing Text-To-Image Generation Models


๐Ÿ“ˆ 23.18 Punkte

๐Ÿ“Œ Text-to-Text-to-Image Generative AI on a CPU | Intel Software


๐Ÿ“ˆ 23.18 Punkte

๐Ÿ“Œ Text-to-Text-to-Image Generative AI | AI Quarterly | Q1 2024 Intel Software


๐Ÿ“ˆ 23.18 Punkte

๐Ÿ“Œ Meet aMUSEd: An Open-Source and Lightweight Masked Image Model (MIM) for Text-to-Image Generation based on MUSE


๐Ÿ“ˆ 22.16 Punkte

๐Ÿ“Œ Free Lossless Image Format 0.3 image/image-pnm.cpp image_load_pnm denial of service


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Free Lossless Image Format 0.3 LibPNG image/image-png.cpp memory corruption


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ MiniMagick up to 4.9.3 lib/mini_magick/image.rb Image.open Image File privilege escalation


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Free Lossless Image Format 0.3 LibPNG image/image-png.cpp flif File memory corruption


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Free Lossless Image Format 0.3 LibPNG image/image-png.cpp flif File memory corruption


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Plone up to 4.3.2 Image Tag Image.py OFS.Image Reflected cross site scriting


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Image Roll - my new simple and fast GTK image viewer with basic image manipulation tools. Written in Rust.


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Avoiding lock-in for your image pipeline with Nuxt Image and Netlify Image CDN


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Kajona CMS 4.7 Image Handler /kajona/image.php __construct image Directory Traversal


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Kajona CMS 4.7 Image Handler /kajona/image.php __construct image Directory Traversal


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Free Lossless Image Format 0.3 image/image-pnm.cpp image_load_pnm Denial of Service


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Free Lossless Image Format 0.3 LibPNG image/image-png.cpp Uninitialized Memory unbekannte Schwachstelle


๐Ÿ“ˆ 21.14 Punkte

๐Ÿ“Œ Equifax: highlighting the problems with social security numbers


๐Ÿ“ˆ 20 Punkte

๐Ÿ“Œ A good piece highlighting some Linux Kernel Vulnerabilities


๐Ÿ“ˆ 20 Punkte

๐Ÿ“Œ International Womenโ€™s Day: Highlighting Resources for Female Developers


๐Ÿ“ˆ 20 Punkte

๐Ÿ“Œ Chrome 86: Improved Focus Highlighting, WebHID, and More


๐Ÿ“ˆ 20 Punkte











matomo