network_gui.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import torch
  2. import traceback
  3. import socket
  4. import json
  5. from scene.cameras import MiniCam
  6. host = "127.0.0.1"
  7. port = 6009
  8. conn = None
  9. addr = None
  10. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. def init(wish_host, wish_port):
  12. global host, port, listener
  13. host = wish_host
  14. port = wish_port
  15. listener.bind((host, port))
  16. listener.listen()
  17. listener.settimeout(0)
  18. def try_connect():
  19. global conn, addr, listener
  20. try:
  21. conn, addr = listener.accept()
  22. print(f"\nConnected by {addr}")
  23. conn.settimeout(None)
  24. except Exception as inst:
  25. pass
  26. def read():
  27. global conn
  28. messageLength = conn.recv(4)
  29. messageLength = int.from_bytes(messageLength, 'little')
  30. message = conn.recv(messageLength)
  31. return json.loads(message.decode("utf-8"))
  32. def send(message_bytes, verify):
  33. global conn
  34. if message_bytes != None:
  35. conn.sendall(message_bytes)
  36. conn.sendall(len(verify).to_bytes(4, 'little'))
  37. conn.sendall(bytes(verify, 'ascii'))
  38. def receive():
  39. message = read()
  40. width = message["resolution_x"]
  41. height = message["resolution_y"]
  42. if width != 0 and height != 0:
  43. try:
  44. do_training = bool(message["train"])
  45. fovy = message["fov_y"]
  46. fovx = message["fov_x"]
  47. znear = message["z_near"]
  48. zfar = message["z_far"]
  49. do_shs_python = bool(message["shs_python"])
  50. do_rot_scale_python = bool(message["rot_scale_python"])
  51. keep_alive = bool(message["keep_alive"])
  52. scaling_modifier = message["scaling_modifier"]
  53. world_view_transform = torch.reshape(torch.tensor(message["view_matrix"]), (4, 4)).cuda()
  54. world_view_transform[:,1] = -world_view_transform[:,1]
  55. world_view_transform[:,2] = -world_view_transform[:,2]
  56. full_proj_transform = torch.reshape(torch.tensor(message["view_projection_matrix"]), (4, 4)).cuda()
  57. full_proj_transform[:,1] = -full_proj_transform[:,1]
  58. custom_cam = MiniCam(width, height, fovy, fovx, znear, zfar, world_view_transform, full_proj_transform)
  59. except Exception as e:
  60. print("")
  61. traceback.print_exc()
  62. raise e
  63. return custom_cam, do_training, do_shs_python, do_rot_scale_python, keep_alive, scaling_modifier
  64. else:
  65. return None, None, None, None, None, None