Lidar_Muxa/docker/brain_test.sh

71 lines
2.2 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Проверка топика /flyguard/brain: он публикуется и содержит настоящую картинку.
# Несколько кадров сохраняются в PNG — их же можно показать в презентации.
#
# docker run --rm --network host --shm-size=1g -v <бэги>:/data \
# -v <репо>/docker:/t:ro -v <куда>:/out flyguard bash /t/brain_test.sh /data/<бэг>
set -u
BAG="${1:?укажите путь к бэгу}"
OUT="${OUT:-/out}"
mkdir -p "$OUT"
ros2 launch flyguard detect.launch.py brain_view:=true > /tmp/node.log 2>&1 &
LAUNCH_PID=$!
for i in $(seq 1 30); do
ros2 topic list 2>/dev/null | grep -q '^/flyguard/brain$' && break
sleep 1
done
echo "топик /flyguard/brain объявлен через ${i} с"
python3 - "$OUT" > /tmp/brain.txt 2>&1 <<'PY' &
import sys, rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
import numpy as np, cv2
out = sys.argv[1]
class Grab(Node):
def __init__(self):
super().__init__("brain_grab")
self.n = 0
self.shapes = set()
self.create_subscription(Image, "/flyguard/brain", self.cb, 10)
def cb(self, m):
self.n += 1
a = np.frombuffer(m.data, np.uint8).reshape(m.height, m.width, -1)
self.shapes.add((m.height, m.width, m.encoding, a.shape[2]))
# берём каждый двадцатый кадр, чтобы картинки отличались
if self.n % 20 == 1 and self.n < 200:
img = a if m.encoding == "bgr8" else cv2.cvtColor(a, cv2.COLOR_RGB2BGR)
cv2.imwrite(f"{out}/brain_{self.n:04d}.png", img)
rclpy.init()
g = Grab()
try:
rclpy.spin(g)
except KeyboardInterrupt:
pass
finally:
print(f"кадров схемы принято: {g.n}")
for s in g.shapes:
print(f"размер {s[1]}x{s[0]}, кодировка {s[2]}, каналов {s[3]}")
PY
GRAB_PID=$!
sleep 2
ros2 bag play --read-ahead-queue-size 10 "$BAG" > /dev/null 2>&1
sleep 2
kill -INT $GRAB_PID 2>/dev/null || true
wait $GRAB_PID 2>/dev/null || true
kill $LAUNCH_PID 2>/dev/null || true
sleep 1
echo
cat /tmp/brain.txt
echo
ls -la "$OUT" | head -15