def datasetPreprocessing(label_file, folder, output): if folder[-1] != '/': folder += '/' if output[-1] != '/': output += '/' length = len(open(label_file).readlines()) with open(label_file) as fp: print("Processing files:") for i, line in enumerate(fp): printProgressBar(i, length) if line[0] != '#': l = line.strip().split(" ") if l[1] == 'ok': impath = (folder + l[0].split('-')[0] + '/' + l[0].split('-')[0] + '-' + l[0].split('-')[1] + '/' + l[0] + '.png') img = cv2.imread(impath) if img is not None: img = imageNorm(img, 60, border=False, tilt=True, hystNorm=True) cv2.imwrite( output + "%s_%s.jpg" % (l[-1], time.time()), img)
def recognise(self, img): """ Recognising word and printing it """ # Pre-processing the word img = imageNorm(img, 60, border=False, tilt=True, hystNorm=True) # Separate letters img = cv2.copyMakeBorder(img, 0, 0, 30, 30, cv2.BORDER_CONSTANT, value=[0, 0, 0]) gaps = charSeg.segmentation(img, RNN=True, debug=True) chars = [] for i in range(len(gaps) - 1): char = img[:, gaps[i]:gaps[i + 1]] # TODO None type error after treshold char, dim = letterNorm(char, is_thresh=True, dim=True) # TODO Test different values if dim[0] > 4 and dim[1] > 4: chars.append(char.flatten()) chars = np.array(chars) word = '' if len(chars) != 0: pred = self.charClass.run(chars) for c in pred: word += idx2char(c) # print("Word: " + word) return word
def loadImages(dataloc, idx=0, num=None): """ Load images and labels """ print("Loading words...") dataloc += '/' if dataloc[-1] != '/' else '' # Load images and short them from the oldest to the newest imglist = glob.glob(os.path.join(dataloc, u'*.jpg')) imglist.sort(key=lambda x: float(x.split("_")[-1][:-4])) tmpLabels = [name[len(dataloc):] for name in imglist] labels = np.array(tmpLabels) images = np.empty(len(imglist), dtype=object) if num is None: upper = len(imglist) else: upper = min(idx + num, len(imglist)) num += idx for i, img in enumerate(imglist): # TODO Speed up loading - Normalization if i >= idx and i < upper: images[i] = imageNorm(cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB), height=60, border=False, tilt=True, hystNorm=True) printProgressBar(i - idx, upper - idx - 1) print() return (images[idx:num], labels[idx:num])
(0, 255, 0), 1) implt(img, t="Separated characters") return gaps # In[5]: img = cv2.imread("./output/words/normal/0.jpg") implt(img) # In[6]: img = cv2.imread("./output/words/normal/85.jpg") implt(img) img = imageNorm(img, 60, border=False, tilt=True, hystNorm=True) implt(img) img = cv2.copyMakeBorder(img, 0, 0, 30, 30, cv2.BORDER_CONSTANT, value=[0, 0, 0]) implt(img) # In[12]: gaps = segmentation(img, step=2, RNN=True, debug=False) # In[13]: