Skip to content

Instantly share code, notes, and snippets.

@anshumankmr
Created March 1, 2020 15:29
Show Gist options
  • Save anshumankmr/c15c2f5ad69971ae00ada6909b8cc4d1 to your computer and use it in GitHub Desktop.
Save anshumankmr/c15c2f5ad69971ae00ada6909b8cc4d1 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
def getTextOverlay(img):
pathname = "."
output = np.zeros(img.shape, dtype=np.uint8)
vis = img.copy()
channels = cv2.text.computeNMChannels(img)
# Append negative channels to detect ER- (bright regions over dark background)
cn = len(channels)-1
for c in range(0,cn):
channels.append((255-channels[c]))
# Apply the default cascade classifier to each independent channel (could be done in parallel)
print("Extracting Class Specific Extremal Regions from "+str(len(channels))+" channels ...")
print(" (...) this may take a while (...)")
for channel in channels:
erc1 = cv2.text.loadClassifierNM1(pathname+'/trained_classifierNM1.xml')
er1 = cv2.text.createERFilterNM1(erc1,16,0.00015,0.13,0.2,True,0.1)
erc2 = cv2.text.loadClassifierNM2(pathname+'/trained_classifierNM2.xml')
er2 = cv2.text.createERFilterNM2(erc2,0.5)
regions = cv2.text.detectRegions(channel,er1,er2)
rects = cv2.text.erGrouping(img,channel,[r.tolist() for r in regions])
#Visualization
for r in range(0,np.shape(rects)[0]):
rect = rects[r]
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 0), 2)
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 255, 255), 1)
output = vis.copy()
return output
image = cv2.imread('./simpsons_frame0.png')
print(type(image))
output = getTextOverlay(image)
cv2.imwrite('simpons_text.png', output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment