C ++ / OpenCV Streaming Camera-Video / Images (MJPEG) from Socket to Browser (Windows 8.1)

Still quite new to openCV / C ++, so please bear with me :)

I'm currently trying to find a good (and possibly simple) way to transfer my cameras in real time (or almost real time) from my OpenCV application so that I can open my browser, enter the IP address and see the image.

So far, I got the server using winsock2 (if anyone has a good alternative to the cross-platform platform, and I can say that this is a great option, I would be very happy) and can connect to it by entering the IP address in my browser .

socket / server code:

//socket long rc; SOCKET acceptSocket; SOCKADDR_IN addr; WSADATA wsa; // initialize winsock rc=WSAStartup(MAKEWORD(2,0),&wsa); if(rc!=0) { printf("Error: startWinsock, Errorcode: %d\n",rc); return 1; } else { printf("Winsock initialized!\n"); } // create Socket acceptSocket=socket(AF_INET,SOCK_STREAM,0); if(acceptSocket==INVALID_SOCKET) { printf("Error: Socket-Creation failed, Errorcode: %d\n",WSAGetLastError()); return 1; } else { printf("Socket succesfully created!\n"); } memset(&addr,0,sizeof(SOCKADDR_IN)); addr.sin_family=AF_INET; addr.sin_port=htons(8080); addr.sin_addr.s_addr=ADDR_ANY; rc=bind(acceptSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN)); if(rc==SOCKET_ERROR) { printf("Error: bind, Errorcode: %d\n",WSAGetLastError()); return 1; } else { printf("Socket bound to port 8080\n"); } rc=listen(acceptSocket,10); if(rc==SOCKET_ERROR) { printf("Error: listen, Errorcode: %d\n",WSAGetLastError()); return 1; } else { printf("acceptSocket is now in listen mode...\n"); } SOCKET connectedSocket=accept(acceptSocket,NULL,NULL); if(connectedSocket==INVALID_SOCKET) { printf("Error, Errorcode: %d\n",WSAGetLastError()); return 1; } else { printf("New connection accepted!\n"); } 

As for the sending part, I tried to use the camera frame directly and save it as jpg + reload jpg:

  char filename[128]; frame_count++; if (frame_count%50 == 0) { sprintf(filename, "frame_%06d.jpg", index); imwrite(filename, camera1_undist); Mat image = imread(filename, CV_LOAD_IMAGE_COLOR); send(connectedSocket, (const char *) image.data, image.total()*image.elemSize(), 0); frame_count = 0; index++; } 

Questions:

1) The image is not displayed as an image, but as numbers / symbols (HEX / ASCII? Mainly black question marks and so on). How do I convert / change what I'm sending in order to actually show the image?

2) I read about MJPEG and found a way to save the output, but I have no idea how to use this output file for future reference. How to use it without MJPEG-Streamer (since it is only Linux)

thanks

+5
source share
1 answer

I did it now, made some mistakes here and there.

The most important part to get an image was

a) to get the html header that is sent before sending the actual image so that my browser knows what will be sent

b) use imencode to save the image in the buffer and send this buffer instead of the image itself

I also don’t save the frame anywhere, but just take it from the very entrance to the camera so that it also reduces.

To do this using mjpegs, I just needed to add another header that tells the "client" that several images will be sent to it, separated by a certain border:

 sprintf(head, "HTTP/1.1 200 OK\r\nContent-Type: multipart/x-mixed-replace;boundary=informs\r\n\r\n"); send(socket,head,strlen(head), 0); 

It helped a bit, especially. with the title: http://nakkaya.com/2011/03/23/streaming-opencv-video-over-the-network-using-mjpeg/ (although at first it annoyed me because I had never used and never seen before clojure.

It was also really useful: http://answers.opencv.org/question/6976/display-iplimage-in-webbrowsers/

+6
source

Source: https://habr.com/ru/post/1202583/


All Articles