@Override
 public void run() {
   for (MBFImage image : this.stream) {
     FImage gimage = Transforms.calculateIntensityNTSC(image);
     //			gimage.processInplace(this.thresholder);
     gimage.threshold(0.7f);
     displayFrame(gimage);
     //			trackInputs(image);
     //			handleInputs(image);
   }
 }
  public static void extractImage(
      String rootDir,
      String scaledDir,
      long subjectId,
      long imageId,
      String fileName,
      int haarMinSize,
      int scaledSize)
      throws IOException {
    FileInputStream input = new FileInputStream(fileName);
    MBFImage image = ImageUtilities.readMBF(input);
    BufferedImage detectedFacesImage = ImageIO.read(new File(fileName));
    // ImageUtils.displayImage(detectedFacesImage);
    FaceDetector<DetectedFace, FImage> fd = new HaarCascadeDetector(haarMinSize);
    List<DetectedFace> faces = fd.detectFaces(Transforms.calculateIntensity(image));
    System.out.println("# Found faces, one per line.");
    System.out.println("# <x>, <y>, <width>, <height>");
    Iterator<DetectedFace> iterator = faces.iterator();
    BufferedImage extractFaceImage = null;
    if (iterator.hasNext()) {
      DetectedFace face = iterator.next();
      Rectangle bounds = face.getBounds();
      //            extractFaceImage = detectedFacesImage.getSubimage((int)bounds.x, (int)bounds.y,
      // (int)bounds.width, (int)bounds.height);
      //            writeImage(extractFaceImage, rootDir, "extracted", subjectId, imageId);

      //            BufferedImage scaledBufferedImage = scale(extractFaceImage, scaledSize);
      //            String scaledFileName = scaledDir + subjectId + "-" + imageId + ".jpg";
      //            System.out.println("scaledFileName = " + scaledFileName);
      //            ImageIO.write(scaledBufferedImage, "jpg", new File(scaledFileName));
      Graphics g = detectedFacesImage.createGraphics();
      g.setColor(Color.GREEN);
      g.drawRect((int) bounds.x, (int) bounds.y, (int) bounds.width, (int) bounds.height);
      System.out.println(bounds.x + ";" + bounds.y + ";" + bounds.width + ";" + bounds.height);
    } else {
      Graphics g = detectedFacesImage.createGraphics();
      g.setColor(Color.GREEN);
      g.drawString("No Image Detected", 20, 20);
    }
    writeImage(detectedFacesImage, rootDir, "detected", subjectId, imageId);
    //
    //        FaceDetector < KEDetectedFace , FImage > fdK = new FKEFaceDetector () ;
    //        List < KEDetectedFace > facesK = fdK.detectFaces ( Transforms.calculateIntensity (
    // image ) ) ;
    //        if (!facesK.isEmpty()) {
    //        	System.out.println("Found face");
    //        	KEDetectedFace detectedKeyFace = facesK.get(0);
    //        	FacialKeypoint[] keypoints = detectedKeyFace.getKeypoints();
    //        	for (FacialKeypoint keypoint: keypoints) {
    //        		System.out.println("keypoint:" + keypoint);
    //        	}
    //        }

  }
  /**
   * {@inheritDoc}
   *
   * @see org.openimaj.video.VideoDisplayListener#beforeUpdate(org.openimaj.image.Image)
   */
  @Override
  public void beforeUpdate(MBFImage frame) {
    if (edgeDetect) frame.processInplace(new CannyEdgeDetector2());

    if (faceDetect) {
      HaarCascadeDetector d = new HaarCascadeDetector(100);
      List<DetectedFace> faces = d.detectFaces(Transforms.calculateIntensityNTSC(frame));

      for (DetectedFace face : faces) {
        Shape transBounds = face.getBounds();
        MBFImageRenderer renderer = frame.createRenderer();
        renderer.drawPolygon(transBounds.asPolygon(), RGBColour.RED);
      }
    }

    if (faceKPDetect) {
      System.out.println("HERE");
      FKEFaceDetector fkp =
          new FKEFaceDetector(HaarCascadeDetector.BuiltInCascade.frontalface_alt.load());
      List<KEDetectedFace> faces = fkp.detectFaces(Transforms.calculateIntensityNTSC(frame));

      for (KEDetectedFace face : faces) {
        Shape transBounds = face.getBounds();
        MBFImageRenderer renderer = frame.createRenderer();
        renderer.drawPolygon(transBounds.asPolygon(), RGBColour.RED);

        for (FacialKeypoint kp : face.getKeypoints()) {
          Point2d pt = kp.position.clone();
          pt.translate((float) transBounds.minX(), (float) transBounds.minY());
          renderer.drawPoint(pt, RGBColour.GREEN, 3);
        }
      }
    }

    if (moustache)
      try {
        frame.internalAssign(new Mustache().addMustaches(frame));
      } catch (IOException e) {
        e.printStackTrace();
      }
  }