CrazyEngineers
  • Search image by train subfloders - Face Recognition Project

    deepz

    Member

    Updated: Oct 22, 2024
    Views: 966
    Hi,
    My project is face recognition.i want search a image by train subfloders.i have attached my coding.In where i have to modify to that code(searching a image in train subfloder)

    import java.lang.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class MyFaceRecog extends JFrame implements ActionListener
    {
    JFrame frmMain=new JFrame("Dictionary-based Face Recognition");
    JLabel lblTestPath=new JLabel("Input Test Image:");
    JTextField txtTestPath=new JTextField("");
    JButton btRecognize=new JButton("Recognize");
    JButton bt = new JButton("select file");
    JLabel lblResult=new JLabel("Result:");
    JFrame frmImage2=new JFrame("Matched Image");
    JTextArea txtResult=new JTextArea("");
    JScrollPane spResult=new JScrollPane(txtResult);
    JFrame frmImage1=new JFrame("Input Image");
    JFileChooser chooser = new JFileChooser();
    String loc;
    int MaxFaceIndex=17;
    double DifferenceThreshold=15000.0;
    int NumFaces;
    int MaxFaces=100;
    int FaceTemplate[][];
    String fac;
    int Faces[][][];
    String[] a;
     
    int LaplacianFaces[][][];
    String FaceFileNames[];
    String tResult="";
    public MyFaceRecog()
    {
    frmMain.setDefaultLookAndFeelDecorated(true);
    frmMain.setResizable(false);
    frmMain.setSize(600, 400);
    frmMain.getContentPane().setLayout(null);
    lblTestPath.setBounds(17,15,100,20);
    frmMain.getContentPane().add(lblTestPath,BorderLayout.WEST);
    txtTestPath.setBounds(15,35,170,20);
    frmMain.getContentPane().add(txtTestPath, BorderLayout.AFTER_LAST_LINE);
    lblResult.setBounds(17,65,100,20);
    frmMain.getContentPane().add(lblResult);
    spResult.setBounds(15,85,280,120);
    frmMain.getContentPane().add(spResult);
    txtResult.setEditable(false);
    btRecognize.setBounds(193,35,100,20);
    btRecognize.addActionListener(this);
    bt.setBounds(400, 35, 100, 20);
    bt.addActionListener(this);
    frmMain.getContentPane().add(btRecognize);
    frmMain.getContentPane().add(bt);
    frmImage1.setDefaultLookAndFeelDecorated(true);
    frmImage1.setResizable(false);
    frmImage1.setBounds(450,100,200,150);
    frmImage2.setDefaultLookAndFeelDecorated(true);
    frmImage2.setResizable(false);
    frmImage2.setBounds(670,100,200,150);
    frmImage1.setVisible(true);
    frmImage2.setVisible(true);
    frmMain.setVisible(true);
    }
     
     
    public void actionPerformed(ActionEvent evt)
    {
     
    if(evt.getSource()==bt)
    {
    int returnVal = chooser.showOpenDialog(MyFaceRecog.this);
     
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    loc=file.getPath();
    System.out.println("Loc is "+loc);
    txtTestPath.setText(loc);
     
    }
    }
    if(evt.getSource()==btRecognize)
    {
    System.out.println("Inside If");
    if(new File(txtTestPath.getText()).exists()==false)
    {
    JOptionPane.showMessageDialog(null,"Test Image, not found.");
    return;
    }
     
    drawImage(frmImage1.getGraphics(),txtTestPath.getText());
     
    tResult="";
    txtResult.setText(tResult);
    train();
    test();
    }
     
    }
     
    void drawImage(Graphics g,String tPath)
    {
    PGM tpgm=new PGM();
    tpgm.setFilePath(tPath);
    tpgm.readImage();
     
    g.clearRect(0,0,200,150);
    for(int r=0;r<tpgm.getRows();r++)
    {
    for(int c=0;c<tpgm.getCols();c++)
    {
    int intensity=tpgm.getPixel(r,c);
    Color color=new Color(intensity,intensity,intensity);
    g.setColor(color);
    g.fillRect(c,r+30,1,1);
    }
    }
     
    }
     
     
     
    public void addResultText(String tStr)
    {
    tResult=tResult+tStr;
    txtResult.setText(tResult);
    }
     
    public void train()
    {
    int xBase,yBase,xSub,ySub;
    int xLow,xHigh,yLow,yHigh;
    int GrayLevel;
    int CellSum,CellAvg;
    int i,j;
    int xDiv,yDiv;
    int BlockWidth,BlockHeight;
    int StartX,StartY;
    int SizeX,SizeY;
     
    SizeX=80;
    SizeY=80;
    xDiv=20;
    yDiv=20;
    BlockWidth=SizeX/xDiv;
    BlockHeight=SizeY/yDiv;
    FaceTemplate=new int[xDiv][yDiv];
    Faces=new int[xDiv][yDiv][MaxFaces];
    LaplacianFaces=new int[xDiv][yDiv][MaxFaces];
    FaceFileNames=new String[MaxFaces];
    NumFaces=0;
    addResultText("Training...");
    PGM pgm1=new PGM();
    for(i=0;i<=MaxFaceIndex;i++)
    {
    for(j=97;j<=98;j++)//'a' to 'b'
    {
     
    for(int k=0;k<=17;k++)
    {
     
    NumFaces=NumFaces+1;
    FaceFileNames[NumFaces]="C:\\Users\\Manoharan\\Documents\\NetBeansProjects\\images\\train\\fac"+k+"\\face"+i+(char)j+".pgm";
     
    PGM_ImageFilter imgFilter=new PGM_ImageFilter();
    imgFilter.set_inFilePath(FaceFileNames[NumFaces]);
    imgFilter.set_outFilePath("temp.pgm");
    imgFilter.resize(SizeX,SizeY);
    System.out.print(""+i);
    System.out.println(""+(char)j);
    pgm1.setFilePath("temp.pgm");
    pgm1.readImage();
     
    for(xBase=0;xBase<=xDiv-1;xBase++)
    {
    for(yBase=0;yBase<=yDiv-1;yBase++)
    {
    StartX=xBase*BlockWidth;
    StartY=yBase*BlockHeight;
    xLow=StartX;
    xHigh=StartX+BlockWidth-1;
    yLow=StartY;
    yHigh=StartY+BlockHeight-1;
     
    CellSum=0;
    for(xSub=xLow;xSub<=xHigh;xSub++)
    {
    for(ySub=yLow;ySub<=yHigh;ySub++)
    {
    GrayLevel=pgm1.getPixel(xSub,ySub);
    CellSum=CellSum+GrayLevel;
     
    }
    System.out.println("CellSum"+CellSum);
    }
    CellAvg=CellSum/(BlockWidth*BlockHeight);
    Faces[xBase][yBase][NumFaces]=CellAvg;
    }
    }
     
    }
     
    }
     
    for(xBase=0;xBase<=xDiv-1;xBase++)
    {
    for(yBase=0;yBase<=yDiv-1;yBase++)
    {
    CellSum=0;
    for(i=1;i<=NumFaces;i++)
    {
    CellSum=CellSum+Faces[xBase][yBase][I];[/I]
    [I]}[/I]
    [I]CellAvg=CellSum/NumFaces;[/I]
    [I] FaceTemplate[xBase][yBase]=CellAvg;[/I]
     
    [I]}[/I]
    [I]}[/I]
     
    [I]for(xBase=0;xBase<=xDiv-1;xBase++)[/I]
    [I]{[/I]
    [I]for(yBase=0;yBase<=yDiv-1;yBase++)[/I]
    [I]{[/I]
    [I]for(i=1;i<=NumFaces;i++)[/I]
    [I]{[/I]
    [I]LaplacianFaces[xBase][yBase][I]=Faces[xBase][yBase][I]-FaceTemplate[xBase][yBase];[/I][/I][/I]
     
    [I]}[/I]
     
    [I]}[/I]
    [I]}[/I]
     
     
    [I]PGM pgm2=new PGM();[/I]
    [I]pgm2.setFilePath("template.pgm");[/I]
    [I]pgm2.setType("P5");[/I]
    [I]pgm2.setComment("");[/I]
    [I]pgm2.setDimension(SizeX,SizeY);[/I]
    [I]pgm2.setMaxGray(255);[/I]
    [I]for(xBase=0;xBase<=xDiv-1;xBase++)[/I]
    [I]{[/I]
    [I]for(yBase=0;yBase<=yDiv-1;yBase++)[/I]
    [I]{[/I]
    [I]StartX=xBase*BlockWidth;[/I]
    [I]StartY=yBase*BlockHeight;[/I]
    [I]xLow=StartX;[/I]
    [I]xHigh=StartX+BlockWidth-1;[/I]
    [I]yLow=StartY;[/I]
    [I]yHigh=StartY+BlockHeight-1;[/I]
    [I] for(xSub=xLow;xSub<=xHigh;xSub++)[/I]
    [I]{[/I]
    [I]for(ySub=yLow;ySub<=yHigh;ySub++)[/I]
    [I]{[/I]
    [I]GrayLevel=FaceTemplate[xBase][yBase];[/I]
     
    [I]pgm2.setPixel(xSub,ySub,GrayLevel);[/I]
    [I]}[/I]
     
    [I] }[/I]
     
    [I]}[/I]
     
     
    [I]}[/I]
     
    [I] NumFaces++;[/I]
    [I]pgm2.writeImage();[/I]
    [I]addResultText("done.");[/I]
    [I]}[/I]
     
    [I] }[/I]
    [I] public void test()[/I]
    [I]{[/I]
    [I]int xBase,yBase,xSub,ySub;[/I]
    [I]int xLow,xHigh,yLow,yHigh;[/I]
    [I]int GrayLevel;[/I]
    [I]int CellSum,CellAvg;[/I]
    [I]int i,j;[/I]
    [I]int xDiv,yDiv;[/I]
    [I]int BlockWidth,BlockHeight;[/I]
    [I]int StartX,StartY;[/I]
    [I]int SizeX,SizeY;[/I]
    [I]SizeX=80;[/I]
    [I]SizeY=80;[/I]
    [I]xDiv=20;[/I]
    [I]yDiv=20;[/I]
    [I]BlockWidth=SizeX/xDiv;[/I]
    [I]BlockHeight=SizeY/yDiv;[/I]
    [I]int TestFace[][]=new int[xDiv][yDiv];[/I]
    [I]int TestLaplacianFace[][]=new int[xDiv][yDiv];[/I]
    [I]int LaplacianDiff;[/I]
    [I]int MinLaplacianIndex;[/I]
    [I]double TotalLaplacianDiff,MinLaplacianDiff;[/I]
    [I]addResultText("\nTesting...");[/I]
    [I]PGM pgm1=new PGM();[/I]
    [I]pgm1.setFilePath(txtTestPath.getText());[/I]
    [I]pgm1.readImage();[/I]
    [I]for(xBase=0;xBase<=xDiv-1;xBase++)[/I]
    [I]{[/I]
    [I]for(yBase=0;yBase<=yDiv-1;yBase++)[/I]
    [I]{[/I]
    [I]StartX=xBase*BlockWidth;[/I]
    [I]StartY=yBase*BlockHeight;[/I]
    [I]xLow=StartX;[/I]
    [I]xHigh=StartX+BlockWidth-1;[/I]
    [I]yLow=StartY;[/I]
    [I]yHigh=StartY+BlockHeight-1;[/I]
     
    [I]CellSum=0;[/I]
    [I]for(xSub=xLow;xSub<=xHigh;xSub++)[/I]
    [I]{[/I]
    [I]for(ySub=yLow;ySub<=yHigh;ySub++)[/I]
    [I]{[/I]
    [I]GrayLevel=pgm1.getPixel(xSub,ySub);[/I]
    [I]CellSum=CellSum+GrayLevel;[/I]
    [I]}[/I]
    [I]}[/I]
    [I]CellAvg=CellSum/(BlockWidth*BlockHeight);[/I]
    [I]TestFace[xBase][yBase]=CellAvg;[/I]
     
    [I]}[/I]
    [I]}[/I]
     
    [I]PGM pgm2=new PGM();[/I]
    [I]pgm2.setFilePath("diff.pgm");[/I]
    [I]pgm2.setType("P5");[/I]
    [I]pgm2.setComment("");[/I]
    [I]pgm2.setDimension(SizeX,SizeY);[/I]
    [I]pgm2.setMaxGray(255);[/I]
     
    [I]for(xBase=0;xBase<=xDiv-1;xBase++)[/I]
    [I]{[/I]
    [I]for(yBase=0;yBase<=yDiv-1;yBase++)[/I]
    [I]{[/I]
    [I]StartX=xBase*BlockWidth;[/I]
    [I]StartY=yBase*BlockHeight;[/I]
    [I]xLow=StartX;[/I]
    [I]xHigh=StartX+BlockWidth-1;[/I]
    [I]yLow=StartY;[/I]
    [I]yHigh=StartY+BlockHeight-1;[/I]
     
    [I]for(xSub=xLow;xSub<=xHigh;xSub++)[/I]
    [I]{[/I]
    [I]for(ySub=yLow;ySub<=yHigh;ySub++)[/I]
    [I]{[/I]
    [I]GrayLevel=TestFace[xBase][yBase];[/I]
     
    [I]pgm2.setPixel(xSub,ySub,GrayLevel);[/I]
    [I]}[/I]
     
    [I]}[/I]
    [I]}[/I]
    [I]}[/I]
     
    [I]for(xBase=0;xBase<=xDiv-1;xBase++)[/I]
    [I]{[/I]
    [I]for(yBase=0;yBase<=yDiv-1;yBase++)[/I]
    [I]{[/I]
    [I]TestLaplacianFace[xBase][yBase]=TestFace[xBase][yBase]-FaceTemplate[xBase][yBase];[/I]
    [I]}[/I]
    [I]}[/I]
     
    [I]MinLaplacianDiff=2147483647; //2^32[/I]
    [I]MinLaplacianIndex=-1;[/I]
     
    [I]for(i=1;i<=NumFaces;i++)[/I]
    [I]{[/I]
    [I] TotalLaplacianDiff=0;[/I]
    [I]for(xBase=0;xBase<=xDiv-1;xBase++)[/I]
    [I]{[/I]
    [I]for(yBase=0;yBase<=yDiv-1;yBase++)[/I]
    [I]{[/I]
    [I]TotalLaplacianDiff=TotalLaplacianDiff+java.lang.Math.abs(TestLaplacianFace[xBase][yBase]-LaplacianFaces[xBase][yBase][I]);[/I][/I]
     
    [I] }[/I]
    [I] if(MinLaplacianDiff>=TotalLaplacianDiff){[/I]
    [I] System.out.println("TotalLaplacianDiff"+TotalLaplacianDiff);[/I]
    [I] System.out.println("NumFaces"+i);[/I]
    [I] }[/I]
     
     
    [I] }[/I]
    [I] System.out.println("Tot"+TotalLaplacianDiff);[/I]
    [I] if(MinLaplacianDiff>TotalLaplacianDiff)[/I]
    [I]{[/I]
     
    [I] MinLaplacianDiff=TotalLaplacianDiff;[/I]
     
    [I]MinLaplacianIndex=i;[/I]
    [I]}[/I]
     
     
    [I] }[/I]
     
     
    [I]pgm2.writeImage();[/I]
    [I] System.out.println("MinLapDiff"+MinLaplacianDiff);[/I]
    [I] System.out.println("MinLaplacianIndex"+MinLaplacianIndex);[/I]
     
     
    [I]if(MinLaplacianDiff>DifferenceThreshold)[/I]
    [I]{[/I]
    [I]frmImage2.getGraphics().clearRect(0,0,200,150);[/I]
    [I]addResultText("done.");[/I]
    [I]addResultText("\n\nNot Matched.");[/I]
    [I]JOptionPane.showMessageDialog(null,"Not Matched.");[/I]
    [I]}[/I]
    [I]else[/I]
    [I]{[/I]
    [I]PGM pgmMatched=new PGM();[/I]
    [I]pgmMatched.setFilePath(FaceFileNames[MinLaplacianIndex]);[/I]
    [I]pgmMatched.readImage();[/I]
    [I]pgmMatched.setFilePath("matched.pgm");[/I]
    [I]pgmMatched.writeImage();[/I]
    [I]drawImage(frmImage2.getGraphics(),"matched.pgm");[/I]
    [I]addResultText("done.");[/I]
    [I]addResultText("\n\nMatched: "+FaceFileNames[MinLaplacianIndex]);[/I]
     
    [I] if(FaceFileNames[MinLaplacianIndex].equals("C:\\Users\\Manoharan\\Documents\\NetBeansProjects\\images\\train\\face3b.pgm"))[/I]
    [I] {[/I]
    [I] addResultText("\n Feature Extraction....");[/I]
    [I] addResultText("\n The extracted feature is nose"); [/I]
    [I] }[/I]
    [I] else if(FaceFileNames[MinLaplacianIndex].equals("C:\\Users\\Manoharan\\Documents\\NetBeansProjects\\images\\train\\face16b.pgm"))[/I]
    [I] {[/I]
    [I] addResultText("\n Feature Extraction....");[/I]
    [I] addResultText("\n The extracted feature is eye brows"); [/I]
    [I] }[/I]
    [I] else if(FaceFileNames[MinLaplacianIndex].equals("C:\\Users\\Manoharan\\Documents\\NetBeansProjects\\images\\train\\face17a.pgm"))[/I]
    [I] {[/I]
    [I] addResultText("\n Feature Extraction....");[/I]
    [I] addResultText("\n The extracted feature is cheeks"); [/I]
    [I] }[/I]
     
    [I]}[/I]
    [I]}[/I]
     
    [I]public static void main(String args[])[/I]
    [I]{[/I]
    [I]new MyFaceRecog();[/I]
    [I]}[/I]
    [I]} 
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register