|
| 1 | +import os |
| 2 | +from collections import defaultdict |
| 3 | +from ctypes import c_uint64 |
| 4 | +from multiprocessing import Manager |
| 5 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | + |
| 11 | +__dir__ = os.path.dirname(os.path.abspath(__file__)) |
| 12 | +sys.path.insert(0, os.path.abspath(os.path.join(__dir__, "../../"))) |
| 13 | + |
| 14 | +from pipeline.data_process.utils import cv_utils |
| 15 | +from pipeline.tasks import TaskType |
| 16 | +from pipeline.utils import log, safe_list_writer, visual_utils |
| 17 | +from pipeline.datatype import ProcessData, ProfilingData, StopData |
| 18 | +from pipeline.framework.module_base import ModuleBase |
| 19 | + |
| 20 | +RESULTS_SAVE_FILENAME = { |
| 21 | + TaskType.DET: "det_results.txt", |
| 22 | + TaskType.CLS: "cls_results.txt", |
| 23 | + TaskType.REC: "rec_results.txt", |
| 24 | + TaskType.DET_REC: "pipeline_results.txt", |
| 25 | + TaskType.DET_CLS_REC: "pipeline_results.txt", |
| 26 | + TaskType.LAYOUT: "layout_results.txt", |
| 27 | + TaskType.LAYOUT_DET_REC: "pipeline_results.txt", |
| 28 | + TaskType.LAYOUT_DET_CLS_REC: "pipeline_results.txt", |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +class CollectNode(ModuleBase): |
| 33 | + def __init__(self, args, msg_queue, tqdm_info): |
| 34 | + super().__init__(args, msg_queue, tqdm_info) |
| 35 | + self.image_sub_remaining = defaultdict(defaultdict) |
| 36 | + self.image_pipeline_res = defaultdict(defaultdict) |
| 37 | + self.infer_size = defaultdict(int) |
| 38 | + self.image_total = Manager().Value(c_uint64, 0) |
| 39 | + self.task_type = args.task_type |
| 40 | + self.res_save_dir = args.res_save_dir |
| 41 | + self.save_filename = RESULTS_SAVE_FILENAME[TaskType(self.task_type.value)] |
| 42 | + |
| 43 | + def init_self_args(self): |
| 44 | + super().init_self_args() |
| 45 | + |
| 46 | + def _collect_stop(self, input_data): |
| 47 | + self.image_total.value = input_data.image_total |
| 48 | + |
| 49 | + def _vis_results(self, image_name, image, taskid, data_type, task=None): |
| 50 | + if self.args.crop_save_dir and (data_type == 0 or (data_type == 1 and self.args.input_array_save_dir)): |
| 51 | + basename = os.path.basename(image_name) |
| 52 | + filename = os.path.join(self.args.crop_save_dir, os.path.splitext(basename)[0]) |
| 53 | + box_list = [np.array(x["points"]).reshape(-1, 2) for x in self.image_pipeline_res[taskid][image_name]] |
| 54 | + crop_list = visual_utils.vis_crop(image, box_list) |
| 55 | + for i, crop in enumerate(crop_list): |
| 56 | + cv_utils.img_write(filename + "_crop_" + str(i) + ".jpg", crop) |
| 57 | + |
| 58 | + if self.args.vis_pipeline_save_dir: |
| 59 | + basename = os.path.basename(image_name) |
| 60 | + filename = os.path.join(self.args.vis_pipeline_save_dir, os.path.splitext(basename)[0]) |
| 61 | + box_list = [np.array(x["points"]).reshape(-1, 2) for x in self.image_pipeline_res[taskid][image_name]] |
| 62 | + text_list = [x["transcription"] for x in self.image_pipeline_res[taskid][image_name]] |
| 63 | + box_text = visual_utils.vis_bbox_text(image, box_list, text_list, font_path=self.args.vis_font_path) |
| 64 | + cv_utils.img_write(filename + ".jpg", box_text) |
| 65 | + |
| 66 | + if self.args.vis_det_save_dir and (data_type == 0 or (data_type == 1 and self.args.input_array_save_dir)): |
| 67 | + basename = os.path.basename(image_name) |
| 68 | + filename = os.path.join(self.args.vis_det_save_dir, os.path.splitext(basename)[0]) |
| 69 | + box_list = [np.array(x).reshape(-1, 2) for x in self.image_pipeline_res[taskid][image_name]] |
| 70 | + box_line = visual_utils.vis_bbox(image, box_list, [255, 255, 0], 2) |
| 71 | + cv_utils.img_write(filename + ".jpg", box_line) |
| 72 | + |
| 73 | + if self.args.vis_layout_save_dir and (data_type == 0 or (data_type == 1 and self.args.input_array_save_dir)): |
| 74 | + basename = os.path.basename(image_name) |
| 75 | + filename = os.path.join(self.args.vis_layout_save_dir, os.path.splitext(basename)[0]) |
| 76 | + box_list = [] |
| 77 | + for x in self.image_pipeline_res[taskid][image_name]: |
| 78 | + x, y, dx, dy = x['bbox'] |
| 79 | + box_list.append(np.array([[x, y+dy], [x+dx, y+dy], [x+dx, y], [x, y]])) |
| 80 | + box_line = visual_utils.vis_bbox(image, box_list, [255, 255, 0], 2) |
| 81 | + cv_utils.img_write(filename + ".jpg", box_line) |
| 82 | + # log.info(f"{image_name} is finished.") |
| 83 | + |
| 84 | + def final_text_save(self): |
| 85 | + rst_dict = dict() |
| 86 | + for rst in self.image_pipeline_res.values(): |
| 87 | + rst_dict.update(rst) |
| 88 | + save_filename = os.path.join(self.res_save_dir, self.save_filename) |
| 89 | + safe_list_writer(rst_dict, save_filename) |
| 90 | + # log.info(f"save infer result to {save_filename} successfully") |
| 91 | + |
| 92 | + def _update_layout_result(self, input_data): |
| 93 | + taskid = input_data.taskid |
| 94 | + image_path = input_data.image_path[0] |
| 95 | + layout_rsts = input_data.data |
| 96 | + |
| 97 | + for layout_rst in layout_rsts["layout_collect_res"]: |
| 98 | + # X, Y = layout_rst.data["raw_img_shape"] |
| 99 | + layout_bbox = layout_rst.data["layout_result"] |
| 100 | + lx, ly, _, _ = layout_bbox['bbox'] |
| 101 | + for rec_rst in layout_rst.infer_result: |
| 102 | + bbox, transcription, score = rec_rst[:-2], rec_rst[-2], rec_rst[-1] |
| 103 | + bbox = [[b[0]+lx, b[1]+ly] for b in bbox] |
| 104 | + if score > 0.5: |
| 105 | + if self.args.result_contain_score: |
| 106 | + self.image_pipeline_res[taskid][image_path].append( |
| 107 | + {"transcription": transcription, "points": bbox, "score": str(score)} |
| 108 | + ) |
| 109 | + else: |
| 110 | + self.image_pipeline_res[taskid][image_path].append( |
| 111 | + {"transcription": transcription, "points": bbox} |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | + def _collect_results(self, input_data: ProcessData): |
| 116 | + taskid = input_data.taskid |
| 117 | + if self.task_type.value in (TaskType.DET_REC.value, TaskType.DET_CLS_REC.value): |
| 118 | + image_path = input_data.image_path[0] # bs=1 |
| 119 | + # print(f"input_data.infer_result:{input_data.infer_result}") |
| 120 | + for result in input_data.infer_result: |
| 121 | + # print(f"result:{result}") |
| 122 | + if result[-1] > 0.5: |
| 123 | + if self.args.result_contain_score: |
| 124 | + self.image_pipeline_res[taskid][image_path].append( |
| 125 | + {"transcription": result[-2], "points": result[:-2], "score": str(result[-1])} |
| 126 | + ) |
| 127 | + else: |
| 128 | + self.image_pipeline_res[taskid][image_path].append( |
| 129 | + {"transcription": result[-2], "points": result[:-2]} |
| 130 | + ) |
| 131 | + if not input_data.infer_result: |
| 132 | + self.image_pipeline_res[taskid][image_path] = [] |
| 133 | + elif self.task_type.value == TaskType.DET.value: |
| 134 | + image_path = input_data.image_path[0] # bs=1 |
| 135 | + self.image_pipeline_res[taskid][image_path] = input_data.infer_result |
| 136 | + elif self.task_type.value in (TaskType.REC.value, TaskType.CLS.value): |
| 137 | + for image_path, infer_result in zip(input_data.image_path, input_data.infer_result): |
| 138 | + self.image_pipeline_res[taskid][image_path] = infer_result |
| 139 | + elif self.task_type.value == TaskType.LAYOUT.value: |
| 140 | + for infer_result in input_data.infer_result: |
| 141 | + image_path = infer_result.pop("image_id")[0] |
| 142 | + if image_path in self.image_pipeline_res[taskid]: |
| 143 | + self.image_pipeline_res[taskid][image_path].append(infer_result) |
| 144 | + else: |
| 145 | + self.image_pipeline_res[taskid][image_path] = [infer_result] |
| 146 | + elif self.task_type.value in (TaskType.LAYOUT_DET_REC.value, TaskType.LAYOUT_DET_CLS_REC.value,): |
| 147 | + self._update_layout_result(input_data) |
| 148 | + else: |
| 149 | + raise NotImplementedError("Task type do not support.") |
| 150 | + |
| 151 | + self._update_remaining(input_data) |
| 152 | + |
| 153 | + def _update_remaining(self, input_data: ProcessData): |
| 154 | + taskid = input_data.taskid |
| 155 | + data_type = input_data.data_type |
| 156 | + # if self.task_type.value in (TaskType.DET_REC.value, TaskType.DET_CLS_REC.value, TaskType.LAYOUT_DET_REC.value): # with sub image |
| 157 | + # for idx, image_path in enumerate(input_data.image_path): |
| 158 | + # if image_path in self.image_sub_remaining[taskid]: |
| 159 | + # self.image_sub_remaining[taskid][image_path] -= input_data.sub_image_size |
| 160 | + # if not self.image_sub_remaining[taskid][image_path]: |
| 161 | + # self.image_sub_remaining[taskid].pop(image_path) |
| 162 | + # self.infer_size[taskid] += 1 |
| 163 | + # if self.task_type.value in (TaskType.LAYOUT_DET_REC.value, ): |
| 164 | + # self._vis_results(image_path, input_data.data["layout_images"][idx], taskid, data_type) if input_data.frame else ... |
| 165 | + # else: |
| 166 | + # self._vis_results( |
| 167 | + # image_path, input_data.frame[idx], taskid, data_type |
| 168 | + # ) if input_data.frame else ... |
| 169 | + # else: |
| 170 | + # remaining = input_data.sub_image_total - input_data.sub_image_size |
| 171 | + # if remaining: |
| 172 | + # self.image_sub_remaining[taskid][image_path] = remaining |
| 173 | + # else: |
| 174 | + # self.infer_size[taskid] += 1 |
| 175 | + # if self.task_type.value in (TaskType.LAYOUT_DET_REC.value, ): |
| 176 | + # self._vis_results(image_path, input_data.data["layout_images"][idx], taskid, data_type) if input_data.frame else ... |
| 177 | + # else: |
| 178 | + # self._vis_results( |
| 179 | + # image_path, input_data.frame[idx], taskid, data_type |
| 180 | + # ) if input_data.frame else ... |
| 181 | + # else: # without sub image |
| 182 | + # if self.task_type.value not in (TaskType.LAYOUT_DET_REC, ): |
| 183 | + for idx, image_path in enumerate(input_data.image_path): |
| 184 | + self.infer_size[taskid] += 1 |
| 185 | + if self.task_type.value in (TaskType.LAYOUT_DET_REC.value, ): |
| 186 | + self._vis_results(image_path, input_data.frame[idx], taskid, data_type) if input_data.frame else ... |
| 187 | + else: |
| 188 | + self._vis_results(image_path, input_data.frame[idx], taskid, data_type) if input_data.frame else ... |
| 189 | + |
| 190 | + |
| 191 | + def process(self, input_data): |
| 192 | + if isinstance(input_data, ProcessData): |
| 193 | + # print(f"ProcessData:{input_data.image_path}") |
| 194 | + taskid = input_data.taskid |
| 195 | + if input_data.taskid not in self.image_sub_remaining.keys(): |
| 196 | + self.image_sub_remaining[input_data.taskid] = defaultdict(int) |
| 197 | + if input_data.taskid not in self.image_pipeline_res.keys(): |
| 198 | + self.image_pipeline_res[input_data.taskid] = defaultdict(list) |
| 199 | + self._collect_results(input_data) |
| 200 | + if self.infer_size[taskid] == input_data.task_images_num: |
| 201 | + self.send_to_next_module({taskid: self.image_pipeline_res[taskid]}) |
| 202 | + |
| 203 | + elif isinstance(input_data, StopData): |
| 204 | + self._collect_stop(input_data) |
| 205 | + if input_data.exception: |
| 206 | + self.stop_manager.value = True |
| 207 | + else: |
| 208 | + raise ValueError("unknown input data") |
| 209 | + |
| 210 | + infer_size_sum = sum(self.infer_size.values()) |
| 211 | + if self.image_total.value and infer_size_sum == self.image_total.value: |
| 212 | + self.final_text_save() |
| 213 | + self.stop_manager.value = True |
| 214 | + |
| 215 | + def stop(self): |
| 216 | + profiling_data = ProfilingData( |
| 217 | + module_name=self.module_name, |
| 218 | + instance_id=self.instance_id, |
| 219 | + process_cost_time=self.process_cost.value, |
| 220 | + send_cost_time=self.send_cost.value, |
| 221 | + image_total=self.image_total.value, |
| 222 | + ) |
| 223 | + self.msg_queue.put(profiling_data, block=False) |
| 224 | + self.is_stop = True |
0 commit comments