Advertisement
AnonKot

client

Aug 29th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import cv2
  2. import socket
  3. import pickle
  4. import struct
  5. import time
  6. import threading
  7. # import pyaudio
  8. from mss import mss
  9. import numpy as np
  10.  
  11. e = True
  12. Status = b'CameraOFF'
  13. cap = None
  14.  
  15.  
  16. def CameraOFF():
  17. global cap
  18. global Status
  19. cap.release()
  20. print("CameraOFF")
  21. cap = None
  22. Status = b'CameraOFF'
  23.  
  24.  
  25. def connect():
  26. global cap
  27. global e
  28. global clientsocket
  29. time.sleep(10)
  30. print("TryConnect")
  31. clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  32. try:
  33. clientsocket.connect(('192.168.1.104', 8089))
  34. except socket.error as error:
  35. print("Error")
  36. if cap != None:
  37. CameraOFF()
  38. connect()
  39. else:
  40. print("RequestAccept")
  41. e = False
  42.  
  43.  
  44. def recv():
  45. while True:
  46.  
  47. global clientsocket
  48. global e
  49. global Status
  50. global potok
  51. try:
  52. Status = clientsocket.recv(1024)
  53. except ConnectionResetError as error:
  54. print(1)
  55. e = True
  56. break
  57. potok = threading.Thread(target=recv)
  58.  
  59.  
  60. def cameraON():
  61. global cap
  62.  
  63. if cap == None:
  64. print("CameraON")
  65. cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
  66.  
  67. ret, frame = cap.read()
  68. frame = cv2.flip(frame, 0)
  69. data = pickle.dumps(frame)
  70. message_size = struct.pack("L", len(data))
  71. try:
  72. clientsocket.sendall(message_size + data)
  73. except ConnectionResetError as error:
  74. print("ServerError")
  75. CameraOFF()
  76. if Status == b'CameraOFF' and cap != None:
  77. CameraOFF()
  78.  
  79.  
  80. potok = threading.Thread(target=recv)
  81.  
  82. while True:
  83.  
  84. if e == True:
  85. connect()
  86. elif e == False and not potok.is_alive():
  87. potok.start()
  88.  
  89. if Status == b'CameraOFF':
  90. time.sleep(1)
  91.  
  92. if Status == b'CameraON':
  93. cameraON()
  94.  
  95. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement