forked from LZQ-RSer/RS_Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis_txt.py
104 lines (98 loc) · 3.03 KB
/
vis_txt.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
# @Time : 2021/2/7 22:27
# @Author : lzq
# @Site :
# @File : vis_txt.py
# @Software: PyCharm Community Edition
import numpy as np
import os
from PIL import Image,ImageFont, ImageDraw
Image.MAX_IMAGE_PIXELS = None
import glob
import tqdm
import cv2
import pandas as pd
import matplotlib.pyplot as plt
def read_txt(str_text):
"""
自定义解析方法
:param str_text:
:return:
"""
list = []
with open(str_text) as f:
line = f.readline()
while line:
# 消除空行
if line.isspace():
line = f.readline()
continue
#消除换行
if '\n' in line:
line = line.strip("\n")
#消除不需要的行
if line[0] in ["i","g"]:
line = f.readline()
continue
list.append(line)
line = f.readline()
return list
def vis_label(txt,class_name,xz=False):
"""
可视化标签
:param txt: 记录标签的txt每一行格式为:图像路径 宽,高 x1,y1,x2,y2,classname
:param class_name: classname 列表
:param xz: 旋转框或者水平框
:return: image
"""
colors_tableau = [(255, 0, 0), (31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 0, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
line = txt.split(' ')
image_name = line[0]
print(image_name)
## 从2开始,因为前面有图像路径,图像长宽
boxs = line[2:]
thickness = 3
image = Image.open(image_name)
for box in boxs:
# print(box)
bo = box.split(',')
b = bo[0:4]
label = bo[-1]
# score = bo[9]
b=[round(float(x)) for x in b]
p = b
left = min(b[::2])#奇数位置
right = max(b[::2])
top = min(b[1::2])#偶数位置
bottom = max(b[1::2])
draw = ImageDraw.Draw(image)
if xz:
for i in range(thickness):
# n =i if i%2 else -i
draw.polygon([x+i for x in p],outline=colors_tableau[int(class_name.index(label))])
continue
top = top - 5
left = left - 5
bottom = bottom + 5
right = right + 5
for i in range(thickness):
draw.rectangle(
[left + i, top + i, right - i, bottom - i],
outline=colors_tableau[int(class_name.index(label))])
del draw
return image
if __name__=='__main__':
#可视化某个真值txt
class_name = ["aircraft","other"]
outfile = './test/clip.json.txt'
img = './test/clip.tif'
boxs = read_txt(outfile)
image = vis_label(boxs[0],class_name,False)
image.show()
exit()
image.save('./test/clip_vis.png')
# exit()