Skip to content

Commit

Permalink
Appended contrast function
Browse files Browse the repository at this point in the history
Contrast function https://www.w3.org/TR/WCAG21/
---------

Co-authored-by: Иван Горбунов <muervos@muervos.muervos>
  • Loading branch information
Giovaaanniii and Иван Горбунов authored Jan 27, 2025
1 parent 7a8a040 commit 0b70d3e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ pytesseract==0.3.10
pytest==7.2.2
python_Levenshtein==0.20.9
colorama==0.4.6
matplotlib==3.9.4
opencv-python==4.10.0.84
mss==9.0.1
qat==1.1.2
pyyaml==5.3.1
pyscreeze==0.1.21
pyscreeze==1.0.1
20 changes: 20 additions & 0 deletions src/pygats/boxes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# pytesseract boxes
import cv2 as cv
import pytesseract
"https://programmersought.com/article/896710504463/"

img = cv.imread("./src/pygats/test1.png")

img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

hImg, wImg, _ = img.shape

boxes = pytesseract.image_to_boxes(img)
for b in boxes.splitlines():
b = b.split(' ')
print(b)
x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
cv.rectangle(img, (x, hImg - y), (w, hImg - h), (0, 0, 255), 2)

cv.imshow("img", img)
cv.waitKey(0)
69 changes: 69 additions & 0 deletions src/pygats/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from pathlib import Path
import cv2
import pytesseract
from matplotlib import pyplot as plt
from PIL import Image
import src.pygats.recog as rec
import src.pygats.pygats as pyg
from src.pygats.formatters import MarkdownFormatter as MD

ctx = pyg.Context(MD())


def find_text():
img = cv2.imread("./src/pygats/test1.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 25))
dilation = cv2.dilate(thresh1, rect_kernel, iterations=1)
edges = cv2.Canny(gray, 100, 200)
plt.imshow(edges, cmap='gray')
plt.show()
contours, hierarchy = cv2.findContours(
dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(thresh1, None)
image_with_sift = cv2.drawKeypoints(thresh1, keypoints, None)
plt.imshow(cv2.cvtColor(image_with_sift, cv2.COLOR_BGR2RGB))
plt.title('SIFT Features')
plt.show()
print(f"Найдено контуров: {len(contours)}")
im2 = img.copy()
cv2.imwrite("./src/pygats/after.png", im2)
search_folder = Path('./src/pygats/search_folder')
search_folder.mkdir(parents=True, exist_ok=True)
with open("./tests/find/result.txt", "w+") as file:
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
cropped = im2[y:y + h, x:x + w]
text = pytesseract.image_to_string(cropped, "rus")
print(f"Распознанный текст: '{text}'")
file.write(text)
cv2.imwrite(f"./src/pygats/search_folder/{cnt}.png", cropped)
file.close()


def pygats_search():
file = open("tests/find/words.ru.txt")
texts = []
good_result = 0
failed_count = 0
text_count = 0
lines = file.readlines()
for line in lines:
if not line.strip():
continue
text_count += 1
try:
text = rec.SearchedText(line.strip(), "rus", None)
texts.append(text)
img = Image.open("./src/pygats/test1.png")
rec.check_text(ctx, img, text)
good_result += 1
except pyg.TestException:
failed_count += 1
if text_count >= 9:
break
print("успешно распознанных слов: ", good_result, "\n", "не распознанных слов: ", failed_count)
3 changes: 3 additions & 0 deletions src/pygats/test1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions tests/find/gen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
import time
from PIL import Image, ImageDraw, ImageFont

from pygats.search import pygats_search

def crop_image(img, w, h):
img_crop = img.crop((0, 0, w, h))
Expand All @@ -11,7 +11,7 @@ def crop_image(img, w, h):
def wrapper():
folder_result = Path(f'tests/find/result.txt')
start = time.time()
#gen("white", 350, 350, 'TimesNewRoman', 32, "File")
pygats_search()
seconds = int(time.time() - start)
seconds = seconds % (24 * 3600)
hours = seconds // 3600
Expand Down Expand Up @@ -65,4 +65,6 @@ def color_shade_gen(step: tuple = (1, 1, 1), size: tuple = (1920, 1080)):
new_image_data = [rgb] * (size[0] * size[1])
new_img = Image.new('RGB', size)
new_img.putdata(new_image_data)
new_img.save(f"{folder_path}/color:{rgb}.png", 'PNG')
new_img.save(f"{folder_path}/color:{rgb}.png", 'PNG')

wrapper()

0 comments on commit 0b70d3e

Please sign in to comment.