Spaces:
Runtime error
Runtime error
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() |