-
Notifications
You must be signed in to change notification settings - Fork 16
/
read_img.py
85 lines (72 loc) · 2.23 KB
/
read_img.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
"""
@author: wenlihaoyu
"""
import json
import base64
from PIL import Image
import cv2
import uuid
import os
def read_image_label(path):
"""
read image and label
"""
imageString = read_img(path)
basename = os.path.basename(path)
txtpath = path.replace(basename,basename.split('.')[0]+'.txt')
label = get_label(txtpath)
return {'url':imageString,'label':label,'path':path}
def read_img(path,flag=True):
try:
f=open(path,'rb') #二进制方式打开图文件
ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
f.close()
return ls_f.decode('utf-8')
except:
return ''
def get_label(filetxt):
"""
获取文本
"""
try:
with open(filetxt) as f:
S = f.read()
except:
S=''
return S.strip()
def set_image_status(path,statusDict,status=1):
"""
1:纠偏中
2:纠偏完成
0:未纠偏
"""
statusDict[path] =status
def correct_image(data,correctpath,statusDict,status=2):
"""
写入完成纠偏的数据
"""
if data!=[]:
for i,lst in enumerate(data):
imagepath = lst['path'].strip()
label = lst['text'].strip()
flag = lst['flag']##是否删除图像及标签
path,filename =imagepath.split('/')[-2:]
root = os.path.join(correctpath,path)
if not os.path.exists(root):
os.makedirs(root)
img = Image.open(imagepath)
imgP =os.path.join(root,filename)
##写入图像
if not flag and label!='###':
img.save(imgP)
#cv2.imwrite(e,img)
##写入纠错文本
basename = os.path.basename(imgP)
txtpath = imgP.replace(basename,basename.split('.')[0]+'.txt')
with open(txtpath,'w') as f:
f.write(label)
set_image_status(imagepath,statusDict,status=status)
os.remove(imagepath)
if os.path.exists(imagepath.replace('.jpg','.txt')):
os.remove(imagepath.replace('.jpg','.txt'))