-
Notifications
You must be signed in to change notification settings - Fork 4
/
preprocessing_robotext.py
71 lines (52 loc) · 1.97 KB
/
preprocessing_robotext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import cv2
import os
import matplotlib.pyplot as plt
from parse import *
# extracting frames from videos
def extract_frame(folderPath, videoName, subfolder):
if not os.path.exists(os.path.join(folderPath, subfolder)):
os.mkdir(os.path.join(folderPath, subfolder))
vidcap = cv2.VideoCapture(folderPath + '/' + videoName)
success, image = vidcap.read()
count = 0
while success:
cv2.imwrite(folderPath + "/" + subfolder + "/IMG_%s_%04d.jpg" % (subfolder, count),
image) # save frame as JPEG file
success, image = vidcap.read()
# print(count, ': Read a new frame: ', success)
count += 1
return "Frames extracted successfully"
# Parsing the bbox file and get the text crop from the videos
def get_bb(filename):
x = 0
y = 0
hei = 0
wid = 0
# print(filename.replace('.jpg','.txt'))
if not os.path.exists(filename): return x, y, hei, wid
count = len(open(filename).readlines())
fp = open(filename, "r")
for i in range(count):
line = fp.readline()
ss = parse("'{}' [{},{},{},{}]", line)
# ss = parse("'{}' [{} {} {} {}]", line)
if ss == None: return 0, 0, 0, 0
y = int(float(ss[1])) # size_img[0] - int(float(ss[2]))
x = int(float(ss[2]))
wid = int(float(ss[3])) - int(float(ss[1]))
hei = int(float(ss[4])) - int(float(ss[2]))
# x = x- hei
return x, y, hei, wid
# retrieving and storing bg and text crops
def extract_bg_crop(folderPath):
images = [i for i in os.listdir(folderPath + "/frame")]
for image in images:
print(image)
txt_file = folderPath+'/' + image.replace('.jpg', '.txt')
x, y, hei, wid = get_bb(txt_file)
img_name = folderPath + '/frame/' + image
I = cv2.imread(img_name, cv2.COLOR_BGR2RGB)
crop = I[x:x + hei, y:y + wid]
# extracting the background from robotext videos
bg_crop = I[x + hei:x + 2 * hei, y:y + wid]
# store the files for further work