Problems with Array

Asked

Viewed 65 times

0

I’m making a facial recognition system, and that’s all I need to finish. I have the following code:

for (int i = 0; i < facesDetectadas.size(); i++) {
                                Rect dadosFace = facesDetectadas.get(i);
                                rectangle(imagemCamera, dadosFace, new Scalar(255, 255, 0, 0), 2, i, i);
                                Mat faceCapturada = new Mat(imagemCinza, dadosFace);
                                opencv_imgproc.resize(faceCapturada, faceCapturada, new Size(160, 160));

                                IntPointer rotulo = new IntPointer(1);
                                DoublePointer confianca = new DoublePointer(1);
                                reconhecedor.predict(faceCapturada, rotulo, confianca);
                                int predicao = rotulo.get(0);
                                String nome;
                                if (predicao == -1) {
                                    nome = "Desconhecido";
                                } else {
                                    nome = pessoas[predicao] + " - " + confianca.get(0);
                                    campoid.setText(String.valueOf(predicao));
                                    rec();
                                }
                                int x = Math.max(dadosFace.tl().x() - 10, 0);
                                int y = Math.max(dadosFace.tl().y() - 10, 0);
                                putText(imagemCamera, nome, new Point(x, y), FONT_HERSHEY_PLAIN, 1.7, new Scalar(0, 255, 0, 2));
                            }

And further up, I have in the "rec" method I have the same array:

Follow the method rec:

private void rec() {
    SwingWorker worker = new SwingWorker() {
        @Override
        protected Object doInBackground() throws Exception {
            conecta.conexao();
            try {
                String SQL = "SELECT * FROM reconhecer where id=" + campoid.getText() + "";
                conecta.executaSQL(SQL);

                while (conecta.rs.next()) {
                    camponome.setText(conecta.rs.getString(1));
                    campoidade.setText(conecta.rs.getString(2));
                    campoemail.setText(conecta.rs.getString(3));
                    campobloco.setText(conecta.rs.getString(5));
                    campoapto.setText(conecta.rs.getString(6));
                    campocondominio.setText(conecta.rs.getString(7));
                    System.out.println("Pessoa idêntificada como: " + conecta.rs.getString(1));
                    System.out.println("---------------");

                    Array identificacao = conecta.rs.getArray(2);
                    String[] pessoas = (String[]) identificacao.getArray();

                    for (int i = 0; i < pessoas.length; i++) {
                        System.out.println(pessoas[i]);
                    }
                    System.out.println("");

                }

            } catch (Exception ex) {
            }
            conecta.desconecta();
            return null;
        }
    };
    worker.execute();
}

Reason I did it separately: I did a swing worker, and it makes it easy not to hang on to the application.

My problem and my doubt: Mine pessoas where is the if, does not recognize the pessoas who’s in the rec, how can I leave the pessoas visible for everything? I tried to use global but it doesn’t work either.

  • 1

    Provide a [mcve] or a more relevant code, because this code there does not say much to help you in relation to doubt.

  • Hello, Abner! Could you share the two whole methods? You can’t pass pessoas via parameter to the rec?

  • The method containing this if need to receive as parameter the array with which you want to work within the if.

  • 2

    doInBackground runs on a different thread, you will not be able to share variables, nor publish, between what is outside and inside it.

  • Yes, but if I put the array there, it needs to "call" the other parameters, for example, the conecta.rs...., I tried to put it there, but it locks the whole application, in every bank check, it locks everything.

  • 1

    It makes it easier to help you if you provide one [mcve], because it is better to analyze the code. These loose passages make it difficult to analyze more accurately.

  • Or you can read and analyze that answer and see if it fits your case.

  • @Articuno is right about the Thread issue, but then you won’t remove it. You can change this variable to be a Singleton thread safe. Check out this answer: https://stackoverflow.com/questions/18944184/instance-variable-in-a-singleton-class-accessed-by-multiple-threads.

  • No result... I just don’t put the pessoas in my if why the system crashes all over. I’ll see if I can get something here. Thank you.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.