Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

text detection from image using opencv python

#wasim shaikh github:httperror451
import cv2
import numpy as np
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:Program FilesTesseract-OCR	esseract.exe"

# Load image, convert to HSV format, define lower/upper ranges, and perform
# color segmentation to create a binary mask
image = cv2.imread('1.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 218])
upper = np.array([157, 54, 255])
mask = cv2.inRange(hsv, lower, upper)

# Create horizontal kernel and dilate to connect text characters
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3))
dilate = cv2.dilate(mask, kernel, iterations=5)

# Find contours and filter using aspect ratio
# Remove non-text contours by filling in the contour
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ar = w / float(h)
    if ar < 5:
        cv2.drawContours(dilate, [c], -1, (0,0,0), -1)

# Bitwise dilated image with mask, invert, then OCR
result = 255 - cv2.bitwise_and(dilate, mask)
data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
print(data)

cv2.imshow('mask', mask)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey()
Comment

text detection from image using opencv python

import cv2
import numpy as np
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:Program FilesTesseract-OCR	esseract.exe"

# Load image, convert to HSV format, define lower/upper ranges, and perform
# color segmentation to create a binary mask
image = cv2.imread('1.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 218])
upper = np.array([157, 54, 255])
mask = cv2.inRange(hsv, lower, upper)

# Create horizontal kernel and dilate to connect text characters
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3))
dilate = cv2.dilate(mask, kernel, iterations=5)

# Find contours and filter using aspect ratio
# Remove non-text contours by filling in the contour
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ar = w / float(h)
    if ar < 5:
        cv2.drawContours(dilate, [c], -1, (0,0,0), -1)

# Bitwise dilated image with mask, invert, then OCR
result = 255 - cv2.bitwise_and(dilate, mask)
data = pytesseract.image_to_string(result, lang='eng',config='--psm 6')
print(data)

cv2.imshow('mask', mask)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey()
Comment

PREVIOUS NEXT
Code Example
Python :: Math Module floor() Function in python 
Python :: user passes test django 
Python :: list append python 3 
Python :: Try using .loc[row_indexer,col_indexer] = value instead 
Python :: pynput keyboard backspace 
Python :: change version of python that poetry use 
Python :: dataframe cut 
Python :: autopy python not installing 
Python :: dataframe coulmn to list 
Python :: series change index pandas 
Python :: how to open chrome console in selenium webdriver 
Python :: how to make spinning donut on python 
Python :: how to use underscore in python 
Python :: python encoding declaration 
Python :: sphinx themes 
Python :: get pattern from string python 
Python :: 1024x768 
Python :: time complexity of data structures in python 
Python :: Use the correct syntax to print the first item in the fruits tuple. 
Python :: data.head on terminal 
Python :: how to get value_counts() order 
Python :: import turtle t=turtle.turtle() def star(t): for t in range(5): t.color("red") t.pendown() t.begin_fill() t.forward(100) t.right(144) t.end_fill() 
Python :: File "main.py", line 21 print("total harga:idr", bakso bulat +str Minuman Drink): ^ SyntaxError: invalid syntax 
Python :: space weather dashboard build your own custom dashboard to analyze and predict weather 
Python :: convert code c++ to python online 
Python :: split a column into two columns pandas 
Shell :: linux check if x11 
Shell :: brew install wine 
Shell :: dotnet ef not found 
Shell :: maven test class 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =