try with http post video
This commit is contained in:
58
api/index.js
58
api/index.js
@@ -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) {
|
function handle_sse_client(request, response, next) {
|
||||||
console.log("handling sse client");
|
console.log("handling sse client");
|
||||||
const headers = {
|
const headers = {
|
||||||
@@ -79,7 +61,7 @@ var connect_to_api = function () {
|
|||||||
ws.on("message", function message(message) {
|
ws.on("message", function message(message) {
|
||||||
try {
|
try {
|
||||||
var msg = JSON.parse(message);
|
var msg = JSON.parse(message);
|
||||||
if (msg.type != "IMAGE") {
|
if (msg.type != "IMAGE") {
|
||||||
// console.log("got message");
|
// console.log("got message");
|
||||||
send_events_to_clients(msg);
|
send_events_to_clients(msg);
|
||||||
} else {
|
} else {
|
||||||
@@ -87,7 +69,7 @@ var connect_to_api = function () {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("could not parse as json");
|
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
|
// set the view engine to ejs
|
||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
|
|
||||||
|
|||||||
@@ -10,10 +10,16 @@ import websockets.server
|
|||||||
import threading
|
import threading
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
#resolution of the camera
|
#resolution of the camera
|
||||||
RES_4K_H = 3496
|
RES_4K_H = 3496
|
||||||
RES_4K_W = 4656
|
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
|
#TODO change to serve video stream through websockets connection
|
||||||
|
|
||||||
class CameraController(Node):
|
class CameraController(Node):
|
||||||
@@ -63,16 +69,35 @@ class CameraController(Node):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
while vid.isOpened():
|
while vid.isOpened():
|
||||||
if self.websocket is not None:
|
pass
|
||||||
img,frame = vid.read()
|
ret, frame = vid.read()
|
||||||
frame = cv2.resize(frame,(640,480))
|
|
||||||
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 65]
|
if not ret:
|
||||||
man = cv2.imencode('.jpg', frame, encode_param)[1]
|
# If reading the frame failed, break the loop
|
||||||
self.get_logger().info('Sending video')
|
break
|
||||||
asyncio.ensure_future(self.websocket.send(man.tobytes()),loop=self.event_loop)
|
|
||||||
await asyncio.sleep(1)
|
# 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:
|
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:
|
except Exception as e:
|
||||||
self.get_logger().error('Something went wrong while reading and sending video: ' + str(e))
|
self.get_logger().error('Something went wrong while reading and sending video: ' + str(e))
|
||||||
|
|||||||
Reference in New Issue
Block a user