def analyzePixelRelevance(): "simplified implementation of paper: Zintgraf et al - Visualizing Deep Neural Network Decisions: Prediction Difference Analysis" # setup model model = Model(open(Constants.fnCharList).read(), DecoderType.BestPath, mustRestore=True) # read image and specify ground-truth text img = cv2.imread(Constants.fnAnalyze, cv2.IMREAD_GRAYSCALE) (w, h) = img.shape assert Model.imgSize[1] == w # compute probability of gt text in original image batch = Batch([Constants.gtText], [preprocess(img, Model.imgSize)]) (_, probs) = model.inferBatch(batch, calcProbability=True, probabilityOfGT=True) origProb = probs[0] grayValues = [0, 63, 127, 191, 255] if Constants.distribution == 'histogram': bins = [0, 31, 95, 159, 223, 255] (hist, _) = np.histogram(img, bins=bins) pixelProb = hist / sum(hist) elif Constants.distribution == 'uniform': pixelProb = [1.0 / len(grayValues) for _ in grayValues] else: raise Exception('unknown value for Constants.distribution') # iterate over all pixels in image pixelRelevance = np.zeros(img.shape, np.float32) for x in range(w): for y in range(h): # try a subset of possible grayvalues of pixel (x,y) imgsMarginalized = [] for g in grayValues: imgChanged = copy.deepcopy(img) imgChanged[x, y] = g imgsMarginalized.append(preprocess(imgChanged, Model.imgSize)) # put them all into one batch batch = Batch([Constants.gtText] * len(imgsMarginalized), imgsMarginalized) # compute probabilities (_, probs) = model.inferBatch(batch, calcProbability=True, probabilityOfGT=True) # marginalize over pixel value (assume uniform distribution) margProb = sum( [probs[i] * pixelProb[i] for i in range(len(grayValues))]) pixelRelevance[x, y] = weightOfEvidence(origProb, margProb) print(x, y, pixelRelevance[x, y], origProb, margProb) np.save(Constants.fnPixelRelevance, pixelRelevance)
def getTestBatch(self): # gray scale images of shape (Model.size[1], Model.size[0]) targetSplits = [] for i in range(self.testSize): output_array = [0]*self.imgSize[0] # output_array = [0]*self.imgSize[1] for split in self.testSamples[i].targetSplit: #output width size is 32 and not 128 split = int(split) # ratio = self.imgSize[0] / self.imgSize[1] # split = int(split / ratio) output_array[split] = 1 targetSplits.append(output_array) imgs = [] scaleFactors = [] for i in range(self.testSize): img = cv2.imread(self.testSamples[i].filePath, cv2.IMREAD_GRAYSCALE) # print(self.testSamples[i].filePath) img, scaleX, scaleY = preprocess(img, self.imgSize, False) imgs.append(img) scaleFactors.append((scaleX, scaleY)) # for i in range(len(imgs)): # cv2.imwrite('test_images/'+str(i)+'.png', imgs[i].T.astype(np.uint8)) return Batch(targetSplits, imgs, scaleFactors)
def getNext(self): "iterator" batchRange = range(self.currIdx, self.currIdx + self.batchSize) targetSplits = [] for i in batchRange: output_array = [0]*self.imgSize[0] # output_array = [0]*self.imgSize[1] for split in self.samples[i].targetSplit: #output width size is 128 split = int(split) # ratio = self.imgSize[0] / self.imgSize[1] # split = int(split / ratio) try: output_array[split] = 1 except IndexError as e: print(self.samples[i].filePath) exit(0) targetSplits.append(output_array) imgs = [] scaleFactors = [] for i in batchRange: img = cv2.imread(self.samples[i].filePath, cv2.IMREAD_GRAYSCALE) # print(self.samples[i].filePath) img, scaleX, scaleY = preprocess(img, self.imgSize, False) imgs.append(img) scaleFactors.append((scaleX, scaleY)) self.currIdx += self.batchSize return Batch(targetSplits, imgs, scaleFactors)
def infer(model, fnImg): img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') print(f'Probability: {probability[0]}')
def getNext(self): "iterator" batchRange = range(self.currIdx, self.currIdx + config.BATCH_SIZE) gtTexts = [self.samples[i].gtText for i in batchRange] imgs = [] for i in batchRange: try: self.binaryImageFile.seek(self.samples[i].imageStartPosition) img = np.frombuffer( self.binaryImageFile.read(self.samples[i].imageSize), np.dtype('B')) img = img.reshape(self.samples[i].imageHeight, self.samples[i].imageWidth) img = preprocess(img) # img = preprocess(img, config.IMAGE_WIDTH, config.IMAGE_HEIGHT, config.RESIZE_IMAGE, # config.CONVERT_IMAGE_TO_MONOCHROME, config.AUGMENT_IMAGE) imgs.append(img) except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) pass except ValueError as e: print("Value error({0}): {1}".format(e.errno, e.strerror)) pass except Error as e: print("Unexpected error:", sys.exc_info()[0]) print("Value error({0}): {1}".format(e.errno, e.strerror)) pass self.currIdx += config.BATCH_SIZE return Batch(gtTexts, imgs)
def infer(model, fnImg): #fnImg = fnImg total_segments = open(path + '/segmented words/total_segments.txt','r') file = total_segments.read() file = int(file) count = 0 for i in range(1,file+1): segments = open(path + '/segmented words/' + str(i) + '/segments.txt','r') file1 = segments.read() file1 = int(file1) for j in range(file1): fnImg1 = fnImg + str(i) + '/segment_no_%d' % j + '.png' for image_path in glob.glob(fnImg1 + "\\*.png"): image = imageio.imread(image_path) img = preprocess(image, Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) count+=1 file2 = open(path + '/results/output' + str(i) + '.txt','a') file2.write(recognized[0]) file2.write(" ") print('Recognized:', '"' + recognized[0] + '"') print('Probability:', probability[0]) file3 = open(path + '/results/output' + str(i) + '.txt','a') file3.write('\n')
def infer(model, fnImg): # if fnImg.shape[1] > 200 and fnImg.shape[0] > 60: # fnImg1 = cv2.resize(fnImg, (100, 32)) # 100, 32 and 85,32 # elif 150 < fnImg.shape[1] < 200 and fnImg.shape[0] > 30: # fnImg1 = cv2.resize(fnImg, (85, 32)) # else: # fnImg1 = cv2.resize(fnImg, (65, 32)) if fnImg.shape[1] > 200: fnImg1 = imutils.resize(fnImg, width=128) else: fnImg1 = imutils.resize(fnImg, width=100) gray = cv2.cvtColor(fnImg1, cv2.COLOR_BGR2GRAY) # thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,115,10) pxmin = np.min(gray) pxmax = np.max(gray) imgContrast = (gray - pxmin) / (pxmax - pxmin) * 255 # increased line width kernel = np.ones((1, 1), np.uint8) imgMorph = cv2.erode(imgContrast, kernel, iterations=1) img = preprocess(imgMorph, Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) word = '' return recognized[0]
def analyzeTranslationInvariance(): # setup model model = Model(open(Constants.fnCharList).read(), DecoderType.BestPath, mustRestore=True) # read image and specify ground-truth text img = cv2.imread(Constants.fnAnalyze, cv2.IMREAD_GRAYSCALE) (w, h) = img.shape assert Model.imgSize[1] == w imgList = [] for dy in range(Model.imgSize[0] - h + 1): targetImg = np.ones((Model.imgSize[1], Model.imgSize[0])) * 255 targetImg[:, dy:h + dy] = img imgList.append(preprocess(targetImg, Model.imgSize)) # put images and gt texts into batch batch = Batch([Constants.gtText] * len(imgList), imgList) # compute probabilities (texts, probs) = model.inferBatch(batch, calcProbability=True, probabilityOfGT=True) # save results to file f = open(Constants.fnTranslationInvarianceTexts, 'wb') pickle.dump(texts, f) f.close() np.save(Constants.fnTranslationInvariance, probs)
def infer(filePath): "recognize text in image provided by file path" model = Model(open(fnCharList).read(), mustRestore=True) img = preprocess(cv2.imread(fnInfer, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img] * Model.batchSize) recognized = model.inferBatch(batch) print('Recognized:', '"' + recognized[0] + '"')
def infer(model, fnImg): "recognize text in image provided by file path" #img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) img = preprocess(fnImg, Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) return probability[0], recognized[0]
def testing(): imgdirs = os.listdir('../data/') decoderType = DecoderType.BestPath model = Model(open(FilePaths.fnCharList).read(), decoderType, mustRestore=True) output = '' for (j, dire) in enumerate(imgdirs): imgFiles = os.listdir('../data/' + dire) # print(imgFiles) for (i, f) in enumerate(imgFiles): #fnImg=input("Enter the image path in '../data/_____' format\n") #fnImg=FilePaths.fnInfer fnImg = '../data/' + str(dire) + '/' + f print('fn=' + fnImg) img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img] * Model.batchSize ) # fill all batch elements with same input image recognized = model.inferBatch(batch) # recognize text print('Recognized:', '"' + recognized[0] + '"') output = output + recognized[0] + ' ' output = output + '\n' print(output)
def infer(model, fnImg): "recognize text in image provided by file path" '''img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print('Recognized:', '"' + recognized[0] + '"') f = open("wordLists.txt","w",encoding='UTF8') f.write(recognized[0]) f.close() print('Probability:', probability[0]) ''' wordList = [] file_list = os.listdir(fnImg) for item in file_list: print(item) imgName = fnImg + "\\" + item img = preprocess(cv2.imread(imgName,cv2.IMREAD_GRAYSCALE),Model.imgSize) batch = Batch(None,[img]) (recognized,probability) = model.inferBatch(batch, True) print('Recognized:', '"' + recognized[0] + '"') wordList.append(recognized[0]) #f = open("C:\\Users\\yea\\.spyder-py3\\answerSheet\\answerwordLists.txt","w",encoding='utf-8') with codecs.open("/Users/hcy/Desktop/GP/answer/answerwordLists.txt","w",encoding='utf8') as f: for i in range(0, len(wordList)): f.write(wordList[i]+"\n") #f.write(recognized[0]) f.close()
def infer(model, input_image): """ recognize input image """ img = preprocess(cv.imread(input_image, cv.IMREAD_GRAYSCALE), Model.image_size) batch = Batch(None, [img]) recognized = model.infer_batch(batch) print('Recognized:', '"' + recognized[0] + '"')
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print('Recognized:', '"' + recognized[0] + '"') print('Probability:', probability[0])
def getNext(self): "iterator" batchRange = range(self.currIdx, self.currIdx + self.batchSize) gtTexts = [self.samples[i].gtText for i in batchRange] imgs = [preprocess(cv2.imread(self.samples[i].filePath, cv2.IMREAD_GRAYSCALE), self.imgSize, self.dataAugmentation) for i in batchRange] self.currIdx += self.batchSize return Batch(gtTexts, imgs)
def img_gen(batch_size=50, input_shape=None): x = np.zeros((batch_size, width, height, 1), dtype=np.uint8) y = np.zeros((batch_size, label_len), dtype=np.uint8) # y = np.zeros((batch_size, ), dtype=np.uint8) while True: for ii in range(batch_size): while True: # abandon the lexicon which is longer than 16 characters pick_index = np.random.randint(0, wordfile_list_len - 1) line = wordfile_list[pick_index] #e.g. 'a01-000u-00-00 ok 154 408 768 27 51 AT A' #index: 0 1 2 3 4 5 6 7 8 line_separation = line.split(' ') #e.g. 'a01-000u-00-00' #index: 0 1 2 3 file_path_separation = line_separation[0].split('-') try: #e.g. '/Letsgo/data' 'a01' 'a01-000u' 'a01-000u-00-00' img_path = image_dir + file_path_separation[ 0] + '/' + file_path_separation[ 0] + '-' + file_path_separation[ 1] + '/' + line_separation[0] + '.png' except IndexError: print(IndexError) break #some text values are not just one word, so we have to add all values after index 8 line_length = len(line_separation) lexicon = str(' '.join(line_separation[8:line_length])).rstrip( ) #rstrip to get rid of the newline img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) # abandon the lexicon which is longer than 16 characters, because I set the label_len = 16, you can change it anyway. # some dataset images may be damaged during unzip if (img is not None) and len(lexicon) <= label_len: img_size = img.shape # (height, width, channels) if img_size[1] > 2 and img_size[0] > 2: break #if (img_size[1]/img_size[0]*1.0) < 6.4: #img_reshape = cv2.resize(img, (int(31.0/img_size[0]*img_size[1]), height)) #mat_ori = np.zeros((height, width - int(31.0/img_size[0]*img_size[1]), 1), dtype=np.uint8) #out_img = np.concatenate([img_reshape, mat_ori], axis=1).transpose([1, 0, 2]) #else: #out_img = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC) #print("DEBUGGING:_______", out_img.shape) #out_img = np.asarray(out_img).transpose([1, 0, 2]) out_img = preprocess(img, (width, height)) out_img = np.expand_dims(out_img, -1) # due to the explanation of ctc_loss, try to not add "-" for blank while len(lexicon) < label_len: lexicon += "-" x[ii] = out_img y[ii] = [characters.find(c) for c in lexicon] yield [ x, y, np.ones(batch_size) * int(input_shape[1] - 2), np.ones(batch_size) * label_len ], y
def infer(model, fnImg): """recognize text in image provided by file path""" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) # print('Probability:', '"' + str(round(float(probability[0]) * 100, 2)) + '%"') return recognized[0]
def load_different_image(): imgs = [] for i in range(1): imgs.append( preprocess( cv2.imread("../data/check_image/a ({}).png".format(i), cv2.IMREAD_GRAYSCALE), Model.imgSize)) return imgs
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img] * Model.batchSize) # fill all batch elements with same input image recognized = model.inferBatch(batch) # recognize text # print('Recognized:', '"' + recognized[0] + '"') # all batch elements hold same result # print(recognized[0]) return recognized[0]
def make_batches(folder_name, images_list): # Sort the images_list lexicographically # print(images_list) images_list.sort() # print(images_list) max_images_per_batch = 60 quo = len(images_list) // max_images_per_batch rem = len(images_list) % max_images_per_batch batch_range = range(0, max_images_per_batch) batches_list = [] images_filepaths = [] cnt = 0 while (quo > 0): new_images = [] gtTexts = [None for i in range(max_images_per_batch)] imgs = [ preprocess( cv2.imread(folder_name + "/" + images_list[i], cv2.IMREAD_GRAYSCALE), img_size) for i in range(cnt * max_images_per_batch, (cnt + 1) * max_images_per_batch) ] batches_list.append(Batch(gtTexts, imgs)) for i in range(cnt * max_images_per_batch, (cnt + 1) * max_images_per_batch): new_images.append(folder_name + "/" + images_list[i]) cnt += 1 quo -= 1 images_filepaths.append(new_images) if (quo == 0 and rem > 0): new_images = [] gtTexts = [None for i in range(rem)] imgs = [ preprocess( cv2.imread(folder_name + "/" + images_list[i], cv2.IMREAD_GRAYSCALE), img_size) for i in range(cnt * max_images_per_batch, len(images_list)) ] batches_list.append(Batch(gtTexts, imgs)) for i in range(cnt * max_images_per_batch, len(images_list)): new_images.append(folder_name + "/" + images_list[i]) images_filepaths.append(new_images) return batches_list, images_filepaths
def infer(model, fnImg, sentence_list, img_num): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch( None, [img] * Model.batchSize) # fill all batch elements with same input image recognized = model.inferBatch(batch) # recognize text sentence_list.append((img_num, recognized[0]))
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) # print(f'Recognized: "{recognized[0]}"') # print(f"Probability: {probability[0]}") return [recognized[0], probability[0]]
def infer(model, wordImg): "recognize text in image provided by file path" img = preprocess(wordImg, Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) #print('Recognized:', '"' + recognized[0] + '"') #print('Probability:', probability[0]) return recognized[0]
def infer(model, fnImg): "recognize text in image provided by file path" img = cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE) ret, img = cv2.threshold(img, 155, 255, cv2.THRESH_BINARY) img = preprocess(img, Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print('Recognized:', '"' + recognized[0] + '"') print('Probability:', probability[0])
def infer(model, fnImg): "recognize text in image provided by file path" import pdb pdb.set_trace() img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') print(f'Probability: {probability[0]}')
def predict(): if request.method == 'POST': img = np.array( Image.open(io.BytesIO( request.files['image'].read())).convert('L')) img = preprocess(img, Model.imgSize, True) batch = Batch(None, [img]) recognized, probability = model.inferBatch(batch, True) return 'Prediction: {}'.format(recognized[0]) return 'Please upload an image'
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img] * Model.batchSize) # fill all batch elements with same input image recognized = model.inferBatch(batch) # recognize text print('Recognized:', '"' + recognized[0] + '"') # all batch elements hold same result f = open('../dataset/recognized/recognized.txt', 'a+') f.write(recognized[0]+"\n") f.close() wordImg = cv2.imread('../dataset/test.png')
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print(f'Recognized: "{recognized[0]}"') print(f'Probability: {probability[0]}') apex = open("D:/SimpleHTR/data/output.txt", "a") apex.write(recognized[0] + " ") apex.close()
def infer(model, fnImg): "recognize text in image provided by file path" input_path = glob.glob(fnImg) for file in input_path: img = preprocess(cv2.imread(file, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print('Image: ',str(os.path.basename(file))) print('Recognized:', '"' + recognized[0] + '"') print('Probability:', probability[0])
def infer(model, fnImg): "recognize text in image provided by file path" #image = cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE) # cv2.cvtColor(fnImg, cv2.COLOR_BGR2GRAY) # image = cv2.resize(image,(500,500)) img = preprocess(fnImg, Model.imgSize) batch = Batch(None, [img] * Model.batchSize) recognized = model.inferBatch(batch) print('Recognized:', '"' + recognized[0] + '"') return recognized[0]