Skip to content

Commit 71d3d05

Browse files
committed
Create note_highlighter_test.py
1 parent 4950b02 commit 71d3d05

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Backend/note_highlighter_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import easyocr
2+
import cv2
3+
4+
# Load the image
5+
img = cv2.imread('img2.jpg')
6+
7+
# Initialize the EasyOCR reader with English language
8+
reader = easyocr.Reader(['en'])
9+
10+
# Perform OCR on the image and get the detail information including bounding boxes
11+
results = reader.readtext(img, detail=1)
12+
13+
# Print all the detected words
14+
print("Detected Words:")
15+
for result in results:
16+
text = result[1]
17+
confidence = result[2]
18+
print(f"Word: {text}, Confidence: {confidence}")
19+
20+
# Create an overlay image as a copy of the original image
21+
overlay = img.copy()
22+
23+
# Get the inputted string to search and highlight
24+
input_string = input("Enter the string to highlight: ")
25+
26+
# Split the inputted string into individual words
27+
input_words = input_string.lower().split()
28+
29+
# Iterate over the OCR results
30+
for result in results:
31+
# Get the current detected word
32+
detected_word = result[1].lower()
33+
34+
# Check if the current detected word matches any word in the inputted string
35+
if any(word in detected_word for word in input_words):
36+
# Get the bounding box coordinates
37+
bbox = result[0]
38+
x, y, w, h = bbox[0][0], bbox[0][1], bbox[2][0] - bbox[0][0], bbox[2][1] - bbox[0][1]
39+
40+
# Draw a filled rectangle around the detected word with the correct size and color
41+
cv2.rectangle(overlay, (x, y), (x + w, y + h), (0, 0, 255), -1)
42+
43+
# Blend the overlay image with the original image
44+
alpha = 0.4
45+
img_new = cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0)
46+
47+
# Calculate the aspect ratio for resizing
48+
aspect_ratio = img.shape[1] / img.shape[0]
49+
50+
# Set the width for resizing and calculate the corresponding height
51+
width = 1000
52+
height = int(width / aspect_ratio)
53+
54+
# Resize the resulting image
55+
resized = cv2.resize(img_new, (width, height), interpolation=cv2.INTER_AREA)
56+
57+
# Display the resized image
58+
cv2.imshow('img', resized)
59+
cv2.waitKey(0)
60+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)