User MSER and NMS for OCR
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
# NMS (Non Maximum Suppression, Non Maximum Suppression)
def nms(boxes, overlapThresh):
if len(boxes) == 0:
return []
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
# Take four coordinate arrays
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
# Computational Area Array
area = (x2 - x1 + 1) * (y2 - y1 + 1)
# Sort by score (if there is no confidence score, it can be sorted by coordinates from small to large, such as lower right corner coordinates)
idxs = np.argsort(y2)
# Start traversing and delete duplicate boxes
while len(idxs) > 0:
# Put the bottom right box into the pick array
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
# Find the maximum and minimum coordinates in the remaining frames
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
# Calculate the proportion of overlapping area to the corresponding box, i.e. IoU
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
# If IoU is greater than the specified threshold, delete
idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0])))
return boxes[pick].astype("int")
folder = r'C:\Work\OCR_Orientation\data\test data'
files = [os.path.join(folder, f) for f in os.listdir(folder)]
# Read pictures
imagePath = files[0]
img = cv2.imread(imagePath)
# Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
orig = img.copy()
# Calling MSER algorithm
mser = cv2.MSER_create()
regions, _ = mser.detectRegions(gray) # Get the text area
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] # Drawing text areas
cv2.polylines(img, hulls, 1, (0, 255, 0))
#cv2.imshow('img', img)
# Processing irregular detection boxes into rectangular boxes
keep = []
for c in hulls:
x, y, w, h = cv2.boundingRect(c)
keep.append([x, y, x + w, y + h])
#cv2.rectangle(vis, (x, y), (x + w, y + h), (255, 255, 0), 1)
boxes = np.array(keep)
merged = nms(boxes, 0.5)
for box in merged:
x1, y1, x2, y2 = box
cv2.rectangle(vis, (x1, y1), (x2, y2), (255, 0, 0), 1)
#cv2.imshow("hulls", vis)
plt.figure(figsize=(30,20))
plt.imshow(vis)
plt.show()