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.

78 lines
2.4 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. cam = cv2.VideoCapture(0)
  9. img_counter = 0
  10. encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
  11. socket_switch = True
  12. cut=[-175,-1,-175,-1]
  13. cut_send = [0,0,0,0]
  14. data = {"gpu_temp":"10C","gpu_load":"15%","cpu_temp":"47C","cpu_load":"15%","mem_temp":"NaN","mem_load":"17%","fan_speed":"10000RPM"}
  15. def get_temps():
  16. global data
  17. temps = psutil.sensors_temperatures()
  18. data["cpu_temp"] = str(int(temps["dell_smm"][0][1]))+"°C"
  19. data["cpu_load"] = str(psutil.cpu_percent())+"%"
  20. data["mem_load"] = str(dict(psutil.virtual_memory()._asdict())["percent"])+"%"
  21. data["fan_speed"] = str(psutil.sensors_fans()["dell_smm"][0][1])+"RPM"
  22. while True:
  23. try:
  24. ret, frame = cam.read()
  25. lens = [len(frame),0,len(frame[0])]
  26. for i in range(0,len(cut),2):
  27. if cut[i]<0:
  28. cut_send[i] = lens[i] + cut[i]
  29. cut_send[i+1] = abs(cut[i])-abs(cut[i+1])
  30. backup = frame
  31. frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
  32. crop_img = frame.copy(order='C')
  33. crop_img = Image.fromarray(crop_img,"RGB")
  34. buffered = BytesIO()
  35. crop_img.save(buffered, format="JPEG")
  36. img = base64.b64encode(buffered.getvalue()).decode("ascii")
  37. frame_cut=backup[cut[0]:cut[1],cut[2]:cut[3]]
  38. cv2.imshow("base",backup)
  39. cv2.imshow("cut",frame_cut)
  40. ex_c = [27, ord("q"), ord("Q")]
  41. if cv2.waitKey(1) & 0xFF in ex_c:
  42. break
  43. if socket_switch:
  44. try:
  45. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46. client_socket.connect(('127.0.0.1', 8485))
  47. connection = client_socket.makefile('wb')
  48. socket_switch = False
  49. except:
  50. socket_switch=True
  51. continue
  52. try:
  53. 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")
  54. print(img)
  55. except:
  56. socket_switch=True
  57. img_counter += 1
  58. if img_counter % 10 ==0:
  59. get_temps()
  60. except KeyboardInterrupt:
  61. if not socket_switch:
  62. client_socket.sendall(b"Bye\n")
  63. cam.release()
  64. exit(0)