File size: 2,289 Bytes
117b368
 
88e28b1
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import os
from utils.mama_utils import *


# 将二维数组转化为JSON字符串
def data_to_json(data_matrix):
    results = []
    for row in data_matrix:
        result = {"person_name": row[0],
                  "img_url": row[1],
                  "score": row[2],
                  "conclusion": row[3]
                  }
        results.append(result)
    return json.dumps({"results": results}, ensure_ascii=False, indent=4)


# 读取labeljson返回所需参数
def read_json_label(json_file_path='./data/suanfamama-fashion-guangzhou-dataset/20240722_label/20240722-193429-6.json'):
    # 检查json路径
    if not os.path.exists(json_file_path):
        print(f" {json_file_path}JSON不存在")
        return None

    # 读取JSON数据
    with open(json_file_path, 'r', encoding='utf-8') as file:
        data = json.loads(file.read())

    # 提取图片路径
    image_path = data['imagePath']

    # 初始化标签名及对应边界框坐标列表
    labels_with_boxes = []

    # 遍历所有形状(shapes)以获取标签和坐标
    for shape in data['shapes']:
        label = shape['label']
        xyxy = [shape['points'][0][0], shape['points'][0][1], shape['points'][1][0], shape['points'][1][1]]
        labels_with_boxes.append({'label': label, 'xyxy': xyxy})

    return image_path, labels_with_boxes


# 将字符串转换为json
def text_to_json(text):
    # 将中文字段转为英文
    transform = {'人物类型': 'type',
                 '上衣类型': 'upper_garment',
                 '下衣类型': 'lower_garment',
                 '帽子类型': 'headgear',
                 '袜子类型': 'sock',
                 '鞋子类型': 'shoe',
                 '配饰类型': 'accessory',
                 '背包类型': 'backpack',
                 '动作类型': 'action',
                 '表情类型': 'countenance'
                 }

    lines = text.split('\n')
    data = {}
    for line in lines:
        parts = line.strip().split(':')
        if len(parts) < 2:
            continue
        key = transform.get(parts[0].strip())
        value = parts[1].strip()
        if contains_any_keyword(value, ['未', '无']):
            value = "None"
        if key:
            data[key] = value
    return data