|
27 | 27 | from python.preprocess import create_operators
|
28 | 28 | from python.postprocess import build_postprocess
|
29 | 29 |
|
30 |
| - |
31 | 30 | class ClsPredictor(Predictor):
|
32 | 31 | def __init__(self, config):
|
33 | 32 | super().__init__(config["Global"])
|
@@ -59,21 +58,47 @@ def predict(self, images):
|
59 | 58 | input_tensor.copy_from_cpu(image)
|
60 | 59 | self.paddle_predictor.run()
|
61 | 60 | batch_output = output_tensor.copy_to_cpu()
|
| 61 | + if self.postprocess is not None: |
| 62 | + batch_output = self.postprocess(batch_output) |
62 | 63 | return batch_output
|
63 | 64 |
|
64 | 65 |
|
65 | 66 | def main(config):
|
66 | 67 | cls_predictor = ClsPredictor(config)
|
67 | 68 | image_list = get_image_list(config["Global"]["infer_imgs"])
|
68 | 69 |
|
69 |
| - assert config["Global"]["batch_size"] == 1 |
70 |
| - for idx, image_file in enumerate(image_list): |
71 |
| - img = cv2.imread(image_file)[:, :, ::-1] |
72 |
| - output = cls_predictor.predict(img) |
73 |
| - output = cls_predictor.postprocess(output, [image_file]) |
74 |
| - print(output) |
75 |
| - return |
| 70 | + batch_imgs = [] |
| 71 | + batch_names = [] |
| 72 | + cnt = 0 |
| 73 | + for idx, img_path in enumerate(image_list): |
| 74 | + img = cv2.imread(img_path) |
| 75 | + if img is None: |
| 76 | + logger.warning( |
| 77 | + "Image file failed to read and has been skipped. The path: {}". |
| 78 | + format(img_path)) |
| 79 | + else: |
| 80 | + img = img[:, :, ::-1] |
| 81 | + batch_imgs.append(img) |
| 82 | + img_name = os.path.basename(img_path) |
| 83 | + batch_names.append(img_name) |
| 84 | + cnt += 1 |
76 | 85 |
|
| 86 | + if cnt % config["Global"]["batch_size"] == 0 or (idx + 1) == len(image_list): |
| 87 | + if len(batch_imgs) == 0: |
| 88 | + continue |
| 89 | + |
| 90 | + batch_results = cls_predictor.predict(batch_imgs) |
| 91 | + for number, result_dict in enumerate(batch_results): |
| 92 | + filename = batch_names[number] |
| 93 | + clas_ids = result_dict["class_ids"] |
| 94 | + scores_str = "[{}]".format(", ".join("{:.2f}".format( |
| 95 | + r) for r in result_dict["scores"])) |
| 96 | + label_names = result_dict["label_names"] |
| 97 | + print("{}:\tclass id(s): {}, score(s): {}, label_name(s): {}". |
| 98 | + format(filename, clas_ids, scores_str, label_names)) |
| 99 | + batch_imgs = [] |
| 100 | + batch_names = [] |
| 101 | + return |
77 | 102 |
|
78 | 103 | if __name__ == "__main__":
|
79 | 104 | args = config.parse_args()
|
|
0 commit comments