Skip to content

Commit

Permalink
code dump from local scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandip117 committed Sep 28, 2023
1 parent 1173525 commit 1cdd283
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from pathlib import Path
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter

import matplotlib.pyplot as plt
import keras_ocr
import cv2
import math
import numpy as np
from chris_plugin import chris_plugin, PathMapper

__version__ = '1.0.0'
Expand Down Expand Up @@ -64,6 +68,41 @@ def main(options: Namespace, inputdir: Path, outputdir: Path):
data = input_file.read_text()
frequency = data.count(options.word)
output_file.write_text(str(frequency))
pipeline = keras_ocr.pipeline.Pipeline()
final_image = inpaint_text(input_file, pipeline)
img_rgb = cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB)
cv2.imwrite(output_file, img_rgb)



def midpoint(x1, y1, x2, y2):
x_mid = int((x1 + x2) / 2)
y_mid = int((y1 + y2) / 2)
return x_mid, y_mid

def inpaint_text(img_path, pipeline):
# read image
img = keras_ocr.tools.read(img_path)
# generate (word, box) tuples
prediction_groups = pipeline.recognize([img])

mask = np.zeros(img.shape[:2], dtype="uint8")
for box in prediction_groups[0]:
x0, y0 = box[1][0]
x1, y1 = box[1][1]
x2, y2 = box[1][2]
x3, y3 = box[1][3]

x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)

thickness = int(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))

cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
thickness)
img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)

return img


if __name__ == '__main__':
Expand Down

0 comments on commit 1cdd283

Please sign in to comment.