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.

107 lines
3.1 KiB

  1. import cv2
  2. import socket
  3. import json
  4. import base64
  5. from PIL import Image
  6. from io import BytesIO
  7. import psutil
  8. <<<<<<< Updated upstream
  9. import multiprocessing
  10. cam = cv2.VideoCapture(0)
  11. =======
  12. cam = cv2.VideoCapture(1)
  13. >>>>>>> Stashed changes
  14. def open_switch():
  15. HOST = '127.0.0.1' # Standard loopback interface address (localhost)
  16. PORT = 8385 # Port to listen on (non-privileged ports are > 1023)
  17. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  18. s.bind((HOST, PORT))
  19. s.listen()
  20. while 1:
  21. conn, addr = s.accept()
  22. with conn:
  23. while True:
  24. data = conn.recv(1024)
  25. if not data:
  26. break
  27. conn.sendall(data)
  28. img_counter = 0
  29. encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
  30. socket_switch = True
  31. cut=[-175,-1,-175,-1]
  32. cut_send = [0,0,0,0]
  33. data = {"gpu_temp":"10C","gpu_load":"15%","cpu_temp":"47C","cpu_load":"15%","mem_temp":"NaN","mem_load":"17%","fan_speed":"10000RPM"}
  34. def get_temps():
  35. global data
  36. temps = psutil.sensors_temperatures()
  37. data["cpu_temp"] = str(int(temps["dell_smm"][0][1]))+"°C"
  38. data["cpu_load"] = str(psutil.cpu_percent())+"%"
  39. data["mem_load"] = str(dict(psutil.virtual_memory()._asdict())["percent"])+"%"
  40. data["fan_speed"] = str(psutil.sensors_fans()["dell_smm"][0][1])+"RPM"
  41. p1 = multiprocessing.Process(target=open_switch)
  42. p1.start()
  43. while True:
  44. try:
  45. ret, frame = cam.read()
  46. lens = [len(frame),0,len(frame[0])]
  47. for i in range(0,len(cut),2):
  48. if cut[i]<0:
  49. cut_send[i] = lens[i] + cut[i]
  50. cut_send[i+1] = abs(cut[i])-abs(cut[i+1])
  51. backup = frame
  52. frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
  53. crop_img = frame.copy(order='C')
  54. crop_img = Image.fromarray(crop_img,"RGB")
  55. buffered = BytesIO()
  56. crop_img.save(buffered, format="JPEG")
  57. img = base64.b64encode(buffered.getvalue()).decode("ascii")
  58. frame_cut=backup[cut[0]:cut[1],cut[2]:cut[3]]
  59. cv2.imshow("base",backup)
  60. cv2.imshow("cut",frame_cut)
  61. ex_c = [27, ord("q"), ord("Q")]
  62. if cv2.waitKey(1) & 0xFF in ex_c:
  63. break
  64. if socket_switch:
  65. try:
  66. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  67. client_socket.connect(('10.10.26.163', 8485))
  68. connection = client_socket.makefile('wb')
  69. socket_switch = False
  70. except:
  71. socket_switch=True
  72. continue
  73. try:
  74. client_socket.sendall(json.dumps({"image_full":img,"image_sizes":{"x":cut_send[2],"y":cut_send[0],"width":cut_send[3],"height":cut_send[1]},"load":data}).encode('gbk')+b"\n")
  75. print(img)
  76. except:
  77. socket_switch=True
  78. img_counter += 1
  79. if img_counter % 10 ==0:
  80. get_temps()
  81. except KeyboardInterrupt:
  82. if not socket_switch:
  83. client_socket.sendall(b"Bye\n")
  84. cam.release()
  85. p1.terminate()
  86. break
  87. cv2.destroyAllWindows()
  88. p1.terminate()