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.

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