555
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  1. import zmq
  2. import cv2
  3. import numpy as np
  4. import struct
  5. import time
  6. context = zmq.Context()
  7. context2 = zmq.Context()
  8. # Socket to talk to server
  9. print("Connecting to hello world server…")
  10. socket = context.socket(zmq.REQ)
  11. socket.connect("tcp://192.168.0.12:5555")
  12. socket2 = context2.socket(zmq.REQ)
  13. socket2.connect("tcp://192.168.0.12:5556")
  14. recordFrames = 0
  15. startTime = 0
  16. def draw_circle(event, x, y, flags, param):
  17. global recordFrames
  18. if event == cv2.EVENT_LBUTTONDOWN:
  19. print(img[y][x]>>8, img[y][x]&0xff)
  20. print(b[y][x]>>8, b[y][x]&0xff)
  21. recordFrames = 300
  22. startTime = time.time()
  23. with open("lepton_log.bin", "wb") as f:
  24. pass
  25. cv2.namedWindow('image')
  26. cv2.setMouseCallback('image', draw_circle)
  27. while 1:
  28. socket.send(b"Hello")
  29. # Get the reply.
  30. message = socket.recv()
  31. socket2.send(b"Hello")
  32. gridEyeData = socket2.recv()
  33. if recordFrames > 0:
  34. print ("left frames", recordFrames)
  35. with open("lepton_log.bin", "ab") as f:
  36. f.write(message)
  37. f.write(gridEyeData)
  38. recordFrames -= 1
  39. sleepTime = 0.1 - (time.time() - startTime)
  40. if sleepTime > 0:
  41. time.sleep(sleepTime) # wait 0.1 second
  42. startTime = time.time()
  43. continue
  44. img = np.array(struct.unpack("<19200H", message), dtype=np.uint16).reshape((120, 160))
  45. # img = np.ndarray(shape=(19200,),dtype='<u2', buffer=message).reshape((120, 160))
  46. #img = np.frombuffer(message, dtype=np.uint16).reshape((120, 160))
  47. b = img.copy()
  48. for x in range(160):
  49. for y in range(120):
  50. b[y][x] = ((img[y][x]&0xff)<<8) + (img[y][x]>>8)
  51. b = cv2.normalize(b, dst=None, alpha=0, beta=65535, norm_type=cv2.NORM_MINMAX)
  52. #img = img.astype(np.uint8)
  53. #image = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
  54. #cvuint8 = cv2.convertScaleAbs(image)
  55. cv2.imshow("image", b)
  56. # cv2.imshow("image_o", img)
  57. gridEyeImg = np.frombuffer(gridEyeData, dtype=np.uint8).reshape((32, 8))
  58. cv2.imshow("gridEyeImg", cv2.resize(gridEyeImg, (160, 640)))
  59. key = cv2.waitKey(100)
  60. if key == ord('q'):
  61. break
  62. cv2.destroyAllWindows()