File size: 1,369 Bytes
bc0621e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
import socket
import cv2
import struct
import numpy as np


# Port to listen on
SERVER_PORT = 5555








# Create a TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", SERVER_PORT))
server_socket.listen(1)

# Accept client connection
client_socket, client_address = server_socket.accept()
print(f"Connected to client {client_address}")
count =0
while True:
    print('recievving')
    # Receive frame size (4 bytes integer)
    data = client_socket.recv(4)
    if not data:
            break
    frame_size = int.from_bytes(data, byteorder='big')

  # Receive the frame data (based on size)
    data = b''
    while len(data) < frame_size:
      packet = client_socket.recv(4096)
      if not packet:
        break
      data += packet

    # Decode the received frame data
    frame_encoded = np.frombuffer(data, dtype=np.uint8)
    frame = cv2.imdecode(frame_encoded, cv2.IMREAD_COLOR)

    # Process the frame here (optional)
    # You can display the frame, save it, or perform further analysis

    # Display the frame (optional)
    cv2.imshow('Received Frame', frame)
    cv2.imwrite(f'./imagesMQZ/frame{count}.jpeg',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        

    # Check for client disconnect
    if not frame:
        break
    
server_socket.close()
cv2.destroyAllWindows()