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.

102 lines
3.0 KiB

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