From ab5e941dcaedcf2cc896eed1ea257c59780fdb59 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 17 Aug 2021 12:38:14 -0400 Subject: Implemented parts of mouse click event --- src/ui.cc | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'src/ui.cc') diff --git a/src/ui.cc b/src/ui.cc index f852bde..9b24815 100644 --- a/src/ui.cc +++ b/src/ui.cc @@ -73,29 +73,52 @@ namespace frontend { return false; } + backend::label* getCurrentRectLabel(ui_impl *data) { + auto labs = data->llr->getEditableLabels(); + for(auto rit = labs.rbegin(); rit != labs.rend(); rit++) { + if(rit->location.nonzero()) { + return &(*rit); + } + } + return nullptr; + } + void mouseCallback(int event, int x, int y, int flags, void* userdata) { // userdata is actually a &shared_ptr - shared_ptr data = (shared_ptr) *userdata; + auto data = static_cast(userdata); static bool mouseDown = false; // We're interested in left button down/up and movement when down if(event == cv::EVENT_LBUTTONDOWN) { // If the current frame doesn't have a rectangle label, then make one mouseDown = true; - double time = data->pb->getTime() - auto labs = data->llr->getSurrounding(time); - backend::label current; - if(labs.first.time == time) { - current = labs.first; - } else if(labs.second.time == time) { - current = labs.second; - } else { - + double time = data->pb->getTime(); + auto lab = getCurrentRectLabel(data); + if(lab && lab->time == time) { + // Use this one + } else { // Make one + // Try to use the first rectangle annotation, fall back on normal, then "Rectangle" + std::string name = "Rectangle"; + if(!data->llr->getRectangleLabels().empty()) { + name = data->llr->getRectangleLabels()[0]; + } else if(!data->llr->getLabels().empty()) { + name = data->llr->getLabels()[0]; + } + data->llr->applyLabel(name, time); + lab = &data->llr->getEditableLabels().back(); } + lab->location.x1 = x; + lab->location.y1 = y; } else if(event == cv::EVENT_LBUTTONUP) { // Mouse is no longer down mouseDown = false; } else if(event == cv::EVENT_MOUSEMOVE) { // If the mouse is down, update x2 and y2 of current window + if(mouseDown) { + auto lab = getCurrentRectLabel(data); // It will be valid! TODO: Will it? + std::cout << "Updating location from " << lab->location.x2 << ", " << lab->location.y2 << " to " << x << ", " << y << "\n"; + lab->location.x2 = x; + lab->location.y2 = y; + } } } @@ -111,9 +134,10 @@ namespace frontend { ioctl(STDOUT_FILENO, TIOCGWINSZ, &size); int cols = size.ws_col; // Register mouse callback - cv::setMouseCallback("Video", mouseCallback, &data); + data->pb->display("Video", (getCurrentRectLabel(data.get()))? getCurrentRectLabel(data.get())->location : backend::rect()); + cv::setMouseCallback("Video", mouseCallback, data.get()); while(true) { - data->pb->display("Video"); + data->pb->display("Video", (getCurrentRectLabel(data.get()))? getCurrentRectLabel(data.get())->location : backend::rect()); data->pb->interFrameSleep(); if(handleKey(cv::pollKey(), data)) { break; -- cgit v1.2.3