Process a batch of images and questions
(model, batch_images, batch_questions, id_list, all_outputs)
| 112 | json.dump(all_outputs, f, indent=2, ensure_ascii=False) |
| 113 | |
| 114 | def process_batch(model, batch_images, batch_questions, id_list, all_outputs): |
| 115 | """Process a batch of images and questions""" |
| 116 | batch_results = model.detect_objects_batch(batch_images, batch_questions) |
| 117 | |
| 118 | for i, result in enumerate(batch_results): |
| 119 | try: |
| 120 | thinking = result["thinking"] |
| 121 | bboxes = result["bboxes"] |
| 122 | # print(result) |
| 123 | if "points" not in result or len(result["points"]) == 0: |
| 124 | points = [[int((bbox[0] + bbox[2]) / 2), int((bbox[1] + bbox[3]) / 2)] for bbox in bboxes] |
| 125 | else: |
| 126 | points = result["points"] |
| 127 | |
| 128 | # visualize_result_with_bboxes_and_points(batch_images[i], bboxes, points, id_list[i]["bbox"], thinking, batch_questions[i]) |
| 129 | |
| 130 | accurate = 0.0 |
| 131 | b_x1, b_y1, b_x2, b_y2 = id_list[i]["bbox"] |
| 132 | for point in points: |
| 133 | if b_x1 <= point[0] <= b_x2 and b_y1 <= point[1] <= b_y2: |
| 134 | accurate = 1.0 |
| 135 | break |
| 136 | |
| 137 | all_outputs.append({ |
| 138 | "image_id": id_list[i]["image_id"], |
| 139 | "ann_id": id_list[i]["ann_id"], |
| 140 | "think": thinking, |
| 141 | "accurate": accurate, |
| 142 | }) |
| 143 | |
| 144 | except Exception as e: |
| 145 | print(f"Error processing result: {e}") |
| 146 | # Add penalty in this situation |
| 147 | all_outputs.append({ |
| 148 | "image_id": id_list[i]["image_id"], |
| 149 | "ann_id": id_list[i]["ann_id"], |
| 150 | "think": "", |
| 151 | "accurate": 0.0, |
| 152 | }) |
| 153 | |
| 154 | if __name__ == "__main__": |
| 155 | main() |
no test coverage detected