main.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. import socket
  3. host = "84.237.21.36"
  4. port = 5152
  5. def recvall(sock, n):
  6. data = bytearray()
  7. while len(data) < n:
  8. packet = sock.recv(n - len(data))
  9. if not packet:
  10. return
  11. data.extend(packet)
  12. return data
  13. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  14. sock.connect((host, port))
  15. for _ in range(10):
  16. sock.send(b"get")
  17. bits = recvall(sock, 40002)
  18. print(len(bits))
  19. img = np.frombuffer(bits[2:], dtype="uint8").reshape(bits[0], bits[1])
  20. centers = []
  21. for y in range(1,img.shape[0]-2):
  22. for x in range(1,img.shape[1]-2):
  23. if img[y, x] > img[y, x-1] and img[y, x] > img[y, x+1] and img[y, x] > img[y+1, x] and img[y, x] > img[y-1, x]:
  24. centers.append(np.array([y,x]))
  25. res = ((centers[0][0] - centers[1][0]) ** 2 + (centers[0][1] - centers[1][1]) ** 2) ** 0.5
  26. sock.send(f"{res:.1f}".encode())
  27. ye = sock.recv(20)
  28. print(ye)
  29. sock.send(b'beat')
  30. print("done:",sock.recv(20))