Skip to content

Instantly share code, notes, and snippets.

@PyroAVR
Last active February 24, 2021 00:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PyroAVR/789cc08c78e53d5ed8ddce3a024a9b2e to your computer and use it in GitHub Desktop.
Save PyroAVR/789cc08c78e53d5ed8ddce3a024a9b2e to your computer and use it in GitHub Desktop.
SO Question: OpenCV frame corruption
#include <string>
#include <iostream>
// #include <fstream>
#include "zmq.hpp"
#include "ComplexData.hpp"
#include "Handshake.cpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
//vars to hold sizeof message, raw data from message before memcpy
int size;
void* hbuf = new void*;
//await message from specified addr, get info about video
size = zmq_request_handshake("tcp://127.0.0.1:9000", hbuf);
cout << "Hello from client " << endl;
//reinterpret void as video_handshake (POD struct FTW)
video_handshake vmetadata;
memcpy(&vmetadata, hbuf, size);
free(hbuf);
//Establish a new connection with the server to get raw video data
zmq::context_t c(1);
zmq::socket_t client(c, ZMQ_PULL);
client.connect("tcp://127.0.0.1:10000");
Mat frame;
const char* w = "Video Feed";
namedWindow(w, CV_WINDOW_AUTOSIZE);
//ugh debugging
// fstream out;
// out.open("client.dat", fstream::out);
for(;;) {
zmq::message_t m;
client.recv(&m);
cout << "Client: " << m.size() << endl;
// out.write(reinterpret_cast<char*>(m.data()), vmetadata.rows * vmetadata.cols);
frame = Mat(vmetadata.rows,vmetadata.cols, vmetadata.cvtype, m.data(), vmetadata.cvtype);
//display the image
imshow(w, frame);
char c = (char)cvWaitKey(5);
if(c == 27) break; //esc
}
// out.close();
return 0;
}
#pragma once
struct video_handshake {
int rows, cols, cvtype, cvstep;
};
void zmq_accept_handshake(const char* endpoint, void* data, int size) {
zmq::context_t context(1);
zmq::socket_t sock(context, ZMQ_REP);
sock.bind(endpoint);
zmq::message_t rep(size);
memcpy(rep.data(), data, size);
zmq::message_t req;
sock.recv(&req);
sock.send(rep);
//sock.unbind(endpoint);
}
int zmq_request_handshake(const char* endpoint, void* data) {
zmq::context_t context(1);
zmq::socket_t sock(context, ZMQ_REQ);
sock.connect(endpoint);
zmq::message_t rep;
zmq::message_t req(1);
sock.send(req);
sock.recv(&rep);
memcpy(data, rep.data(), rep.size());
sock.unbind(endpoint);
return rep.size();
}
all:
g++ server.cpp -lzmq -lopencv_core -lopencv_highgui -std=c++1y -g -O0 -o server
g++ client.cpp -lzmq -lopencv_core -lopencv_highgui -std=c++1y -g -O0 -o client
#include <string>
// #include <fstream>
#include <iostream>
#include "zmq.hpp"
#include "ComplexData.hpp"
#include "Handshake.cpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
//need to get info about the camera first
Mat frame;
VideoCapture cap(0);
cap >> frame;
video_handshake h = {static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT)),
static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH)),
frame.type(), static_cast<int>(frame.elemSize())};
//wait for client to request metadata, intiating video transmission
zmq_accept_handshake("tcp://*:9000", &h,sizeof(h));
cout << "Hello from Server" << endl;
//allow the client to make a new connection to receive video on
zmq::context_t c(1);
zmq::socket_t server(c, ZMQ_PUSH);
server.bind("tcp://*:10000");
//ugh debugging
// fstream out;
// out.open("server.dat", fstream::out);
const char* w = "Input Data";
namedWindow(w, CV_WINDOW_AUTOSIZE);
while(cap.isOpened()) {
//get a frame
cap >> frame;
imshow(w, frame);
char c = (char)cvWaitKey(5);
if(c == 27) break; //esc
cout << "Server: " << frame.rows * frame.cols << endl;
//allocate a new message big enough for our frame, move data to message, send
zmq::message_t m(frame.rows * frame.cols);
// out.write(reinterpret_cast<char*>(frame.data), frame.rows * frame.cols);
memcpy(m.data(), frame.data, frame.rows * frame.cols);
server.send(m);
}
// out.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment