Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import cv2
- import pickle
- import struct
- import numpy
- from PIL import ImageGrab
- class Client:
- ENCODE_PARAM = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
- def __init__(self, *address, ind=0, width=960, height=540):
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.connect(address)
- self.connection = self.sock.makefile('wb')
- self.ind = ind
- self.width = width
- self.height = height
- @property
- def camera(self):
- cam = cv2.VideoCapture(self.ind)
- cam.set(3, self.width)
- cam.set(4, self.height)
- return cam
- def capture_camera(self):
- while True:
- _, frame = self.camera.read()
- self.sock.sendall(self._pack_frame(frame))
- def capture_screen(self):
- while True:
- image = ImageGrab.grab((0, 0, self.width, self.height))
- frame = numpy.array(image)
- self.sock.sendall(self._pack_frame(frame))
- def _pack_frame(self, frame):
- _, frame = cv2.imencode('.jpg', frame, self.ENCODE_PARAM)
- data = pickle.dumps(frame, 0)
- return struct.pack('>L', len(data)) + data
- # Example
- def main():
- Client('localhost', 8485, width=1920, height=1080).capture_screen()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement