tfrecords提取圖片再利用百度ocr識別
項目背景:這段時間一直研究ocr識別。數據是工程圖紙。由於圖片轉化過程中因爲格式問題,些許圖片顯示錯誤,如下圖。
圖片本來是有文字的,這就需要從tfrecords中把錯誤圖片提取出來,然後再刪除掉。
思想
1.把tfrecords中圖片全部提取並保存
2.利用百度ocr接口識別錯誤圖片,輸出錯誤圖片路徑
tfrecords提取圖片
filename_queue = tf.train.string_input_producer(["val_0.tfrecords"]) #讀入流中
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) #返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'images' : tf.FixedLenFeature([], tf.string),
}) #取出包含image和label的feature對象
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [128, 128, 3])
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess: #開始一個會話
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
for i in range(20):
example, l = sess.run([image,label])#在會話中取出image和label
img=Image.fromarray(example, 'RGB')#這裏Image是之前提到的
img.save(cwd+str(i)+'.jpg')#存下圖片
print(example, l)
coord.request_stop()
coord.join(threads)
百度ocr識別錯誤圖片
因爲我的錯誤是“?”錯誤,所以在這“?”個數如果大於等於2個,就把圖片路徑輸出並保存
MyPath = "D:/pid/"#這是讀取的圖片存放的文件夾的路徑,可以改爲要讀取的文件夾
filesoure = MyPath
def baiduduqu(filesoure,filename):
from aip import AipOcr
APP_ID = '20435572'
API_KEY = 'P2oHGGVRGkfAzIPeFTUGtWG2'
SECRET_KEY = 'B0XRzHitx93PO5nwZ4zL64g7ztLzBhqr'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
dakai = open(filename,'rb')
duqu = dakai.read()
message = client.basicGeneral(duqu)
for duqu in message.get('words_result'):
print(duqu.get('words'))
print(duqu.get('words_result'))
if duqu.get('words').count('?')>=1:
with open("C:/Users/hkr/Desktop/index.txt", "a", encoding='utf-8') as f:
f.write(filename)
f.write('\r\n')
f.close()
def filename (fielsoure,filetype):
import os
pathDir = os.listdir(filesoure)
for allDir in pathDir:
child = os.path.join('%s%s' % (filesoure,allDir))
print(child)
baiduduqu(filesoure,child)
def run():
import os
os.chdir(filesoure)
for i in os.listdir(os.getcwd()):
postfix = os.path.splitext(i)[1]
if postfix == '.jpg' or postfix == '.png':
filename(filesoure,postfix)
if __name__ == '__main__':
run()