websocketController
index
/home/bedrock/Desktop/project/AlertWatch/Production/detector/util/websocketController.py

WebSocket Client for Sending Video and Tensor Data
 
This module provides a WebSocket client implementation for connecting to a server,
sending video frames, tensors, and additional data. It supports reconnecting on failure.
 
Classes:
    - WebSocketClient: A client for managing WebSocket connections and sending data.
 
Functions:
    - initialize_websocket: Initializes a `WebSocketClient` instance and connects to the server.
 
Dependencies:
    - asyncio: For managing asynchronous WebSocket connections and tasks.
    - websockets: For WebSocket communication.
    - cv2: For video frame encoding.
    - json, base64: For preparing and encoding data to send over WebSocket.
 
Usage:
    Use `initialize_websocket` to set up a WebSocket connection and start sending data.
 
Author:
    Lior Jigalo
 
License:
    MIT

 
Modules
       
asyncio
base64
cv2
json
websockets

 
Classes
       
builtins.object
WebSocketClient

 
class WebSocketClient(builtins.object)
    WebSocketClient(ws_url, reconnect_interval=2)
 
WebSocket client for connecting to a server and sending data.
 
Attributes:
    ws_url (str): The WebSocket server URL.
    reconnect_interval (int): Time in seconds between reconnection attempts.
    websocket (websockets.WebSocketClientProtocol | None): The WebSocket connection instance.
    first_connection (bool): Indicates whether this is the first connection attempt.
 
Methods:
    connect: Establishes a WebSocket connection with retries.
    run_bash_commands: Placeholder for executing bash commands during initialization.
    send_data: Sends video frames, tensors, and additional data to the server.
 
  Methods defined here:
__init__(self, ws_url, reconnect_interval=2)
Initializes the WebSocketClient instance.
 
Args:
    ws_url (str): The WebSocket server URL.
    reconnect_interval (int): Time in seconds between reconnection attempts (default: 2).
async connect(self)
Establishes a WebSocket connection to the server.
 
Retries if the connection fails, up to a maximum of 2 attempts.
async send_data(self, frame, tensors, command_outputs=None)
Sends video frames and tensor data to the WebSocket server.
 
Args:
    frame (np.ndarray): The video frame to send.
    tensors (dict): A dictionary of tensor data to send.
    command_outputs (dict | None): Optional command outputs to include in the message.
 
Returns:
    None

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
async initialize_websocket(ws_url, reconnect_interval) -> websocketController.WebSocketClient
Initializes the WebSocket client and connects to the server.
 
Args:
    ws_url (str): The WebSocket server URL.
    reconnect_interval (int): Time in seconds between reconnection attempts.
 
Returns:
    WebSocketClient: An instance of the initialized WebSocket client.