Soru & Cevap

Face Detection and Database

13.02.2014 - 01:44

Merhaba ,

Üzerine çalıştığım projede yüz algılama yapıyorum ve algılanan yüzün değerlerini yani bazı özel uzunlukları bir veritabanında tutmak istiyorum. bir veri tabanı oluşturdum ancak ölçülerle kayıt yaparken sıkıntı yok kayıt yapabiliyorum ancak veri tabanından çekerken şöyle bi sıkıntı oluyor , ilk hangi ismi girdiysem sadece o isim geliyor her yüz için o ismi ekrana basıyor bu konuda yardımcı olabilecek var mı ?

ikinci bir sorum da yüzü algılarken aynı kişi için aynı açıdan bazı farklı değerler gelebiliyor bunun sebebi ne olabilir ?

23 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-3646-1552938139.jpg
ibrahimhobekkaya
14.02.2014 - 09:26

Öncelikle teşekkür ederim ,

Daha önce bi çok kez face detection ile ilgili soru sordum fakat pek konuya pek hakim olanın bulunmadığını farkettim.

[code]

public class MainActivity extends Activity implements SurfaceHolder.Callback {
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;

    static Uri uriTarget;
    Button buttonTakePicture, front;
    TextView text;
    public static int l, bottom, top, right, left, t, r, b;
    public Database dtBase;

    Map<String, Integer> map2 = new HashMap<String, Integer>();
    DrawingView drawingView;
    Face[] detectedFaces;

    final int RESULT_SAVEIMAGE = 0;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dtBase = new Database(this);
        //        Cursor cursor = dtBase.readAllData(dtBase,
        //                dtBase.TableName_Face, new String[] { "faceId",
        //                        "userName", "left", "right", "bottom",
        //                        "top" });
        //        if (cursor.moveToNext()) {
        //            System.err.println(cursor.getString(cursor.getColumnIndex("userName")));
        //            System.err.println(cursor.getString(cursor.getColumnIndex("left")));
        //            System.err.println(cursor.getString(cursor.getColumnIndex("right")));
        //            System.err.println(cursor.getString(cursor.getColumnIndex("bottom")));
        //            System.err.println(cursor.getString(cursor.getColumnIndex("top")));
        //        }

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView) findViewById(R.id.camera_view);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        drawingView = new DrawingView(this);
        LayoutParams layoutParamsDrawing = new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        this.addContentView(drawingView, layoutParamsDrawing);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.setting, null);
        LayoutParams layoutParamsControl = new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        buttonTakePicture = (Button) findViewById(R.id.takepicture);
        buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                        myPictureCallback_JPG);
            }
        });

        LinearLayout layoutBackground = (LinearLayout) findViewById(R.id.background);
        layoutBackground.setOnClickListener(new LinearLayout.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                buttonTakePicture.setEnabled(false);
                camera.autoFocus(myAutoFocusCallback);
            }
        });

        text = (TextView) findViewById(R.id.text);
    }

    @SuppressLint("NewApi")
    FaceDetectionListener faceDetectionListener = new FaceDetectionListener() {

        @Override
        public void onFaceDetection(Face[] faces, Camera camera) {
            if (faces.length != 0) {
                drawingView.setHaveFace(true);
                detectedFaces = faces;
            }
            drawingView.invalidate();
        }
    };

    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
        @Override
        public void onAutoFocus(boolean arg0, Camera arg1) {
            buttonTakePicture.setEnabled(true);
        }
    };

    ShutterCallback myShutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {

        }
    };

    PictureCallback myPictureCallback_RAW = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {

        }
    };

    @SuppressLint("NewApi")
    PictureCallback myPictureCallback_JPG = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
                    new ContentValues());

            OutputStream imageFileOS;
            try {
                imageFileOS = getContentResolver().openOutputStream(uriTarget);
                imageFileOS.write(arg0);
                imageFileOS.flush();
                imageFileOS.close();

            } catch (FileNotFoundException e) {

            } catch (IOException e) {

            }

            camera.startPreview();
            camera.startFaceDetection();
        }
    };

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @SuppressLint("NewApi")
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

        if (previewing) {
            camera.stopFaceDetection();
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null) {
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();

                camera.startFaceDetection();
                previewing = true;
            } catch (IOException e) {

            }
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        camera.setFaceDetectionListener(faceDetectionListener);
    }

    @SuppressLint("NewApi")
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopFaceDetection();
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

    private class DrawingView extends View {
        boolean haveFace;
        Paint drawingPaint;

        public DrawingView(Context context) {
            super(context);
            haveFace = false;
            drawingPaint = new Paint();
            drawingPaint.setColor(Color.GREEN);
            drawingPaint.setStyle(Paint.Style.STROKE);
            drawingPaint.setStrokeWidth(3);
        }

        public void setHaveFace(boolean h) {
            haveFace = h;
        }

        @SuppressLint("DrawAllocation")
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        protected void onDraw(Canvas canvas) {
            if (haveFace) {
                int vWidth = getWidth();
                int vHeight = getHeight();
                for (int i = 0; i < detectedFaces.length; i++) {
                    l = detectedFaces[i].rect.left;
                    t = detectedFaces[i].rect.top;
                    r = detectedFaces[i].rect.right;
                    b = detectedFaces[i].rect.bottom;
                    left = (l + 1000) * vWidth / 2000;
                    top = (t + 1000) * vHeight / 2000;
                    right = (r + 1000) * vWidth / 2000;
                    bottom = (b + 1000) * vHeight / 2000;
                    map2.put("left", left);
                    map2.put("right", right);
                    map2.put("top", top);
                    map2.put("bottom", bottom);

                    canvas.drawRect(left, top, right, bottom, drawingPaint);
                    haveFace = true;
                }
                if (haveFace == true) {
                    haveFace = false;
                    Cursor cursor = dtBase.readAllData(dtBase,
                            dtBase.TableName_Face, new String[] { "faceId",
                            "userName", "left", "right", "bottom",
                    "top" });
                    if (cursor.moveToNext()) {
                        System.err.println(cursor.getString(cursor.getColumnIndex("userName")));
                        System.err.println(cursor.getString(cursor.getColumnIndex("left")));
                        System.err.println(cursor.getString(cursor.getColumnIndex("right")));
                        System.err.println(cursor.getString(cursor.getColumnIndex("bottom")));
                        System.err.println(cursor.getString(cursor.getColumnIndex("top")));

                        Map<String, Integer> map = new HashMap<String, Integer>();

                        map.put("left1",
                                cursor.getInt(cursor.getColumnIndex("left")));
                        map.put("right1",
                                cursor.getInt(cursor.getColumnIndex("right")));
                        map.put("bottom1",
                                cursor.getInt(cursor.getColumnIndex("bottom")));
                        map.put("top1",
                                cursor.getInt(cursor.getColumnIndex("top")));
                        int count =0;
                        for (Map.Entry<String, Integer> entry : map.entrySet()) {
                            for(int i =entry.getValue()-20 ;i <entry.getValue()+20 ; i++){
                                if(entry.getKey().equals("left1") && i==left){
                                    System.err.println(entry.getKey()+ "lllllllllllllllllllllllllllllll");
                                    count++;
                                }
                                if(entry.getKey().equals("right1") && i==right){
                                    System.err.println(entry.getKey()+ "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
                                    count++;
                                }
                                if(entry.getKey().equals("bottom1") && i==bottom){
                                    System.err.println(entry.getKey()+ "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
                                    count++;
                                }
                                if(entry.getKey().equals("top1") && i==top){
                                    System.err.println(entry.getKey()+ "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
                                    count++;
                                }
                            }
                            if(count>=2){
                                Toast.makeText(MainActivity.this, cursor.getString(cursor.getColumnIndex("userName")), Toast.LENGTH_SHORT).show();
                                count=0;
                            }
                        }
                    }
                    Intent a = new Intent(MainActivity.this, addDatabase.class);
                    startActivity(a);
                }
                System.out.println("left "+left);
                System.out.println("right "+right);
                System.out.println("top "+ top);
                System.out.println("bottom "+bottom);
            } else {
                canvas.drawColor(Color.TRANSPARENT);
            }
        }
    }
}

[/code]

 burada yüz algılanmasını yapmaya çalışıyorum ve left, right,top,bottom gibi yüzle ilgili değerler alıp, yüzü kare içine  alıyorum.

[code]

public class addDatabase extends Activity {
    Button save;
    EditText name;
    public Database dtBase;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_database);
        save = (Button) findViewById(R.id.save);
        name = (EditText) findViewById(R.id.name);
        dtBase = new Database(this);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ContentValues value = new ContentValues();
                value.put("userName", name.getText().toString());
                value.put("left", MainActivity.left);
                value.put("right", MainActivity.right);
                value.put("bottom", MainActivity.bottom);
                value.put("top", MainActivity.top);
                dtBase.Add(dtBase, dtBase.TableName_Face, value);

                System.err.println(name.getText().toString());
                System.err.println(MainActivity.left);
                System.err.println(MainActivity.right);
                System.err.println(MainActivity.top);
                System.err.println(MainActivity.bottom);

                Intent s = new Intent(addDatabase.this, MainActivity.class);
                startActivity(s);
            }
        });

    }
}

