|
|
import json |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from pathlib import Path |
|
|
|
|
|
def visualize_footprint_json(json_path, output_dir): |
|
|
"""可视化足印JSON数据""" |
|
|
|
|
|
with open(json_path, 'r') as f: |
|
|
data = json.load(f) |
|
|
|
|
|
|
|
|
output_dir = Path(output_dir) |
|
|
output_dir.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
max_x = max_y = 0 |
|
|
for frame in data['frames']: |
|
|
for footprint in frame['footprints']: |
|
|
pos = footprint['position'] |
|
|
max_x = max(max_x, pos['x'] + pos['width']) |
|
|
max_y = max(max_y, pos['y'] + pos['height']) |
|
|
|
|
|
for area in data['footprintArea']: |
|
|
pos = area['areaPosition'] |
|
|
max_x = max(max_x, pos['x'] + pos['width']) |
|
|
max_y = max(max_y, pos['y'] + pos['height']) |
|
|
|
|
|
|
|
|
canvas_width = max_x + 50 |
|
|
canvas_height = max_y + 50 |
|
|
|
|
|
|
|
|
color_map = { |
|
|
'LF': (0, 255, 0), |
|
|
'RF': (0, 255, 255), |
|
|
'LH': (255, 0, 255), |
|
|
'RH': (0, 165, 255), |
|
|
'unknown': (128, 128, 128) |
|
|
} |
|
|
|
|
|
|
|
|
area_canvas = np.ones((canvas_height, canvas_width, 3), dtype=np.uint8) * 255 |
|
|
for area in data['footprintArea']: |
|
|
pos = area['areaPosition'] |
|
|
color = color_map[area['type']] |
|
|
|
|
|
|
|
|
cv2.rectangle(area_canvas, |
|
|
(pos['x'], pos['y']), |
|
|
(pos['x'] + pos['width'], pos['y'] + pos['height']), |
|
|
color, 2) |
|
|
|
|
|
|
|
|
label = f"{area['type']}#{area['footprintAreaId'].split('_')[-1]}" |
|
|
cv2.putText(area_canvas, label, |
|
|
(pos['x'], pos['y'] - 5), |
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) |
|
|
|
|
|
|
|
|
cv2.imwrite(str(output_dir / 'footprint_areas.png'), area_canvas) |
|
|
|
|
|
|
|
|
frame_ids = sorted(list(set(frame['frameId'] for frame in data['frames']))) |
|
|
for frame_id in frame_ids: |
|
|
frame_canvas = np.ones((canvas_height, canvas_width, 3), dtype=np.uint8) * 255 |
|
|
|
|
|
|
|
|
overlay = frame_canvas.copy() |
|
|
for area in data['footprintArea']: |
|
|
if area['startFrame'] <= frame_id <= area['endFrame']: |
|
|
pos = area['areaPosition'] |
|
|
color = color_map[area['type']] |
|
|
cv2.rectangle(overlay, |
|
|
(pos['x'], pos['y']), |
|
|
(pos['x'] + pos['width'], pos['y'] + pos['height']), |
|
|
color, -1) |
|
|
|
|
|
|
|
|
alpha = 0.3 |
|
|
cv2.addWeighted(overlay, alpha, frame_canvas, 1 - alpha, 0, frame_canvas) |
|
|
|
|
|
|
|
|
for frame_data in data['frames']: |
|
|
if frame_data['frameId'] == frame_id: |
|
|
for footprint in frame_data['footprints']: |
|
|
pos = footprint['position'] |
|
|
color = color_map[footprint['type']] |
|
|
|
|
|
|
|
|
cv2.rectangle(frame_canvas, |
|
|
(pos['x'], pos['y']), |
|
|
(pos['x'] + pos['width'], pos['y'] + pos['height']), |
|
|
color, 2) |
|
|
|
|
|
|
|
|
label = f"{footprint['type']}#{footprint['id'].split('_')[-1]}" |
|
|
cv2.putText(frame_canvas, label, |
|
|
(pos['x'], pos['y'] - 5), |
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) |
|
|
|
|
|
|
|
|
cv2.putText(frame_canvas, f"Frame: {frame_id}", |
|
|
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) |
|
|
|
|
|
|
|
|
cv2.imwrite(str(output_dir / f'frame_{frame_id:04d}.png'), frame_canvas) |
|
|
|
|
|
print(f"可视化结果已保存至: {output_dir}") |
|
|
|
|
|
def main(): |
|
|
|
|
|
results_dir = Path("results") |
|
|
latest_dir = max(results_dir.glob("*"), key=lambda p: p.stat().st_mtime) |
|
|
json_path = latest_dir / "data" / "footprint_data.json" |
|
|
|
|
|
|
|
|
output_dir = latest_dir / "visualization" |
|
|
|
|
|
visualize_footprint_json(json_path, output_dir) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |