-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbelloga-img-preprocessing.py
More file actions
170 lines (130 loc) · 4.83 KB
/
Copy pathbelloga-img-preprocessing.py
File metadata and controls
170 lines (130 loc) · 4.83 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from kafka import KafkaConsumer
from json import loads
from craft_text_detector import Craft
import os
import time
import json
from collections import OrderedDict
from kafka import KafkaProducer
from json import dumps
import time
#generator declare
def boundingbox_json_generator(produce_info_array) :
f = open("./outputs/image_text_detection.txt", 'r')
lines = f.readlines()
bounding_box_info_array = []
for line in lines:
coordinate = line.strip()
coordinate_array =list(map(int, coordinate.split(',')))
x_info = coordinate_array[0::2]
y_info = coordinate_array[1::2]
bounding_box_info = OrderedDict()
bounding_box_info["x"]=x_info
bounding_box_info["y"]=y_info
bounding_box_info_array.append(bounding_box_info)
f.close()
output_json = OrderedDict()
output_json['enterpriseId']= produce_info_array[0]
output_json['rawDataId']= produce_info_array[1]
output_json['fileUrl']= produce_info_array[2]
output_json['projectId'] = produce_info_array[3]
output_json['fileName']= produce_info_array[4]
output_json['dataType']= "OCR"
output_json['boundingBoxInfo']= bounding_box_info_array
output_json = json.dumps(output_json, ensure_ascii=False)
return output_json
#producing boundingbox declare
def produce_boundingbox(output_json):
#producer를 할당한다
producer = KafkaProducer(acks=0, compression_type='gzip', bootstrap_servers=['13.209.250.13:9092'],
value_serializer=lambda x: dumps(x).encode('utf-8'))
#data-preprocessing을 토픽으로 지정하여 데이터를 전송한다
start = time.time()
for i in range(1):
preprocessed_data = json.loads(output_json)
data = preprocessed_data
print("Produced Data")
print(data)
producer.send("ocr-data-preprocessing", value=data)
producer.flush()
#img preprocessing
def image_preprocessing(message):
msg_value = message.value.decode('utf-8')
msg_json = json.loads(msg_value)
# 다운받을 이미지 url
url = msg_json['fileUrl']
# curl 요청
os.system("curl " + url + " > labelingTarget.jpg")
#input json을 받아서 각 변수에 담아준다
enterpriseId = msg_json['enterpriseId']
rawDataId = msg_json['rawDataId']
projectId = msg_json['projectId']
fileName = msg_json['fileName']
imageUrl = url
# set image path and export folder directory
image = './labelingTarget.jpg' # can be filepath, PIL image or numpy array
output_dir = 'outputs/'
# create a craft instance
craft = Craft(output_dir=output_dir, crop_type="poly", cuda=False)
# apply craft text detection and export detected regions to output directory
prediction_result = craft.detect_text(image)
# unload models from ram/gpu
craft.unload_craftnet_model()
craft.unload_refinenet_model()
# import craft functions
from craft_text_detector import (
read_image,
load_craftnet_model,
load_refinenet_model,
get_prediction,
export_detected_regions,
export_extra_results,
empty_cuda_cache
)
# read image
image = read_image(image)
# load models
refine_net = load_refinenet_model(cuda=False)
craft_net = load_craftnet_model(cuda=False)
# perform prediction
prediction_result = get_prediction(
image=image,
craft_net=craft_net,
refine_net=refine_net,
text_threshold=0.7,
link_threshold=0.4,
low_text=0.4,
cuda=False,
long_size=1280
)
# export detected text regions
exported_file_paths = export_detected_regions(
image=image,
regions=prediction_result["boxes"],
output_dir=output_dir,
rectify=True
)
# export heatmap, detection points, box visualization
export_extra_results(
image=image,
regions=prediction_result["boxes"],
heatmaps=prediction_result["heatmaps"],
output_dir=output_dir
)
# unload models from gpu
empty_cuda_cache()
producer_info_array = [enterpriseId, rawDataId, imageUrl, projectId, fileName]
return producer_info_array
# topic, broker list
consumer = KafkaConsumer('raw-data-upload',
bootstrap_servers='13.209.250.13:9092',
enable_auto_commit=True,
auto_offset_reset='earliest')
# consumer list를 가져온다
print('[begin] Preprocessing Start')
for message in consumer:
print("Recived Data")
print("Topic: {}, Partition: {}, Offset: {}, Key: {}, Value: {}".format( message.topic, message.partition, message.offset, message.key, message.value.decode('utf-8')))
produce_info_array = image_preprocessing(message)
output_json = boundingbox_json_generator(produce_info_array)
produce_boundingbox(output_json)