|
| 1 | +/// Example 2.3 : Video pipeline with ImageCroppingCalculator (dynamic crop) |
| 2 | +/// By Oleksiy Grechnyev, IT-JIM |
| 3 | +/// Here I show how to use ImageCroppingCalculator with a dynamic crop |
| 4 | +/// Translation: the cropping rect is no longer supplied in calculator options, |
| 5 | +/// But cropping rects for each frame come from a separate input stream in_rect |
| 6 | +/// ACHTUNG! This pipeline is still NOT real-time! |
| 7 | + |
| 8 | + |
| 9 | +#include <iostream> |
| 10 | +#include <string> |
| 11 | +#include <memory> |
| 12 | +#include <atomic> |
| 13 | +#include <mutex> |
| 14 | +#include <cmath> |
| 15 | + |
| 16 | +#include "mediapipe/framework/calculator_framework.h" |
| 17 | +#include "mediapipe/framework/formats/image_frame.h" |
| 18 | +#include "mediapipe/framework/formats/image_frame_opencv.h" |
| 19 | +#include "mediapipe/framework/port/parse_text_proto.h" |
| 20 | +#include "mediapipe/framework/port/status.h" |
| 21 | + |
| 22 | +#include "mediapipe/framework/port/opencv_highgui_inc.h" |
| 23 | +#include "mediapipe/framework/port/opencv_imgproc_inc.h" |
| 24 | + |
| 25 | +#include "mediapipe/framework/formats/rect.pb.h" |
| 26 | + |
| 27 | +//============================================================================== |
| 28 | +mediapipe::Status run() { |
| 29 | + using namespace std; |
| 30 | + using namespace mediapipe; |
| 31 | + |
| 32 | + // A graph with ImageCroppingCalculator |
| 33 | + // Here we have a second inpt stream in_rect |
| 34 | + // Which contains the cropping rect for each frame, note the tag RECT |
| 35 | + string protoG = R"( |
| 36 | + input_stream: "in" |
| 37 | + input_stream: "in_rect" |
| 38 | + output_stream: "out" |
| 39 | + node { |
| 40 | + calculator: "ImageCroppingCalculator" |
| 41 | + input_stream: "IMAGE:in" |
| 42 | + input_stream: "RECT:in_rect" |
| 43 | + output_stream: "IMAGE:out" |
| 44 | + } |
| 45 | + )"; |
| 46 | + |
| 47 | + // Parse config and create graph |
| 48 | + CalculatorGraphConfig config; |
| 49 | + if (!ParseTextProto<mediapipe::CalculatorGraphConfig>(protoG, &config)) { |
| 50 | + return absl::InternalError("Cannot parse the graph config !"); |
| 51 | + } |
| 52 | + CalculatorGraph graph; |
| 53 | + MP_RETURN_IF_ERROR(graph.Initialize(config)); |
| 54 | + |
| 55 | + // Mutex protecting imshow() and the stop flag |
| 56 | + mutex mutexImshow; |
| 57 | + atomic_bool flagStop(false); |
| 58 | + |
| 59 | + // Add observer to "out", then start the graph |
| 60 | + // This callback displays the frame on the screen |
| 61 | + auto cb = [&mutexImshow, &flagStop](const Packet &packet)->Status{ |
| 62 | + |
| 63 | + // Get cv::Mat from the packet |
| 64 | + const ImageFrame & outputFrame = packet.Get<ImageFrame>(); |
| 65 | + cv::Mat ofMat = formats::MatView(&outputFrame); |
| 66 | + cv::Mat frameOut; |
| 67 | + cvtColor(ofMat, frameOut, cv::COLOR_RGB2BGR); |
| 68 | + cout << packet.Timestamp() << ": RECEIVED VIDEO PACKET size = " << frameOut.size() << endl; |
| 69 | + |
| 70 | + { |
| 71 | + lock_guard<mutex> lock(mutexImshow); |
| 72 | + // Display frame on screen and quit on ESC |
| 73 | + cv::imshow("frameOut", frameOut); |
| 74 | + if (27 == cv::waitKey(1)){ |
| 75 | + cout << "It's time to QUIT !" << endl; |
| 76 | + flagStop = true; |
| 77 | + } |
| 78 | + } |
| 79 | + return OkStatus(); |
| 80 | + }; |
| 81 | + MP_RETURN_IF_ERROR(graph.ObserveOutputStream("out", cb)); |
| 82 | + graph.StartRun({}); |
| 83 | + |
| 84 | + // Start the camera and check that it works |
| 85 | + cv::VideoCapture cap(cv::CAP_ANY); |
| 86 | + if (!cap.isOpened()) |
| 87 | + return absl::NotFoundError("CANNOT OPEN CAMERA !"); |
| 88 | + cv::Mat frameIn, frameInRGB; |
| 89 | + |
| 90 | + // Camera loop, runs until we get flagStop == true |
| 91 | + for (int i=0; !flagStop ; ++i){ |
| 92 | + // Read next frame from camera |
| 93 | + cap.read(frameIn); |
| 94 | + if (frameIn.empty()) |
| 95 | + return absl::NotFoundError("CANNOT OPEN CAMERA !"); |
| 96 | + |
| 97 | + cout << "SIZE_IN = " << frameIn.size() << endl; |
| 98 | + { |
| 99 | + lock_guard<mutex> lock(mutexImshow); |
| 100 | + cv::imshow("frameIn", frameIn); |
| 101 | + } |
| 102 | + |
| 103 | + // Convert it to a packet and send |
| 104 | + cv::cvtColor(frameIn, frameInRGB, cv::COLOR_BGR2RGB); |
| 105 | + ImageFrame *inputFrame = new ImageFrame( |
| 106 | + ImageFormat::SRGB, frameInRGB.cols, frameInRGB.rows, ImageFrame::kDefaultAlignmentBoundary |
| 107 | + ); |
| 108 | + frameInRGB.copyTo(formats::MatView(inputFrame)); |
| 109 | + Timestamp ts(i); |
| 110 | + MP_RETURN_IF_ERROR(graph.AddPacketToInputStream("in", |
| 111 | + Adopt(inputFrame).At(ts) |
| 112 | + )); |
| 113 | + |
| 114 | + // Create a crop rect (center+width+height) for each frame |
| 115 | + // Let's move the rect up and down for fun |
| 116 | + int yc = int(frameIn.rows * (0.5 + 0.2 * cos(0.3 * i))); |
| 117 | + Rect rect; |
| 118 | + rect.set_width(0.8*frameIn.cols); |
| 119 | + rect.set_height(0.4*frameIn.rows); |
| 120 | + rect.set_x_center(frameIn.cols / 2); |
| 121 | + rect.set_y_center(yc); |
| 122 | + MP_RETURN_IF_ERROR(graph.AddPacketToInputStream("in_rect", MakePacket<Rect>(rect).At(ts))); |
| 123 | + } |
| 124 | + // Don't forget to close both input streams ! |
| 125 | + graph.CloseInputStream("in"); |
| 126 | + graph.CloseInputStream("in_rect"); |
| 127 | + // Wait for the graph to finish |
| 128 | + MP_RETURN_IF_ERROR(graph.WaitUntilDone()); |
| 129 | + return OkStatus(); |
| 130 | +} |
| 131 | + |
| 132 | +//============================================================================== |
| 133 | +int main(int argc, char** argv){ |
| 134 | + using namespace std; |
| 135 | + |
| 136 | + FLAGS_alsologtostderr = 1; |
| 137 | + google::SetLogDestination(google::GLOG_INFO, "."); |
| 138 | + google::InitGoogleLogging(argv[0]); |
| 139 | + |
| 140 | + cout << "Example 2.3 : Video pipeline with ImageCroppingCalculator (dynamic crop)" << endl; |
| 141 | + mediapipe::Status status = run(); |
| 142 | + cout << "status =" << status << endl; |
| 143 | + cout << "status.ok() = " << status.ok() << endl; |
| 144 | + return 0; |
| 145 | +} |
0 commit comments