# OpenCV ## Requirements We want that the camera we want it to detect what is happening on the video feed and identify it so it can identify dangers. ## Issues * OpenCL not grabbing gpu * Solution: https://github.com/Smorodov/Multitarget-tracker/issues/93 ## Installation ### Dependencies * glew (for openGL) * opencv C++ lib How to install OpenCV ```bash sudo apt-get install libopencv-dev ``` ## Code explanation ### Opening the camera with OpenCV ```cpp VideoCapture cap(0); //Open the default camera (0), points to /dev/video0. You could also change the number to the preferred camera if (!cap.isOpened()) { //if camera is not opened throw a error message cerr << "Error: Could not open camera" << endl; return; } ``` ## Taking a picture and storing it in a variable ```cpp Mat frame; //create a new Matrix variable called frame while (true) { cap >> frame; // Capture a new image frame. if (frame.empty()) { //if the variable frame is not filled return a error cerr << "Error: Could not capture image" << endl; continue; } ``` ## Encoding the image for sending it over MQTT ```cpp vector buf; //create a dyanmic buffer for the image imencode(".jpg", frame, buf); //encode the image to the buffer auto* enc_msg = reinterpret_cast(buf.data()); ``` ```cpp void CapnSend() { // Convert the image to a byte array // Publish the image data client.publishMessage("kobuki/cam", string(enc_msg, enc_msg + buf.size())); cout << "Sent image" << endl; std::this_thread::sleep_for(std::chrono::milliseconds(300)); // Send image every 1000ms } } ``` ## Sources * https://github.com/UnaNancyOwen/OpenCVDNNSample/tree/master