|
| 1 | +import socket |
| 2 | +import rospy |
| 3 | +import numpy as np |
| 4 | +import libh264decoder |
| 5 | +from sensor_msgs.msg import Image |
| 6 | +import pdb |
| 7 | +import threading |
| 8 | + |
| 9 | + |
| 10 | +class Video_Feed: |
| 11 | + def __init__(self): |
| 12 | + |
| 13 | + self.message_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 14 | + self.video_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 15 | + |
| 16 | + self.message_address = ("192.168.10.1", 8899) |
| 17 | + self.video_port = 11111 |
| 18 | + self.decoder = libh264decoder.H264Decoder() |
| 19 | + |
| 20 | + # "" represents INADDR_ANY and is used to bind to all interfaces |
| 21 | + rospy.init_node("raw_video", anonymous=True) |
| 22 | + self.publisher = rospy.Publisher("raw_video", Image, queue_size=1) |
| 23 | + |
| 24 | + self.message_socket.sendto(b"command", self.message_address) |
| 25 | + print("sent: command") |
| 26 | + self.message_socket.sendto(b"streamon", self.message_address) |
| 27 | + print("sent: streamon") |
| 28 | + |
| 29 | + self.video_socket.bind(("", self.video_port)) |
| 30 | + |
| 31 | + recieve_thread = threading.Thread(target=self.recieve) |
| 32 | + recieve_thread.daemon = True |
| 33 | + recieve_thread.start() |
| 34 | + |
| 35 | + # pdb.set_trace() |
| 36 | + |
| 37 | + def recieve(self): |
| 38 | + packet_data = "" |
| 39 | + while True: |
| 40 | + try: |
| 41 | + res_string, ip = self.video_socket.recvfrom(2048) |
| 42 | + pdb.set_trace() |
| 43 | + packet_data += res_string |
| 44 | + if len(res_string) != 1460: |
| 45 | + for frame in self._h264_decode(packet_data): |
| 46 | + # pdb.set_trace() |
| 47 | + rospy.loginfo("published frame") |
| 48 | + self.publisher.publish(frame) |
| 49 | + |
| 50 | + packet_data = "" |
| 51 | + except socket.error as exc: |
| 52 | + print("Cautch exception TypeError {}".format(exc)) |
| 53 | + |
| 54 | + def _h264_decode(self, packet_data): |
| 55 | + pdb.set_trace() |
| 56 | + """ |
| 57 | + decode raw h264 format data from Tello |
| 58 | +
|
| 59 | + :param packet_data: raw h264 data array |
| 60 | +
|
| 61 | + :return: a list of decoded frame |
| 62 | + """ |
| 63 | + res_frame_list = [] |
| 64 | + frames = self.decoder.decode(packet_data) |
| 65 | + for framedata in frames: |
| 66 | + (frame, w, h, ls) = framedata |
| 67 | + if frame is not None: |
| 68 | + print('frame size %i bytes, w %i, h %i, linesize %i' % (len(frame), w, h, ls)) |
| 69 | + |
| 70 | + frame = np.fromstring(frame, dtype=np.ubyte, count=len(frame), sep='') |
| 71 | + frame = (frame.reshape((h, ls / 3, 3))) |
| 72 | + frame = frame[:, :w, :] |
| 73 | + res_frame_list.append(frame) |
| 74 | + |
| 75 | + return res_frame_list |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + """ Initialises and cleansup after ros node""" |
| 80 | + feed = Video_Feed() |
| 81 | + try: |
| 82 | + feed.recieve() |
| 83 | + rospy.spin() |
| 84 | + except KeyboardInterrupt: |
| 85 | + rospy.signal_shutdown("down we go") |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + main() |
0 commit comments