| 20 | |
| 21 | |
| 22 | class Labeler: |
| 23 | abs_dir_path = "" |
| 24 | abs_parent_dir_path = "" |
| 25 | abs_result_dir_path = "" |
| 26 | result_dir_name = "result" |
| 27 | abs_fail_dir_path = "" |
| 28 | fail_dir_name = "decodeFail" |
| 29 | |
| 30 | index = 1 |
| 31 | image_size_per_dir = 1000 |
| 32 | |
| 33 | total = 0 |
| 34 | fail = 0 |
| 35 | success = 0 |
| 36 | |
| 37 | def __init__(self): |
| 38 | self.qr_scanner = QR_Extractor() |
| 39 | self.abs_parent_dir_path = os.path.abspath(".") |
| 40 | self.abs_result_dir_path = os.path.join(self.abs_parent_dir_path, self.result_dir_name) |
| 41 | self.abs_fail_dir_path = os.path.join(self.abs_parent_dir_path, self.fail_dir_name) |
| 42 | |
| 43 | print(self.abs_result_dir_path) |
| 44 | self.init_index() |
| 45 | |
| 46 | def read_dir(self, dir_path): |
| 47 | print("select path = " + dir_path) |
| 48 | # read all images |
| 49 | images = os.listdir(dir_path) |
| 50 | self.start_label(os.path.abspath(dir_path), images) |
| 51 | print("识别完成 Total = " + str(self.total) + " 成功识别 = " + str(self.success) + " 失败 = " + str(self.fail)) |
| 52 | |
| 53 | def start_label(self, dir_path, images): |
| 54 | self.abs_dir_path = os.path.abspath(dir_path) |
| 55 | self.total += len(images) |
| 56 | for image_file in images: |
| 57 | # print("start label image = " + image_file) |
| 58 | if not is_image(image_file): |
| 59 | continue |
| 60 | |
| 61 | image_path = os.path.join(dir_path, image_file) |
| 62 | image_name = image_file.split(".")[0] |
| 63 | image_suffix = image_file.split(".")[1] |
| 64 | print("image path = " + image_path) |
| 65 | |
| 66 | image_size = get_image_size(image_path) |
| 67 | print("image size = " + str(image_size)) |
| 68 | |
| 69 | qr_code_results = self.decode_qr_code(image_path) |
| 70 | if qr_code_results is None or len(qr_code_results) <= 0: |
| 71 | print("no qr code") |
| 72 | self.collect_special_image(image_path) |
| 73 | self.fail += 1 |
| 74 | continue |
| 75 | |
| 76 | # 转化为 Yolo 位置 |
| 77 | label_data_list = self.translate_data(image_size, qr_code_results) |
| 78 | |
| 79 | # 保存同名文件 |