赞
踩
通过迭代方式保存图片的时候,通过imwrite可以很好的将不同命名的图片保存在文件中,但是我今天遇到一个很奇葩的问题,图片全部保存下来了,但是每张图片的中文命名出现了乱码,如下图:
原部分代码如下:
ef cnn_predict(cnn, Lic_img): characters = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] Lic_pred = [] for lic in Lic_img: lic_pred = cnn.predict(lic.reshape(1, 80, 240, 3)) # 预测形状应为(1,80,240,3) lic_pred = np.array(lic_pred).reshape(7, 65) # 列表转为ndarray,形状为(7,65) if len(lic_pred[lic_pred >= 0.8]) >= 4: # 统计其中预测概率值大于80%以上的个数,大于等于4个以上认为识别率高,识别成功 chars = '' for arg in np.argmax(lic_pred, axis=1): # 取每行中概率值最大的arg,将其转为字符 chars += characters[arg] chars = chars[0:2] + '·' + chars[2:] f = open('test.txt', 'a+') if chars: print(chars) f.write('%s %s \n' % (path,chars)) else: print('Unable to recognize') f.write('Unable to recognize_%s\n' % (path)) Lic_pred.append((lic, chars)) # 将车牌和识别结果一并存入Lic_pred save_path = 'F:/Dataset/car/' cv2.imwrite(save_path+'{}.jpg'.format(chars),lic) os.makedirs(os.path.dirname(save_path),exist_ok=True) if Lic_pred: print('success') else: a = '未能识别' f.write('%s %s \n' % (path, a)) return Lic_pred
在代码中是没有问题的,通过print也能够正确的输出,经查阅资料,发现是乱码的原因,也找到了解决办法:将源代码的
cv2.imwrite(save_path+’{}.jpg’.format(chars),lic)
改为
cv2.imencode(’.jpg’, lic)[1].tofile(save_path+chars+’.jpg’)
然后再运行
就可以成功解决,结果图如下
注意:
#读取中文路径
def cv_imread(file_paht):
cv_img=cv2.imdecode(np.fromfile(file_paht,dtype=np.uint8),-1)
return cv_img
#保存中文路径
def cv_imwrite(savePath,tem):
cv2.imencode(’.jpg’,tem)[1].tofile(savePath) # 保存图片
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。