File size: 1,914 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
63
64
65
import socket
import cv2
import struct
import numpy as np

# Import library for H.265 decoding (choose one)
# Option 1: Using OpenCV with FFmpeg backend (if FFmpeg is installed)
# cv2.h264decode is a wrapper for FFmpeg's H.265 decoder
# Make sure FFmpeg is installed and the environment variable (e.g., PATH) is set correctly

# Option 2: Using a dedicated H.265 decoder library (e.g., gstreamer)
# You'll need to install and configure the chosen library following its documentation

# 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('Receiving')
  # 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 H.265 data
  # Option 1: Using OpenCV with FFmpeg backend
  frame = cv2.imdecode(np.frombuffer(data, dtype=np.uint8), cv2.IMREAD_COLOR)

  # Option 2: Using a dedicated H.265 decoder library
  # Replace this with the appropriate decoding function from your chosen library
  # frame = ... (decode the data using your library)

  # 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()