l515_socket.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ## License: Apache 2.0. See LICENSE file in root directory.
  2. ## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
  3. ###############################################
  4. ## Open CV and Numpy integration ##
  5. ###############################################
  6. import pyrealsense2 as rs
  7. import numpy as np
  8. import cv2
  9. import socket
  10. import math
  11. import pickle
  12. import sys
  13. import time
  14. import datetime
  15. max_length = 65000
  16. host = "192.168.50.158"
  17. port = 5000
  18. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  19. pipeline = rs.pipeline()
  20. config = rs.config()
  21. config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
  22. config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
  23. pipeline.start(config)
  24. try:
  25. while True:
  26. frames = pipeline.wait_for_frames()
  27. depth_frame = frames.get_depth_frame()
  28. color_frame = frames.get_color_frame()
  29. if not depth_frame or not color_frame:
  30. continue
  31. depth_image = np.asanyarray(depth_frame.get_data())
  32. color_image = np.asanyarray(color_frame.get_data())
  33. depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
  34. text_depth = "depth value of point is " + str(
  35. np.round(depth_frame.get_distance(320, 240), 4)) + "meter(s)"
  36. color_image = cv2.circle(color_image, (320, 240), 1, (0, 255, 255), 10)
  37. color_image = cv2.putText(color_image, text_depth, (10, 20), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 1,
  38. cv2.LINE_AA)
  39. images = np.hstack((color_image, depth_colormap))
  40. images = cv2.resize(images, (640,320),interpolation=cv2.INTER_AREA)
  41. retval, buffer = cv2.imencode(".jpg", images)
  42. if retval:
  43. # convert to byte array
  44. buffer = buffer.tobytes()
  45. # get size of the frame
  46. buffer_size = len(buffer)
  47. num_of_packs = 1
  48. if buffer_size > max_length:
  49. num_of_packs = math.ceil(buffer_size / max_length)
  50. frame_info = {"packs": num_of_packs}
  51. # send the number of packs to be expected
  52. print("Number of packs:", num_of_packs)
  53. sock.sendto(pickle.dumps(frame_info), (host, port))
  54. left = 0
  55. right = max_length
  56. for i in range(num_of_packs):
  57. print("left:", left)
  58. print("right:", right)
  59. # truncate data to send
  60. data = buffer[left:right]
  61. left = right
  62. right += max_length
  63. # send the frames accordingly
  64. sock.sendto(data, (host, port))
  65. finally:
  66. pipeline.stop()