如何在彩色图中绘制序列分割结果?

问题描述 投票:0回答:1

我是序列分割领域的新手。我想做的是这样的: 给定一个时间序列(即视频),我沿时间轴为该视频的每 1 秒分配一个标签。我可以有两个清单:

(1)  GroundTruth_List = ['sit', 'sit', 'sit', 'order_coco', 'order_coco', 'drink_coco']
(2)  Predicted_List = ['sit', 'sit', 'sit', 'order_coco', 'order_coco', 'drink_milk']

我希望粗略地直观地看到这段视频有多少错误的预测。 在网上搜索后,我发现也许 matplotlib 可能是正确的方向。 有人可以提供一些关于如何可视化上述两个列表的示例代码吗?例如,两条水平线代表两个列表,如果段类是“坐”,我们将其绘制为蓝色,但对于其他类,我们会自动将线绘制为不同的颜色。

python matplotlib visualization video-processing
1个回答
1
投票

我会根据你的问题所理解的来回答

要可视化这两个列表,您可以使用 matplotlib 创建水平条形图。每个段将表示为彩色条,其中颜色表示类别标签。下面是实现此目的的示例代码:

import matplotlib.pyplot as plt

# Given lists
GroundTruth_List = ['sit', 'sit', 'sit', 'order_coco', 'order_coco', 'drink_coco']
Predicted_List = ['sit', 'sit', 'sit', 'order_coco', 'order_coco', 'drink_milk']

# Prepare data for plotting
labels = set(GroundTruth_List + Predicted_List)
label_colors = plt.cm.tab20.colors[:len(labels)]  # Get a set of distinct colors

# Function to plot a segment as a colored bar
def plot_segment(start, end, label, color):
    plt.barh(0, end - start, left=start, height=0.5, color=color, alpha=0.7, label=label)

# Plot the GroundTruth_List
for i in range(len(GroundTruth_List)):
    start = i
    end = i + 1
    label = GroundTruth_List[i]
    color = label_colors[list(labels).index(label)]
    plot_segment(start, end, label, color)

# Plot the Predicted_List
for i in range(len(Predicted_List)):
    start = i
    end = i + 1
    label = Predicted_List[i]
    color = label_colors[list(labels).index(label)]
    plot_segment(start, end, label, color)

# Add legend and labels
plt.legend(loc='upper right')
plt.yticks([])
plt.xlabel('Time (seconds)')
plt.title('Sequence Segmentation Visualization')

plt.show()

已编辑

import matplotlib.pyplot as plt

# Given lists
GroundTruth_List = ['sit', 'sit', 'sit', 'order_coco', 'order_coco', 'drink_coco']
Predicted_List = ['sit', 'sit', 'sit', 'order_coco', 'order_coco', 'drink_milk']

# Prepare data for plotting
labels = set(GroundTruth_List + Predicted_List)
label_colors = plt.cm.tab20.colors[:len(labels)]  # Get a set of distinct colors

# Function to plot a segment as a colored bar
def plot_segment(start, end, label, color):
    plt.barh(0, end - start, left=start, height=0.5, color=color, alpha=0.7, label=label)

# Plot the GroundTruth_List
for i in range(len(GroundTruth_List)):
    start = i
    end = i + 1
    label = GroundTruth_List[i]
    color = label_colors[list(labels).index(label)]
    plot_segment(start, end, label, color)

# Plot the Predicted_List
for i in range(len(Predicted_List)):
    start = i
    end = i + 1
    label = Predicted_List[i]
    color = label_colors[list(labels).index(label)]
    plot_segment(start, end, label, color)

# Add legend and labels
# Combine GroundTruth and Predicted labels and remove duplicates
combined_labels = list(set(GroundTruth_List + Predicted_List))
plt.legend(loc='upper right', labels=combined_labels)

# Set the y-axis tick labels to empty to hide them
plt.yticks([])

# Set x-axis labels based on time segments
plt.xticks(range(len(GroundTruth_List) + 1))

# Set the center of the y-axis as 0.5 and add a small buffer for visualization
plt.ylim(-0.5, 0.5)

# Add gridlines for better visualization
plt.grid(axis='x', linestyle='--', alpha=0.7)

plt.xlabel('Time (seconds)')
plt.title('Sequence Segmentation Visualization')

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.