为 C++ 初学者从 OpenCV 流式传输的优雅方式?
Elegant way of Streaming from OpenCV for C++ beginners?
我使用 OpenCV 捕获流并绘制框,在框架上做标记并想重新流式传输可以通过浏览器或任何视频流连接软件看到的数据。
我尝试使用:
***服务器****
#define BUF_LEN 65540 // Larger than maximum UDP packet size
#include "opencv2/opencv.hpp"
using namespace cv;
#include "config.h"
int main(int argc, char * argv[]) {
if (argc != 2) { // Test for correct number of parameters
cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
exit(1);
}
unsigned short servPort = atoi(argv[1]); // First arg: local port
namedWindow("recv", CV_WINDOW_AUTOSIZE);
try {
UDPSocket sock(servPort);
char buffer[BUF_LEN]; // Buffer for echo string
int recvMsgSize; // Size of received message
string sourceAddress; // Address of datagram source
unsigned short sourcePort; // Port of datagram source
clock_t last_cycle = clock();
while (1) {
// Block until receive message from a client
do {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
} while (recvMsgSize > sizeof(int));
int total_pack = ((int * ) buffer)[0];
cout << "expecting length of packs:" << total_pack << endl;
char * longbuf = new char[PACK_SIZE * total_pack];
for (int i = 0; i < total_pack; i++) {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
if (recvMsgSize != PACK_SIZE) {
cerr << "Received unexpected size pack:" << recvMsgSize << endl;
continue;
}
memcpy( & longbuf[i * PACK_SIZE], buffer, PACK_SIZE);
}
cout << "Received packet from " << sourceAddress << ":" << sourcePort << endl;
Mat rawData = Mat(1, PACK_SIZE * total_pack, CV_8UC1, longbuf);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
if (frame.size().width == 0) {
cerr << "decode failure!" << endl;
continue;
}
imshow("recv", frame);
free(longbuf);
waitKey(1);
clock_t next_cycle = clock();
double duration = (next_cycle - last_cycle) / (double) CLOCKS_PER_SEC;
cout << "\teffective FPS:" << (1 / duration) << " \tkbps:" << (PACK_SIZE * total_pack / duration / 1024 * 8) << endl;
cout << next_cycle - last_cycle;
last_cycle = next_cycle;
}
} catch (SocketException & e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}
*****客户******
using namespace std;
#include "opencv2/opencv.hpp"
using namespace cv;
#include "config.h"
int main(int argc, char * argv[]) {
if ((argc < 3) || (argc > 3)) { // Test for correct number of arguments
cerr << "Usage: " << argv[0] << " <Server> <Server Port>\n";
exit(1);
}
string servAddress = argv[1]; // First arg: server address
unsigned short servPort = Socket::resolveService(argv[2], "udp");
try {
UDPSocket sock;
int jpegqual = ENCODE_QUALITY; // Compression Parameter
Mat frame, send;
vector < uchar > encoded;
VideoCapture cap("http://ckyxtrm.com:3000/live/muzisyenhakan/29mayismh/178.ts"); // Grab the camera
namedWindow("send", CV_WINDOW_AUTOSIZE);
if (!cap.isOpened()) {
cerr << "OpenCV Failed to open camera";
exit(1);
}
clock_t last_cycle = clock();
while (1) {
cap >> frame;
if(frame.size().width==0)continue;//simple integrity check; skip erroneous data...
resize(frame, send, Size(FRAME_WIDTH, FRAME_HEIGHT), 0, 0, INTER_LINEAR);
vector < int > compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(jpegqual);
imencode(".jpg", send, encoded, compression_params);
imshow("send", send);
int total_pack = 1 + (encoded.size() - 1) / PACK_SIZE;
int ibuf[1];
ibuf[0] = total_pack;
sock.sendTo(ibuf, sizeof(int), servAddress, servPort);
for (int i = 0; i < total_pack; i++)
sock.sendTo( & encoded[i * PACK_SIZE], PACK_SIZE, servAddress, servPort);
// waitKey(FRAME_INTERVAL);
waitKey(1);
clock_t next_cycle = clock();
double duration = (next_cycle - last_cycle) / (double) CLOCKS_PER_SEC;
cout << "\teffective FPS:" << (1 / duration) << " \tkbps:" << (PACK_SIZE * total_pack / duration / 1024 * 8) << endl;
cout << next_cycle - last_cycle;
last_cycle = next_cycle;
}
// Destructor closes the socket
} catch (SocketException & e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}
这是一个无法达到超过 10 fps 的缓慢速度,并且似乎不适用于我们的新方向。
我查看了 ffmpeg 和 libsources,但没有找到向我们展示开始方向的示例。
我们如何使用所有添加的叠加层等从 OpenCV 重新流式传输?
谢谢
我在这里找到了一个最新的解决方案 MpegServerforRaspi
Raspberry Pi
的 MJPEG 视频 HTTP 流媒体
这是一个简单的 MJPEG HTTP 视频流,原版已写入 Raspberry Pi 上的 运行。视频输入使用OpenCV处理,输出服务器基于此web服务器
它回答了我的问题..
我使用 OpenCV 捕获流并绘制框,在框架上做标记并想重新流式传输可以通过浏览器或任何视频流连接软件看到的数据。
我尝试使用:
***服务器****
#define BUF_LEN 65540 // Larger than maximum UDP packet size
#include "opencv2/opencv.hpp"
using namespace cv;
#include "config.h"
int main(int argc, char * argv[]) {
if (argc != 2) { // Test for correct number of parameters
cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
exit(1);
}
unsigned short servPort = atoi(argv[1]); // First arg: local port
namedWindow("recv", CV_WINDOW_AUTOSIZE);
try {
UDPSocket sock(servPort);
char buffer[BUF_LEN]; // Buffer for echo string
int recvMsgSize; // Size of received message
string sourceAddress; // Address of datagram source
unsigned short sourcePort; // Port of datagram source
clock_t last_cycle = clock();
while (1) {
// Block until receive message from a client
do {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
} while (recvMsgSize > sizeof(int));
int total_pack = ((int * ) buffer)[0];
cout << "expecting length of packs:" << total_pack << endl;
char * longbuf = new char[PACK_SIZE * total_pack];
for (int i = 0; i < total_pack; i++) {
recvMsgSize = sock.recvFrom(buffer, BUF_LEN, sourceAddress, sourcePort);
if (recvMsgSize != PACK_SIZE) {
cerr << "Received unexpected size pack:" << recvMsgSize << endl;
continue;
}
memcpy( & longbuf[i * PACK_SIZE], buffer, PACK_SIZE);
}
cout << "Received packet from " << sourceAddress << ":" << sourcePort << endl;
Mat rawData = Mat(1, PACK_SIZE * total_pack, CV_8UC1, longbuf);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
if (frame.size().width == 0) {
cerr << "decode failure!" << endl;
continue;
}
imshow("recv", frame);
free(longbuf);
waitKey(1);
clock_t next_cycle = clock();
double duration = (next_cycle - last_cycle) / (double) CLOCKS_PER_SEC;
cout << "\teffective FPS:" << (1 / duration) << " \tkbps:" << (PACK_SIZE * total_pack / duration / 1024 * 8) << endl;
cout << next_cycle - last_cycle;
last_cycle = next_cycle;
}
} catch (SocketException & e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}
*****客户******
using namespace std;
#include "opencv2/opencv.hpp"
using namespace cv;
#include "config.h"
int main(int argc, char * argv[]) {
if ((argc < 3) || (argc > 3)) { // Test for correct number of arguments
cerr << "Usage: " << argv[0] << " <Server> <Server Port>\n";
exit(1);
}
string servAddress = argv[1]; // First arg: server address
unsigned short servPort = Socket::resolveService(argv[2], "udp");
try {
UDPSocket sock;
int jpegqual = ENCODE_QUALITY; // Compression Parameter
Mat frame, send;
vector < uchar > encoded;
VideoCapture cap("http://ckyxtrm.com:3000/live/muzisyenhakan/29mayismh/178.ts"); // Grab the camera
namedWindow("send", CV_WINDOW_AUTOSIZE);
if (!cap.isOpened()) {
cerr << "OpenCV Failed to open camera";
exit(1);
}
clock_t last_cycle = clock();
while (1) {
cap >> frame;
if(frame.size().width==0)continue;//simple integrity check; skip erroneous data...
resize(frame, send, Size(FRAME_WIDTH, FRAME_HEIGHT), 0, 0, INTER_LINEAR);
vector < int > compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(jpegqual);
imencode(".jpg", send, encoded, compression_params);
imshow("send", send);
int total_pack = 1 + (encoded.size() - 1) / PACK_SIZE;
int ibuf[1];
ibuf[0] = total_pack;
sock.sendTo(ibuf, sizeof(int), servAddress, servPort);
for (int i = 0; i < total_pack; i++)
sock.sendTo( & encoded[i * PACK_SIZE], PACK_SIZE, servAddress, servPort);
// waitKey(FRAME_INTERVAL);
waitKey(1);
clock_t next_cycle = clock();
double duration = (next_cycle - last_cycle) / (double) CLOCKS_PER_SEC;
cout << "\teffective FPS:" << (1 / duration) << " \tkbps:" << (PACK_SIZE * total_pack / duration / 1024 * 8) << endl;
cout << next_cycle - last_cycle;
last_cycle = next_cycle;
}
// Destructor closes the socket
} catch (SocketException & e) {
cerr << e.what() << endl;
exit(1);
}
return 0;
}
这是一个无法达到超过 10 fps 的缓慢速度,并且似乎不适用于我们的新方向。
我查看了 ffmpeg 和 libsources,但没有找到向我们展示开始方向的示例。
我们如何使用所有添加的叠加层等从 OpenCV 重新流式传输?
谢谢
我在这里找到了一个最新的解决方案 MpegServerforRaspi
Raspberry Pi
的 MJPEG 视频 HTTP 流媒体这是一个简单的 MJPEG HTTP 视频流,原版已写入 Raspberry Pi 上的 运行。视频输入使用OpenCV处理,输出服务器基于此web服务器
它回答了我的问题..