try with http post video

This commit is contained in:
Sem van der Hoeven
2023-06-02 12:47:11 +02:00
parent f197f595c5
commit 1e1b9c0e93
2 changed files with 72 additions and 29 deletions

View File

@@ -25,24 +25,6 @@ function send_events_to_clients(data) {
});
}
function send_image_data_to_clients(frameData) {
sse_clients.forEach((client) => {
// Create a Buffer from the frame data
const buffer = Buffer.from(frameData);
// Set the SSE event name as 'message'
client.response.write("event: message\n");
// Convert the Buffer to a base64-encoded string
const base64Data = buffer.toString("base64");
// Set the SSE event data as the base64-encoded string
client.response.write(
"data: " + JSON.stringify({ image: base64Data }) + "\n\n"
);
});
}
function handle_sse_client(request, response, next) {
console.log("handling sse client");
const headers = {
@@ -79,7 +61,7 @@ var connect_to_api = function () {
ws.on("message", function message(message) {
try {
var msg = JSON.parse(message);
if (msg.type != "IMAGE") {
if (msg.type != "IMAGE") {
// console.log("got message");
send_events_to_clients(msg);
} else {
@@ -87,7 +69,7 @@ var connect_to_api = function () {
}
} catch (error) {
console.log("could not parse as json");
// send_image_data_to_clients(message);
// send_image_data_to_clients(message);
}
});
@@ -98,6 +80,42 @@ var connect_to_api = function () {
});
};
function send_image_data_to_clients(videoData) {
sse_clients.forEach((client) => {
// Set the SSE event name as 'message'
client.response.write("event: message\n");
// Convert the Buffer to a base64-encoded string
const base64Data = videoData.toString("base64");
// Set the SSE event data as the base64-encoded string
client.response.write(
"data: " + JSON.stringify({ image: base64Data }) + "\n\n"
);
});
}
// Define the endpoint to receive video data
app.post("/video", (req, res) => {
console.log("got video endpoint")
let videoData = Buffer.from("");
req.on("data", (chunk) => {
// Accumulate the received video data
videoData = Buffer.concat([videoData, chunk]);
});
req.on("end", () => {
// Process the received video data
console.log("Received video data:" + videoData.length);
send_image_data_to_clients(videoData);
// Send a response indicating successful receipt
res.sendStatus(200);
});
});
// set the view engine to ejs
app.set("view engine", "ejs");

View File

@@ -10,10 +10,16 @@ import websockets.server
import threading
import cv2
import requests
#resolution of the camera
RES_4K_H = 3496
RES_4K_W = 4656
video_url = "http://10.1.1.41:8080/video"
# Set the headers for the POST request
headers = {'Content-Type': 'application/octet-stream'}
#TODO change to serve video stream through websockets connection
class CameraController(Node):
@@ -63,16 +69,35 @@ class CameraController(Node):
while True:
try:
while vid.isOpened():
if self.websocket is not None:
img,frame = vid.read()
frame = cv2.resize(frame,(640,480))
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 65]
man = cv2.imencode('.jpg', frame, encode_param)[1]
self.get_logger().info('Sending video')
asyncio.ensure_future(self.websocket.send(man.tobytes()),loop=self.event_loop)
await asyncio.sleep(1)
pass
ret, frame = vid.read()
if not ret:
# If reading the frame failed, break the loop
break
# Convert the frame to bytes
_, img_encoded = cv2.imencode('.jpg', frame)
frame_data = img_encoded.tobytes()
# Send the frame data as the request body
response = requests.post(video_url, data=frame_data, headers=headers)
# Check the response status
if response.status_code == 200:
print('Frame sent successfully.')
else:
self.get_logger().info('No websocket connection')
print('Failed to send frame.')
# if self.websocket is not None:
# img,frame = vid.read()
# frame = cv2.resize(frame,(640,480))
# encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 65]
# man = cv2.imencode('.jpg', frame, encode_param)[1]
# self.get_logger().info('Sending video')
# asyncio.ensure_future(self.websocket.send(man.tobytes()),loop=self.event_loop)
# await asyncio.sleep(1)
# else:
# self.get_logger().info('No websocket connection')
except Exception as e:
self.get_logger().error('Something went wrong while reading and sending video: ' + str(e))