MOHAN KRISHNA

0 %
Mohan Krishna
Multimedia Professional
Ai & ML Researcher & Enthusiast
  • Residence:
    India
  • City:
    Vijayawada
  • Age:
    46
AI/ML Enthusiast. New Media Trainer, VFX Artist, Non Linear Video Editor, Graphic Designer, Sound Editor and iOS App Designer.
Telugu
English
Hindi
Tamil
Proficiency:
Graphic Design
Web Design
Video & VFX
Machine Learning
Artificial Intelligence
Digital Marketing
Areas of Interest:
Take a look at some of the things I love working on.
  • Non Linear Video Editing
  • Graphic Design
  • Web Design
  • Audio Editing
  • Content Management Systems
  • Python
  • Deep Learning
  • OpenCV
  • Image Classification

Document Word Detection

September 26, 2022
'''
output : word detection on document (Simple OCR type of application)
'''
# http://www.pillalamarri.in/python/document-word-detection/
import cv2
import numpy as np
import imutils

# frame read
frame = cv2.imread('test.jpeg')

# resize
frame = cv2.resize(frame, (600, 600))

# grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# remove noise
blur = cv2.GaussianBlur(gray, (5, 5), 0)

# otsu thresh (bimodel thresold)
thresh = cv2.threshold(blur, 0, 255,
                       cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# get structuring element

horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1))
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 25))
print('horizontal kernel : {}'.format(horizontal_kernel))
print('vertical kernel : {}'.format(vertical_kernel))

# opening (erosion followed by dilation)

horizontal_lines = cv2.morphologyEx(thresh,
                                    cv2.MORPH_OPEN,
                                    horizontal_kernel,
                                    iterations=2)
vertical_lines = cv2.morphologyEx(thresh,
                                  cv2.MORPH_OPEN,
                                  vertical_kernel,
                                  iterations=2)

# contours apply on detected lines
# First one is source image, second is contour retrieval mode, third is contour approximation method

cnts = cv2.findContours(horizontal_lines, cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)
cntsv = cv2.findContours(vertical_lines, cv2.RETR_EXTERNAL,
                         cv2.CHAIN_APPROX_SIMPLE)

# find contours
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cntsv = cntsv[0] if len(cntsv) == 2 else cntsv[1]

for c in cnts:
    cv2.drawContours(frame, [c], -1, (255, 255, 255), 2)
for c in cntsv:
    cv2.drawContours(frame, [c], -1, (255, 255, 255), 2)

# imshow
cv2.imshow('thresh', thresh)
cv2.imshow('horizontal_lines', horizontal_lines)
cv2.imshow('vertical_lines', vertical_lines)
cv2.imshow('frame', frame)

# grayscale

gray1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
thresh1 = cv2.adaptiveThreshold(gray1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 23, 30)
canny = imutils.auto_canny(thresh1)

output = cv2.bitwise_not(canny)
kernel = np.ones((5, 5), np.uint8)

opening = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)

dilation = cv2.dilate(canny, kernel, iterations=1)

contour, hierachy = cv2.findContours(dilation, cv2.RETR_TREE,
                                     cv2.CHAIN_APPROX_SIMPLE)

for i in contour:
    area = cv2.contourArea(i)
    if area > 20:
        x, y, w, h = cv2.boundingRect(i)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 120, 255), 2)

cv2.imshow('output', output)
cv2.imshow('dilate', dilation)
cv2.imshow('opening', opening)
cv2.imshow('original_frame', frame)
cv2.imshow('canny', canny)
cv2.imshow('thresh1', thresh1)

# Saving output image
cv2.imwrite('output.jpg', frame)

# destroy all window
cv2.waitKey(0)
cv2.destroyAllWindows()
# http://www.pillalamarri.in/python/document-word-detection/
Posted in PythonTags: