Skip to content

Instantly share code, notes, and snippets.

@scottpanton
Created December 27, 2020 03:39
Show Gist options
  • Save scottpanton/a52fa59d1429b71efbe525973ab41ed1 to your computer and use it in GitHub Desktop.
Save scottpanton/a52fa59d1429b71efbe525973ab41ed1 to your computer and use it in GitHub Desktop.
Crop RGBImages from https://sourceforge.net/p/videosubfinder to only include subtitles identified in TXTImages for use with Google Vision API
import cv2,os,numpy,math,glob
path = '/some/place/'
frames = [os.path.basename(x) for x in glob.glob(path + 'TXTImages/*.jpeg')]
#add padding around sub
pad = 40
for frame in frames:
print(frame)
txtpath = path + 'TXTImages/' + frame
croppath = path + 'RGBCropped/' + frame
rgbpath = path + 'RGBImages/' + frame
#load txt image and find bounding box of contents
im = cv2.imread(txtpath)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
imgray = cv2.blur(imgray,(15,15))
ret,thresh = cv2.threshold(imgray,math.floor(numpy.average(imgray)),255,cv2.THRESH_BINARY_INV)
dilated=cv2.morphologyEx(thresh, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10)))
contours,_ = cv2.findContours(dilated,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
best_box=[-1,-1,-1,-1]
for c in contours:
x,y,w,h = cv2.boundingRect(c)
if best_box[0] < 0:
best_box=[x,y,x+w,y+h]
else:
if x<best_box[0]:
best_box[0]=x
if y<best_box[1]:
best_box[1]=y
if x+w>best_box[2]:
best_box[2]=x+w
if y+h>best_box[3]:
best_box[3]=y+h
x1,y1,x2,y2 = best_box
#add padding but don't exceed edge of image then crop and save
x1 = max(x1-pad, 0)
y1 = max(y1-pad, 0)
x2 = min(x2+pad, im.shape[1])
y2 = min(y2+pad, im.shape[0])
rgb = cv2.imread(rgbpath)
cropped = rgb[y1:y2, x1:x2]
cv2.imwrite(croppath, cropped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment