Ejemplo n.º 1
0
ishara::ishara(QWidget *parent) : QMainWindow(parent), ui(new Ui::ishara) {

  if (QSystemTrayIcon::isSystemTrayAvailable()) {
      /*
       * init system tray
       */
      maximizeAction = new QAction(tr("&Show"), this);
      connect(maximizeAction, SIGNAL(triggered()), this, SLOT(show()));
      quitAction = new QAction(tr("&Quit"), this);
      connect(quitAction, SIGNAL(triggered()), this, SLOT(on_actionQuit_triggered()));

      startStopAction = new QAction(tr("S&tart"), this);
      connect(startStopAction, SIGNAL(triggered()), this, SLOT(startStop()));

      trayIconMenu = new QMenu(this);
      trayIconMenu->addAction(maximizeAction);
      trayIconMenu->addSeparator();
      trayIconMenu->addAction(startStopAction);
      trayIconMenu->addSeparator();
      trayIconMenu->addAction(quitAction);
      trayIcon = new QSystemTrayIcon(this);
      trayIcon->setContextMenu(trayIconMenu);

      connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
      const QIcon *icon = new QIcon(":/prefix1/res/ishara.ico");
      trayIcon->setIcon(*icon);
      trayIcon->hide();
    }

  /*
   * initializing some variables from the configuration file
   */
  settings.sync();

  hMin1 = settings.value("hMin1", 0).toInt();
  hMax1 = settings.value("hMax1", 179).toInt();
  sMin1 = settings.value("sMin1", 0).toInt();
  sMax1 = settings.value("sMax1", 255).toInt();
  vMin1 = settings.value("vMin1", 0).toInt();
  vMax1 = settings.value("vMax1", 255).toInt();

  hMin2 = settings.value("hMin2", 0).toInt();
  hMax2 = settings.value("hMax2", 179).toInt();
  sMin2 = settings.value("sMin2", 0).toInt();
  sMax2 = settings.value("sMax2", 255).toInt();
  vMin2 = settings.value("vMin2", 0).toInt();
  vMax2 = settings.value("vMax2", 255).toInt();

  pinchR = settings.value("pinchR", 66).toInt();
  rightClickDealy = settings.value("rightClickDealy", 20).toInt();
  smoothFac = settings.value("smoothFac", 8).toInt();

  cfgScroll = settings.value("cfgScroll", 2).toInt();
  cfgLClick = settings.value("cfgLClick", 2).toInt();
  cfgRClick = settings.value("cfgRClick", 2).toInt();

  /*
   * initializing some more variables
   */
  CAM_INDEX = -1;
  mcorInit_X = 0;
  mcorInit_Y = 0;
  msPoint_X = 0;
  msPoint_Y = 0;
  xScreenHeight = 0;
  xScreenWidth = 0;
  startEmulation = 0;
  motionEnable = 0;
  tmpX = 0;
  tmpY = 0;
  waitCountRC = 0;
  pinch = 0;
  ifScrollUp = 0;
  ifScrollDwn = 0;
  btnPress = 1;
  btnRel = 1;
  devSelActive = 0;
  iteration = 0;

  /*
   * setting up the user interface
   */
  ui->setupUi(this);

  /*
   * setting up smoothness factor slider; smoothness factor is the maximum allowable
   * distance the index finger can move without effecting any change in the mouse
   * cursor position; a reasonable value for this would be 6 to 8 depending on the user
   */
  ui->sliderSmoothFac->setRange(2, 12);
  ui->sliderSmoothFac->setValue(smoothFac);

  /*
   * setting up color selection sliders; the HSV range for the two color markers to be
   * detected are set using these sliders
   */
  ui->sliderHMin1->setRange(0, 179);
  ui->sliderHMax1->setRange(0, 179);
  ui->sliderSMin1->setRange(0, 255);
  ui->sliderSMax1->setRange(0, 255);
  ui->sliderVMin1->setRange(0, 255);
  ui->sliderVMax1->setRange(0, 255);

  ui->sliderHMin2->setRange(0, 179);
  ui->sliderHMax2->setRange(0, 179);
  ui->sliderSMin2->setRange(0, 255);
  ui->sliderSMax2->setRange(0, 255);
  ui->sliderVMin2->setRange(0, 255);
  ui->sliderVMax2->setRange(0, 255);

  ui->sliderHMin1->setValue(hMin1);
  ui->sliderHMax1->setValue(hMax1);
  ui->sliderSMin1->setValue(sMin1);
  ui->sliderSMax1->setValue(sMax1);
  ui->sliderVMin1->setValue(vMin1);
  ui->sliderVMax1->setValue(vMax1);

  ui->sliderHMin2->setValue(hMin2);
  ui->sliderHMax2->setValue(hMax2);
  ui->sliderSMin2->setValue(sMin2);
  ui->sliderSMax2->setValue(sMax2);
  ui->sliderVMin2->setValue(vMin2);
  ui->sliderVMax2->setValue(vMax2);

  /*
   * setting up color selection spinboxes; the values determining the HSV range can
   * also be altered using the spin boxes
   */
  ui->spnHMin1->setRange(0, 179);
  ui->spnHMax1->setRange(0, 179);
  ui->spnSMin1->setRange(0, 255);
  ui->spnSMax1->setRange(0, 255);
  ui->spnVMin1->setRange(0, 255);
  ui->spnVMax1->setRange(0, 255);

  ui->spnHMin2->setRange(0, 179);
  ui->spnHMax2->setRange(0, 179);
  ui->spnSMin2->setRange(0, 255);
  ui->spnSMax2->setRange(0, 255);
  ui->spnVMin2->setRange(0, 255);
  ui->spnVMax2->setRange(0, 255);

  ui->spnHMin1->setValue(hMin1);
  ui->spnHMax1->setValue(hMax1);
  ui->spnSMin1->setValue(sMin1);
  ui->spnSMax1->setValue(sMax1);
  ui->spnVMin1->setValue(vMin1);
  ui->spnVMax1->setValue(vMax1);

  ui->spnHMin2->setValue(hMin2);
  ui->spnHMax2->setValue(hMax2);
  ui->spnSMin2->setValue(sMin2);
  ui->spnSMax2->setValue(sMax2);
  ui->spnVMin2->setValue(vMin2);
  ui->spnVMax2->setValue(vMax2);

  /*
   * setting up pinchR slider; the pinchR variable determines the maximum distance
   * between the center co-ordinates of the two color markers when detecting a Pinch
   */
  ui->sliderPinchR->setRange(10, 120);
  ui->sliderPinchR->setValue(pinchR);

  /*
   * setting up pinchR spinbox; the value of pinchR can also be altered using a spinbox
   */
  ui->spnPinchR->setRange(10, 120);
  ui->spnPinchR->setValue(pinchR);

  /*
   * setting up rightClickDealy slider; the variables rightClickDealy
   * determines the loop count while determining a right click
   */
  ui->sliderRCRC->setRange(10, 30);
  ui->sliderRCRC->setValue(rightClickDealy);

  /*
   * setting up rightClickDealy spinbox, the usage is obvious
   */
  ui->spnRCRC->setRange(10, 30);
  ui->spnRCRC->setValue(rightClickDealy);

  /*
   * setting up check boxes to enable/disable functionalities
   */
  if(cfgScroll > 0) {
      ui->chkEnableScroll->setChecked(true);
    }
  else {
      ui->chkEnableScroll->setChecked(false);
    }

  if(cfgLClick > 0) {
      ui->chkEnableLeftClick->setChecked(true);
    }
  else {
      ui->chkEnableLeftClick->setChecked(false);
    }

  if(cfgRClick > 0) {
      ui->chkEnableRightClick->setChecked(true);
    }
  else {
      ui->chkEnableRightClick->setChecked(false);
    }

  /*
   * camera selection in Linux
   */
  for(deviceIndex = 0 ; deviceIndex < 64 ; ++deviceIndex) {
      QString device = "/dev/video" + QString::number(deviceIndex);
      QByteArray array = device.toLocal8Bit();
      char *buffer = array.data();
      if((fd = open(buffer, O_RDONLY)) != -1) {
          if(ioctl(fd, VIDIOC_QUERYCAP, &vidiocap) == -1) {
              ui->comboSelectCam->addItem(device);
            }
          else {
              ui->comboSelectCam->addItem(QString::fromLocal8Bit(reinterpret_cast<const char*>(vidiocap.card)));
            }
          if(CAM_INDEX == -1) {
              CAM_INDEX = deviceIndex;
            }
        }
    }

  /*
   * camera operations
   */
  camOpen();

  capture.read(src);
  if(src.empty() == true) {
      ui->statusBar->showMessage("Read error!");
      return;
    }

  /*
   * get the screen size
   */
  getxScreenSize();

  /*
   * time warp - make it so
   */
  timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), this, SLOT(processFrameAndUpdateGUI()));
  timer->start(20);

  /*
   * clean up the crap
   */
  src.release();
  frame.release();
  imgHSV.release();
}
Ejemplo n.º 2
0
void ishara::processFrameAndUpdateGUI() {
    cv::Mat imgThresh1;
    cv::Mat imgThresh2;

    int pos_x1 = 0;
    int pos_y1 = 0;
    int pos_x2 = 0;
    int pos_y2 = 0;

    hMin1 = ui->sliderHMin1->value();
    hMax1 = ui->sliderHMax1->value();
    sMin1 = ui->sliderSMin1->value();
    sMax1 = ui->sliderSMax1->value();
    vMin1 = ui->sliderVMin1->value();
    vMax1 = ui->sliderVMax1->value();

    hMin2 = ui->sliderHMin2->value();
    hMax2 = ui->sliderHMax2->value();
    sMin2 = ui->sliderSMin2->value();
    sMax2 = ui->sliderSMax2->value();
    vMin2 = ui->sliderVMin2->value();
    vMax2 = ui->sliderVMax2->value();

    smoothFac = ui->sliderSmoothFac->value();
    pinchR = ui->sliderPinchR->value();
    rightClkCount = ui->sliderRCRC->value();

    capture.read(src);

    /**
     * Flip the initial image and convert to RGB as opencv would read images in BGR format.
     */
    cv::flip(src, frame, 1);
    cv::cvtColor(frame, frame, CV_BGR2RGB);

    /**
     * Converting the image to HSV and applying Gaussian Blur filter to reduce some noise.
     */
    cv::cvtColor(frame, imgHSV, CV_BGR2HSV);
    cv::GaussianBlur(imgHSV, imgHSV, cv::Size(11, 11), 0);

    /**
     * Identifying the color markers used in the index finger.
     */
    cv::inRange(imgHSV,
                cv::Scalar(hMin1, sMin1, vMin1),
                cv::Scalar(hMax1, sMax1, vMax1),
                imgThresh1);
    trackObject(&imgThresh1, &pos_x1, &pos_y1);

    /**
     * Identifying the color markers used in the index thumb.
     */
    cv::inRange(imgHSV,
                cv::Scalar(hMin2, sMin2, vMin2),
                cv::Scalar(hMax2, sMax2, vMax2),
                imgThresh2);
    trackObject(&imgThresh2, &pos_x2, &pos_y2);

    mcorInit_X = pos_x1;
    mcorInit_Y = pos_y1;

    if(mcorInit_X + mcorInit_Y) {
        if(startEmulation == 1) {
            Display *display = XOpenDisplay(0);
            if(!display) {
                ui->statusBar->showMessage("Error opening display!");
                return;
            }

            Window root = DefaultRootWindow(display);
            if(!root) {
                ui->statusBar->showMessage("Root window not found!");
                return;
            }

            /**
             * Map and move mouse pointer on screen.
             */
            getxScreenSize();
            mouseMap();
            XTestFakeMotionEvent(display, DefaultScreen(display), mcorFinal_X, mcorFinal_Y, 0);

            if(pos_x2 + pos_y2) {
                if(tmpX == mcorFinal_X && tmpY == mcorFinal_Y)
                    ++waitCount;
                else {
                    tmpX = 0;
                    tmpY = 0;
                    waitCount = 0;
                }

                /**
                 * Right click.
                 */
                if(ui->chkEnableRightClick->isChecked() == true) {
                    if(waitCount == rightClkCount && (ifScrollUp != 1 && ifScrollDwn != 1) && pinch != 1) {
                        XTestFakeButtonEvent(display, 3, 1, 1);
                        XTestFakeButtonEvent(display, 3, 0, 1);
                        tmpX = 0;
                        tmpY = 0;
                        waitCount = 0;
                    }
                }

                tmpX = mcorFinal_X;
                tmpY = mcorFinal_Y;

                preClick(&pos_x2, &pos_y2);

                /**
                 * Scroll.
                 */
                if(ui->chkEnableScroll->isChecked() == true) {
                    scrollInit(&pos_x2, &pos_y2);
                    if(ifScrollUp == 1) {
                        XTestFakeButtonEvent(display, 4, 1, 10);
                        XTestFakeButtonEvent(display, 4, 0, 10);
                    }
                    if(ifScrollDwn == 1) {
                        XTestFakeButtonEvent(display, 5, 1, 10);
                        XTestFakeButtonEvent(display, 5, 0, 10);
                    }
                }

                /**
                 * Left click.
                 */
                if(ui->chkEnableLeftClick->isChecked() == true) {
                    cv::circle(frame, cv::Point(mcorInit_X, mcorInit_Y), pinchR, cv::Scalar(0, 0, 255));
                    if(pinch == 1) {
                        if(btnPress == 1) {
                            XTestFakeButtonEvent(display, 1, 1, 1);
                            btnPress = 0;
                            btnRel = 1;
                        }
                    }
                    else{
                        if(btnRel == 1) {
                            XTestFakeButtonEvent(display, 1, 0, 1);
                            btnRel = 0;
                        }
                        else
                            btnPress = 1;
                    }
                }
            }

            XFlush(display);
            XCloseDisplay(display);
        }
    }

    /**
     * Display images.
     */
    QImage qImgDisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
    ui->lblDisplayClean->setPixmap(imgDisplay.fromImage(qImgDisplay).scaledToHeight(480, Qt::FastTransformation));

    QImage qImgDisplayThresh1((uchar*)imgThresh1.data, imgThresh1.cols, imgThresh1.rows, imgThresh1.step, QImage::Format_Indexed8);
    ui->lblConfigureLeft->setPixmap(imgDisplay.fromImage(qImgDisplayThresh1).scaledToHeight(236, Qt::FastTransformation));

    QImage qImgDisplayThresh2((uchar*)imgThresh2.data, imgThresh2.cols, imgThresh2.rows, imgThresh2.step, QImage::Format_Indexed8);
    ui->lblConfigureRight->setPixmap(imgDisplay.fromImage(qImgDisplayThresh2).scaledToHeight(236, Qt::FastTransformation));

    /**
     * Clean up the crap.
     */
    imgThresh1.release();
    imgThresh2.release();
}