I am captured stream with OpenCV and draw boxes, making marks on the frame and want to re-stream this data that can be seen through browser or any video stream connectable software.
I try to use :
***SERVER****
#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;
}
*****CLIENT******
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;
}
This is a slow cant achive more than 10 fps and seems not will usable for our new direction.
I looked ffmpeg , libsources and didnt find an example to show us a direction to start.
How we can re-stream from the OpenCV with all added overlays etc?
Thanks
答案 0 :(得分:0)
我在这里找到了一个最新解决方案MpegServerforRaspi
Raspberry Pi的MJPEG视频HTTP流媒体
这是一个简单的MJPEG HTTP视频流媒体原始编写,可在Raspberry Pi上运行。视频输入使用OpenCV处理,输出服务器基于此Web服务器
它回答了我的问题..