⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processeffect.java

📁 能够检测到人脸
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        else
        //Check the angle between the nose bridge and the eyes line
        {
          if (slope < 0)
          {
            if (res[0] < point[0])
            {
              if (180 - angle > 90 + limit )
                fixNose = true;
            }
            else
            if (angle < 90 - limit)
              fixNose = true;
          }
          else
          if (slope > 0)
          {
            if (res[0] > point[0])
            {
              if (180 - angle > 90 + limit )
                fixNose = true;
            }
            else
            if (angle < 90 - limit)
              fixNose = true;
          }
          else
          if (slope == 0)
            if (angle < 90 - limit )
              fixNose = true;
        }
        //If required, correct nose tip position
        if (fixNose)
        {
          fixNose = false;
          angle = Math.abs(Math.atan(-slope));

          int x, y;
          y = (int) point[1] + (int) (Math.cos(angle) * (eyesLen / 2d));
          x = (int) point[0] + (int) (Math.sin(angle) * (eyesLen / 2d));

          if( frame.drawROI && frame.drawNose )
            outData = drawRect(x - sWid / 2, y - sHei / 2, sWid, sHei, outData);

          nosePixels = ImageProcessing.extractPixels(image, x - sWid / 2,
              y - sHei / 2, sWid, sHei, nosePixels);
          noseROI = ImageProcessing.toGrayscale(nosePixels, noseROI);
          noseTip = faceTracker.trackTemplate(noseTemplate,
                                                noseROI, sWid, tWid, sHei, tHei);

          x0 = (int) noseTip.getX();
          y0 = (int) noseTip.getY();
          nX = x - sWid / 2 + x0;
          nY = y - sHei / 2 + y0;
        }

        //ROI shift values are used to predict the user's movements on the x and y axis
        nxShift = nX-onX;
        nyShift = nY-onY;
        bxShift = bteX - obteX;
        byShift = bteY - obteY;
        trbtex = rX - bteX;
        trbtey = rY - bteY;
        tlbtex = lX - bteX;
        tlbtey = lY - bteY;

        //Calculate transition and scale between nose tip movement and mouse movement
        int xDist = nX-160;
        int yDist = nY-120;

        xDist *= frame.xScale;
        yDist *= frame.yScale;

        //Move the mouse to the given location ( nose pointed )
        if( frame.enable )
          robot.mouseMove(centerX+xDist,centerY+yDist);

        //Extract final nose tip template
        for (int y = 0; y < tHei; y++)
          for (int x = 0; x < tWid; x++)
            noseTemplate[x][y] = noseROI[ (y0 - tHei / 2 + y) * sWid +
                (x0 - tWid / 2 + x)];

        //Extract final BTE template
        for (int y = 0; y < tHei; y++)
          for (int x = 0; x < tWid; x++)
            BteTemplate[x][y] = BteROI[ (y1 - tHei / 2 + y) * sWid +
                (x1 - tWid / 2 + x)];

        //Overlay video with the locations of the found features
        if( frame.drawNose )
          outData = drawRect(nX - tWid / 2, nY - tHei / 2, tWid, tHei, outData);
        if( frame.drawBte )
          outData = drawRect(bteX - tWid / 2, bteY - tHei / 2, tWid, tHei,
                             outData);
        if( frame.drawEyes )
        {
          outData = drawRect(lX - 7, lY - 5, 15, 11, outData);
          outData = drawRect(rX - 7, rY - 5, 15, 11, outData);
        }
      }

      //If detect button was clicked
      if( frame.detectFaces )
      {
        frame.detectFaces = false;
        fixNose = false;

        //Create image from frame
        if( bti == null )
          bti = new BufferToImage((VideoFormat)inBuffer.getFormat());
        image = bti.createImage(inBuffer);

        //Find eyes and nose coordinates
        int coordinates[] = faceDetector.detectAllFaces(image);

        //If nose and eyes were found
        if (coordinates != null)
        {
          frame.track = true;

          //meaningless initialization values
          lt = -1;
          rt = -5;

          int[] grayPixels = faceDetector.grayPixels;

          //Extract eyes and nose coordinates
          lX = coordinates[0];
          lY = coordinates[1];
          rX = coordinates[2];
          rY = coordinates[3];
          nX = coordinates[4];
          nY = coordinates[5];
          coordinates = null;

          double eyesLen = Math.sqrt(Math.pow(lX-rX,2)+Math.pow(lY-rY,2));
          double slope = (rX-lX != 0 ? (double)(rY-lY) / (double)(rX-lX) : Math.tan(Math.toRadians(-90)));

          leftBrow = faceTracker.findEyeBrowsLine(lX,lY,rX,rY,image,left,eyesLen,slope,frame.brThreshold);
          if( leftBrow != null )
          {
            int lsx = leftBrow[0];
            int lsy = leftBrow[1];
            int lex = leftBrow[2];
            int ley = leftBrow[3];
            int sx = leftBrow[4];
            int sy = leftBrow[5];
            if( frame.drawBrows )
              outData = drawLine(sx+lsx,sy+lsy,sx+lex,sy+ley,outData);
          }

          rightBrow = faceTracker.findEyeBrowsLine(lX,lY,rX,rY,image,right,eyesLen,slope,frame.brThreshold);
          if( rightBrow != null )
          {
            int lsx = rightBrow[0];
            int lsy = rightBrow[1];
            int lex = rightBrow[2];
            int ley = rightBrow[3];
            int sx = rightBrow[4];
            int sy = rightBrow[5];
            if( frame.drawBrows )
              outData = drawLine(sx+lsx,sy+lsy,sx+lex,sy+ley,outData);
          }

          //Locate BTE coordinates
          int xLen = rX - lX;
          int yLen = rY - lY;
          double step = (yLen != 0 ? (double)xLen / (double)yLen : 0);
          bteX = lX + xLen / 2;
          bteY = (step != 0 ? lY + (int)(( (xLen / 2) / step)) : lY);

          //Initialize ROIs' shift values
          nxShift = nyShift = bxShift = byShift = 0;
          trbtex = rX-bteX;
          trbtey = rY-bteY;
          tlbtex = lX-bteX;
          tlbtey = lY-bteY;

          //Extract first BTE and nose tip templates
          int tx,ty;
          for (int y = 0; y < tHei; y++)
            for (int x = 0; x < tWid; x++)
            {
              tx = nxShift+nX - tWid / 2 + x;
              ty = nyShift+nY - tHei / 2 + y;
              if( (tx >= 0) && (tx < 320) && (ty >= 0) && (ty < 240) )
                noseTemplate[x][y] = grayPixels[ ty * 320 + tx ];
              else
                noseTemplate[x][y] = 0;

              tx = bxShift+bteX - tWid / 2 + x;
              ty = byShift+bteY - tHei / 2 + y;
              if( (tx >= 0) && (tx < 320) && (ty >= 0) && (ty < 240) )
                BteTemplate[x][y] = grayPixels[ ty * 320 + tx ];
              else
                BteTemplate[x][y] = 0;

              tx = tlbtex+lX - tWid / 2 + x;
              ty = tlbtey+lY - tHei / 2 + y;
              if ( (tx >= 0) && (tx < 320) && (ty >= 0) && (ty < 240))
                lTemplate[x][y] = grayPixels[ty * 320 + tx];
              else
                lTemplate[x][y] = 0;

              tx = trbtex+rX - tWid / 2 + x;
              ty = trbtey+rY - tHei / 2 + y;
              if ( (tx >= 0) && (tx < 320) && (ty >= 0) && (ty < 240))
                rTemplate[x][y] = grayPixels[ty * 320 + tx];
              else
                rTemplate[x][y] = 0;
            }

          //Overlay video with nose and eyes locations
          if( frame.drawNose )
            outData = drawRect(nxShift+nX - tWid / 2, nyShift+nY - tHei / 2, tWid, tHei, outData);
          if( frame.drawEyes )
          {
            outData = drawRect(tlbtex+lX - tWid / 2, tlbtey+lY - tHei / 2, tWid, tHei, outData);
            outData = drawRect(trbtex+rX - tWid / 2, trbtey+rY - tHei / 2, tWid, tHei, outData);
          }

        }
      }
      outBuffer.setData(outData);

      //Copy the input attributes to the output
      outBuffer.setFormat(inBuffer.getFormat());
      outBuffer.setLength(inBuffer.getLength());
      outBuffer.setOffset(inBuffer.getOffset());
      return BUFFER_PROCESSED_OK;
    }

    //////////////////////////////

    private byte[] drawRect(int x0,int y0,int width,int height,byte[] data)
    {
      int pos;
      //draw first horizontal line
      for(int x=x0 ; x<x0+width ; x++)
      {
        pos = (240-y0)*960+(x)*3;
        if( (pos>=0) && (pos+2<data.length) )
        {
          data[pos] = blue;
          pos++;
          data[pos] = green;
          pos++;
          data[pos] = red;
        }
        else
          break;
      }
      //draw second horizontal line
      for(int x=x0 ; x<x0+width ; x++)
      {
        pos = (240-(y0+height))*960+(x)*3;
        if( (pos>=0) && (pos+2<data.length) )
        {
          data[pos] = blue;
          pos++;
          data[pos] = green;
          pos++;
          data[pos] = red;
        }
        else
          break;
      }
      //draw first vertical line
      for(int y=y0 ; y<y0+height ; y++)
      {
        pos = (240-y)*960+(x0)*3;
        if( (pos>=0) && (pos+2<data.length) )
        {
          data[pos] = blue;
          pos++;
          data[pos] = green;
          pos++;
          data[pos] = red;
        }
        else
          break;
      }
      //draw second vertical line
      for(int y=y0 ; y<y0+height ; y++)
      {
        pos = (240-y)*960+(x0+width)*3;
        if( (pos>=0) && (pos+2<data.length) )
        {
          data[pos] = blue;
          pos++;
          data[pos] = green;
          pos++;
          data[pos] = red;
        }
        else
          break;
      }
      return data;
    }

    //////////////////////////////

    private byte[] drawLine(int sx,int sy,int ex,int ey,byte data[])
    {
      double xLen = ex-sx;
      double yLen = ey-sy;
      double step = (yLen != 0 ? xLen / yLen : xLen);
      step = ( (int)step == 0 ? 1 : (int)step );
      double slope = (xLen != 0 ? yLen/xLen : Math.tan(Math.toRadians(-90)));
      slope = (slope < 0 ? -1 : 1);
      int pos;
      for(int x=sx ; x<ex ; x++)
      {
        if (x % (int)step == 0)
          sy += slope;
        pos = (240-sy)*960+(x)*3;
        if( (pos>=0) && (pos+2<data.length) )
        {
          data[pos] = blue;
          pos++;
          data[pos] = green;
          pos++;
          data[pos] = red;
        }
        else
          break;
      }
      return data;
    }

    //////////////////////////////

    //methods for interface : Codec
    public Format[] getSupportedInputFormats()
    {
        return formats;
    }

    public Format [] getSupportedOutputFormats(Format input)
    {
        if (input == null)
        {
          System.out.println("getSupportedOutputFormats input == null");
          return formats;
        }
        if (matches(input, formats) != null)
          return new Format[] { formats[0].intersects(input) };
        else
          return new Format[0];
    }

    public Format setInputFormat(Format input)
    {
        inputFormat = input;
        return input;
    }

    public Format setOutputFormat(Format output)
    {
        if (output == null || matches(output, formats) == null)
            return null;

        RGBFormat incoming = (RGBFormat)output;
        Dimension size = incoming.getSize();
        int maxDataLength = incoming.getMaxDataLength();
        int lineStride = incoming.getLineStride();
        float frameRate = incoming.getFrameRate();
        int flipped = incoming.getFlipped();
        int endian = incoming.getEndian();

        if (size == null)
            return null;
        if (maxDataLength < size.width * size.height * 3)
            maxDataLength = size.width * size.height * 3;
        if (lineStride < size.width * 3)
            lineStride = size.width * 3;
        if (flipped != Format.FALSE)
            flipped = Format.FALSE;

        outputFormat = formats[0].intersects(new RGBFormat(size,
                                                        maxDataLength,
                                                        null,
                                                        frameRate,
                                                        Format.NOT_SPECIFIED,
                                                        Format.NOT_SPECIFIED,
                                                        Format.NOT_SPECIFIED,
                                                        Format.NOT_SPECIFIED,
                                                        Format.NOT_SPECIFIED,
                                                        lineStride,
                                                        Format.NOT_SPECIFIED,
                                                        Format.NOT_SPECIFIED));

        return outputFormat;
    }

    //methods for interface : PlugIn
    public String getName() {
        return "Process Effect";
    }

    public void open() {
    }

    public void close() {
    }

    public void reset() {
    }

    //methods for interface : javax.media.Controls
    public Object getControl(String controlType) {
        return null;
    }

    public Object[] getControls() {
        return null;
    }


    //Utility methods
    Format matches(Format in, Format outs[])
    {
      for (int i = 0; i < outs.length; i++)
      {
        if (in.matches(outs[i]))
          return outs[i];
      }
      return null;
    }





}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -