-
Notifications
You must be signed in to change notification settings - Fork 8
/
PaddleDetection.py
59 lines (52 loc) · 1.87 KB
/
PaddleDetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
@File : PaddleDetection.py
@Version : 1.0
@Author : laugh12321
@Contact : [email protected]
@Date : 2022/11/11 09:24:34
@Desc : PaddleDetection 自动标注
'''
import os
import json
def save_labelme_results(image_list, results, labels, save_dir, threshold=0.5):
import base64
import io
from PIL import Image
def img_to_b64(image):
f = io.BytesIO()
image.save(f, format="PNG")
img_bin = f.getvalue()
return base64.encodebytes(img_bin) if hasattr(base64, "encodebytes") else base64.encodestring(img_bin)
start_idx = 0
for idx, imagePath in enumerate(image_list):
image = Image.open(imagePath)
imageWidth, imageHeight = image.size
fileName, _ = os.path.splitext(os.path.basename(imagePath))
annotion = {
"version": "5.0.5",
"flags": {},
"shapes": [],
"imagePath": imagePath,
"imageData": img_to_b64(image).decode('ascii'),
"imageHeight": imageHeight,
"imageWidth": imageWidth,
}
im_bboxes_num = results['boxes_num'][idx]
if 'boxes' in results:
boxes = results['boxes'][start_idx:start_idx + im_bboxes_num, :].tolist()
annotion["shapes"].extend([{
"label": labels[int(box[0])],
"points": [
[box[2], box[3]],
[box[4], box[5]],
],
"group_id": None,
"shape_type": "rectangle",
"flags": {}
} for box in boxes if ((box[1] > threshold) & (box[0] > -1))])
start_idx += im_bboxes_num
json.dump(
annotion,
open(os.path.join(save_dir, f'{fileName}.json'), "w+", encoding='utf-8'),
indent=4, sort_keys=False, ensure_ascii=False
) # 保存json