Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import socket
  2. import cv2
  3. import pickle
  4. import struct
  5. import numpy
  6.  
  7. from PIL import ImageGrab
  8.  
  9.  
  10. class Client:
  11. ENCODE_PARAM = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
  12.  
  13. def __init__(self, *address, ind=0, width=960, height=540):
  14. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  15. self.sock.connect(address)
  16. self.connection = self.sock.makefile('wb')
  17.  
  18. self.ind = ind
  19. self.width = width
  20. self.height = height
  21.  
  22. @property
  23. def camera(self):
  24. cam = cv2.VideoCapture(self.ind)
  25. cam.set(3, self.width)
  26. cam.set(4, self.height)
  27. return cam
  28.  
  29. def capture_camera(self):
  30. while True:
  31. _, frame = self.camera.read()
  32. self.sock.sendall(self._pack_frame(frame))
  33.  
  34. def capture_screen(self):
  35. while True:
  36. image = ImageGrab.grab((0, 0, self.width, self.height))
  37. frame = numpy.array(image)
  38. self.sock.sendall(self._pack_frame(frame))
  39.  
  40. def _pack_frame(self, frame):
  41. _, frame = cv2.imencode('.jpg', frame, self.ENCODE_PARAM)
  42. data = pickle.dumps(frame, 0)
  43.  
  44. return struct.pack('>L', len(data)) + data
  45.  
  46.  
  47. # Example
  48. def main():
  49. Client('localhost', 8485, width=1920, height=1080).capture_screen()
  50.  
  51.  
  52. if __name__ == '__main__':
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement