Label: 51, Position: (1634, 1375), Size: 29 x 26 Label: 52, Position: (1500, 1378), Size: 14 x 49 Label: 53, Position: (1582, 1378), Size: 26 x 24 Label: 54, Position: (1564, 1383), Size: 19 x 30 Label: 55, Position: (1514, 1384), Size: 32 x 29 Label: 56, Position: (1661, 1385), Size: 32 x 30 Label: 57, Position: (1548, 1388), Size: 18 x 26 Label: 58, Position: (1511, 1390), Size: 14 x 31 Label: 59, Position: (1577, 1390), Size: 39 x 22 Label: 60, Position: (1612, 1390), Size: 23 x 25转换成(x, y, w, h)的形式
时间: 2024-03-22 10:38:48 浏览: 20
下面是Python代码实现:
```python
data = [(51, (1634, 1375), (29, 26)),
(52, (1500, 1378), (14, 49)),
(53, (1582, 1378), (26, 24)),
(54, (1564, 1383), (19, 30)),
(55, (1514, 1384), (32, 29)),
(56, (1661, 1385), (32, 30)),
(57, (1548, 1388), (18, 26)),
(58, (1511, 1390), (14, 31)),
(59, (1577, 1390), (39, 22)),
(60, (1612, 1390), (23, 25))]
# 转换格式
data = [(p[0], p[1], s[0], s[1]) for _, p, s in data]
# 写入txt文件
with open("result.txt", "w") as f:
for d in data:
f.write(" ".join(str(x) for x in d) + "\n")
```
同样地,将原始数据中的label丢弃,并将每个元素转换成(x, y, w, h)的形式。然后将结果写入txt文件中。
相关问题
Label: 51, Position: (1634, 1375), Size: 29 x 26 Label: 52, Position: (1500, 1378), Size: 14 x 49 Label: 53, Position: (1582, 1378), Size: 26 x 24 Label: 54, Position: (1564, 1383), Size: 19 x 30 Label: 55, Position: (1514, 1384), Size: 32 x 29 Label: 56, Position: (1661, 1385), Size: 32 x 30 Label: 57, Position: (1548, 1388), Size: 18 x 26 Label: 58, Position: (1511, 1390), Size: 14 x 31 Label: 59, Position: (1577, 1390), Size: 39 x 22 Label: 60, Position: (1612, 1390), Size: 23 x 25转换为(x,y,w,h)的形式
Label 51: (1634, 1375, 29, 26)
Label 52: (1500, 1378, 14, 49)
Label 53: (1582, 1378, 26, 24)
Label 54: (1564, 1383, 19, 30)
Label 55: (1514, 1384, 32, 29)
Label 56: (1661, 1385, 32, 30)
Label 57: (1548, 1388, 18, 26)
Label 58: (1511, 1390, 14, 31)
Label 59: (1577, 1390, 39, 22)
Label 60: (1612, 1390, 23, 25)
转换为(x, y, w, h)形式。
Label: 1, Position: (0, 0), Size: 2212 x 1578 Label: 2, Position: (1596, 1281), Size: 28 x 17 Label: 3, Position: (1578, 1286), Size: 25 x 18 Label: 4, Position: (1619, 1288), Size: 42 x 43 Label: 5, Position: (1534, 1289), Size: 44 x 11给每一行两边加上分号
你可以使用字符串操作和循环来实现这个转换。下面是一个示例代码:
```python
string = "Label: 1, Position: (0, 0), Size: 2212 x 1578\nLabel: 2, Position: (1596, 1281), Size: 28 x 17\nLabel: 3, Position: (1578, 1286), Size: 25 x 18\nLabel: 4, Position: (1619, 1288), Size: 42 x 43\nLabel: 5, Position: (1534, 1289), Size: 44 x 11"
lines = string.split("\n")
new_lines = []
for line in lines:
new_line = f"; {line} ;"
new_lines.append(new_line)
result = "\n".join(new_lines)
print(result)
```
这个代码会输出以下结果:
```
; Label: 1, Position: (0, 0), Size: 2212 x 1578 ;
; Label: 2, Position: (1596, 1281), Size: 28 x 17 ;
; Label: 3, Position: (1578, 1286), Size: 25 x 18 ;
; Label: 4, Position: (1619, 1288), Size: 42 x 43 ;
; Label: 5, Position: (1534, 1289), Size: 44 x 11 ;
```
注意,这个代码假设输入的字符串格式都相同。如果输入的字符串格式不同,可能需要对代码进行相应的修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)