[/code]

 ikinci kısımda ise database e ekleme yapmak için bi ekranım var o ekranda isim yazıp değerleri o isme karşılık kaydetme yapıyorum.

[code]

public class Database extends SQLiteOpenHelper {
    private static String Kayıt = "DataBase";
    public String TableName_Face = "ibrahim";
    private static int Version = 1;

    public Database(Context context) {
        super(context, Kayıt, null, Version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String FaceSql = "CREATE TABLE "
                + TableName_Face
                + "(faceId INTEGER PRIMARY KEY AUTOINCREMENT, userName VARCHAR , left Integer, right Integer, bottom Integer, top Integer"
                + ")";
        db.execSQL(FaceSql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS" + TableName_Face);
    }

    public void Add(Database database, String tableName, ContentValues params) {
        SQLiteDatabase SqlLite = database.getWritableDatabase();
        SqlLite.insert(tableName, null, params);
        Log.e("Benden selam olsun", "eklendi");
    }
    
    public Cursor readAllData(Database database, String tableName, String[] array){
        Cursor cursor ;
        SQLiteDatabase SqlLite= database.getReadableDatabase();
        cursor= SqlLite.query(tableName, array, null, null, null, null, null);
        return cursor;
    }
}

[/code]

son kısımda ise database olaylarını yapmaya çalışıyorum kodum tam olarak böyle.

Veri tabanına ekleme yapıyor ancak dediğim gibi sadece ilk değeri geri getiriyor.

Bu konuda bana yardımcı olabilirseniz sevinirim.