Skip to content

Commit 7968299

Browse files
committed
Optim code
1 parent 06cfafd commit 7968299

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

label_convert/coco_to_labelImg.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ def convert(self, info_list: List[Path]) -> None:
7878
xywh_str = " ".join([str(v) for v in xywh])
7979
label_str = f"{category_id} {xywh_str}"
8080

81-
# 写入标注的txt文件
8281
txt_full_path = save_dir / f"{Path(img_name).stem}.txt"
8382
self.write_txt(txt_full_path, label_str, mode="a")
8483

85-
# 复制图像到转换后目录
8684
img_full_path = img_dir / img_name
8785
shutil.copy2(img_full_path, save_dir)
8886

label_convert/labelme_to_coco.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -209,41 +209,30 @@ def generate_json(self, img_list, save_dir):
209209
points = np.array(shape.get("points"))
210210

211211
if shape_type == RECTANGLE:
212+
seg_points = [np.ravel(points, order="C").tolist()]
213+
212214
x0, y0 = np.min(points, axis=0)
213215
x1, y1 = np.max(points, axis=0)
214216
w, h = x1 - x1, y1 - y0
217+
bbox_points = [x0, y0, w, h]
215218
area = w * h
216219

217-
seg_points = [np.ravel(points, order="C").tolist()]
218-
219-
one_anno_dict = {
220-
"segmentation": seg_points,
221-
"area": area,
222-
"iscrowd": 0,
223-
"image_id": img_id,
224-
"bbox": [x0, y0, w, h],
225-
"category_id": label_id,
226-
"id": self.object_id,
227-
}
228220
elif shape_type == POLYGON:
229-
mask = np.zeros((img_h, img_w), dtype="uint8")
230-
img_mask = cv2.fillPoly(mask, np.int32([points]), 255)
231-
contours, _ = cv2.findContours(
232-
img_mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE
233-
)
234-
contour = contours[0]
235-
bbox_points = self.get_mini_boxes(contour)
236-
area = cv2.contourArea(contour)
237-
238-
one_anno_dict = {
239-
"segmentation": points.tolist(),
240-
"area": area,
241-
"iscrowd": 0,
242-
"image_id": img_id,
243-
"bbox": bbox_points,
244-
"category_id": label_id,
245-
"id": self.object_id,
246-
}
221+
seg_points = points.tolist()
222+
bbox_points, area = self.cvt_poly_to_rect(img_h, img_w, points)
223+
else:
224+
print(f"Current {shape_type} is not supported!")
225+
continue
226+
227+
one_anno_dict = {
228+
"segmentation": seg_points,
229+
"area": area,
230+
"iscrowd": 0,
231+
"image_id": img_id,
232+
"bbox": bbox_points,
233+
"category_id": label_id,
234+
"id": self.object_id,
235+
}
247236

248237
anno_list.append(one_anno_dict)
249238
self.object_id += 1
@@ -276,10 +265,14 @@ def cp_file(self, file_path: Path, dst_dir: Path):
276265

277266
shutil.copy2(str(file_path), dst_dir)
278267

279-
def convert_polygon_to_rectangle(
280-
self,
281-
):
282-
pass
268+
def cvt_poly_to_rect(self, img_h: int, img_w: int, points):
269+
mask = np.zeros((img_h, img_w), dtype="uint8")
270+
img_mask = cv2.fillPoly(mask, np.int32([points]), 255)
271+
contours, _ = cv2.findContours(img_mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
272+
contour = contours[0]
273+
bbox_points = self.get_mini_boxes(contour)
274+
area = cv2.contourArea(contour)
275+
return bbox_points, area
283276

284277
@staticmethod
285278
def get_mini_boxes(contour) -> List[int]:

tests/test_files/labelme_dataset/4645_8.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)