Qengineering / Face-Recognition-Raspberry-Pi-64-bits

Recognize 2000+ faces on your Raspberry Pi 4 with database auto-fill and anti-spoofing
https://qengineering.eu/deep-learning-examples-on-raspberry-32-64-os.html
BSD 3-Clause "New" or "Revised" License
69 stars 19 forks source link

Compile with g++ #10

Closed kevinzezel closed 2 years ago

kevinzezel commented 2 years ago

Hi,

First of all, nice work. Thanks for sharing.

I don't have much experience with c++, so sorry for the dumb question.

How can i compile and run the script outside of codeblocks using g++?

I moved all .hpp and .cpp files to the same folder and i run this command on raspberry PI console:

g++ main.cpp -o run -I/usr/local/include/opencv4 -I/usr/local/include/ncnn `pkg-config --cflags --libs opencv4`

I got this error:

pi@raspberrypi:~/teste_retinaface $ g++ main.cpp -o run -I/usr/local/include/opencv4 -I/usr/local/include/ncnn `pkg-config --cflags --libs opencv4`
/usr/bin/ld: /tmp/ccbcfqqm.o: in function `main':
main.cpp:(.text+0x34): undefined reference to `TRetina::TRetina(int, int, bool)'
/usr/bin/ld: main.cpp:(.text+0x198): undefined reference to `TRetina::detect_retinaface(cv::Mat const&, std::vector<FaceObject, std::allocator<FaceObject> >&)'
/usr/bin/ld: main.cpp:(.text+0x328): undefined reference to `TRetina::~TRetina()'
/usr/bin/ld: main.cpp:(.text+0x37c): undefined reference to `TRetina::~TRetina()'
collect2: error: ld returned 1 exit status

Obs: I'm testing only the retinaface. The following code runs perfectly in codeblocks with the project that you provide.

Files in folder:

pi@raspberrypi:~/teste_retinaface $ ls -al
total 32
drwxr-xr-x  2 pi pi  4096 Mar 11 16:33 .
drwxr-xr-x 18 pi pi  4096 Mar 11 16:14 ..
-rw-r--r--  1 pi pi  1561 Mar 11 16:26 main.cpp
-rw-r--r--  1 pi pi 12519 Mar 11 16:14 TRetina.cpp
-rw-r--r--  1 pi pi  1022 Mar 11 16:14 TRetina.h

main.cpp

#include <iostream>
#include <opencv2/opencv.hpp>
#include "TRetina.h"

#define RETINA

//----------------------------------------------------------------------------------------
//
// Created by markson zhang
//
// Edited by Xinghao Chen 2020/7/27
//
// Modified by Q-engineering 2020/12/28
//
//----------------------------------------------------------------------------------------
// Build defines
// comment them to turn a function off
//----------------------------------------------------------------------------------------

const int   RetinaWidth      = 320;
const int   RetinaHeight     = 240;

using namespace std;
using namespace cv;

int main(){

    float ScaleX, ScaleY;
    cv::Mat frame;
    cv::Mat result_cnn;
    std::vector<FaceObject> Faces;

    TRetina Rtn(RetinaWidth, RetinaHeight, false);

    cv::VideoCapture cap(0);             //RaspiCam
    //cv::VideoCapture cap("Norton_A.mp4");   //Movie
    if (!cap.isOpened()) {
        cerr << "ERROR: Unable to open the camera" << endl;
        return 0;
    }
    cout << "Start grabbing, press ESC on TLive window to terminate" << endl;

    while(1){
        cap >> frame;
        if (frame.empty()) {
            cerr << "End of movie" << endl;
            break;
        }
        ScaleX = ((float) frame.cols) / RetinaWidth;
        ScaleY = ((float) frame.rows) / RetinaHeight;

        // copy/resize image to result_cnn as input tensor
        cv::resize(frame, result_cnn, Size(RetinaWidth,RetinaHeight),INTER_LINEAR);

        Rtn.detect_retinaface(result_cnn,Faces);

        for(size_t i=0; i < Faces.size(); i++){
            FaceObject& obj = Faces[i];
            obj.rect.x *= ScaleX;
            obj.rect.y *= ScaleY;
            obj.rect.width *= ScaleX;
            obj.rect.height*= ScaleY;

            for(int u=0;u<5;u++){
                obj.landmark[u].x*=ScaleX;
                obj.landmark[u].y*=ScaleY;
            }

        }

        cout << "Faces:" << Faces.size() << endl;
    }

    return 0;
}
Qengineering commented 2 years ago

Assuming your in the 'main' directory.

.
├── bin
├── FaceRecognition.cbp
├── FaceRecognition.depend
├── FaceRecognition.layout
├── Graham Norton.jpg
├── img
├── include
├── LICENSE
├── models
├── Norton_2.mp4
├── Norton_A.mp4
├── obj
├── README.md
└── src

Give the following commands

g++ -O3 -Wall -fexceptions -Wno-unknown-pragmas -O2 -I/usr/local/include/opencv4 -I/usr/local/include/ncnn -Iinclude -c /home/pi/software/Face-Recognition/src/main.cpp -o main.o
g++ -O3 -Wall -fexceptions -Wno-unknown-pragmas -O2 -I/usr/local/include/opencv4 -I/usr/local/include/ncnn -Iinclude -c /home/pi/software/Face-Recognition/src/TArcface.cpp -o TArcface.o
g++ -O3 -Wall -fexceptions -Wno-unknown-pragmas -O2 -I/usr/local/include/opencv4 -I/usr/local/include/ncnn -Iinclude -c /home/pi/software/Face-Recognition/src/TBlur.cpp -o TBlur.o
g++ -O3 -Wall -fexceptions -Wno-unknown-pragmas -O2 -I/usr/local/include/opencv4 -I/usr/local/include/ncnn -Iinclude -c /home/pi/software/Face-Recognition/src/TLive.cpp -o TLive.o
g++ -O3 -Wall -fexceptions -Wno-unknown-pragmas -O2 -I/usr/local/include/opencv4 -I/usr/local/include/ncnn -Iinclude -c /home/pi/software/Face-Recognition/src/TMtCNN.cpp -o TMtCNN.o
g++ -O3 -Wall -fexceptions -Wno-unknown-pragmas -O2 -I/usr/local/include/opencv4 -I/usr/local/include/ncnn -Iinclude -c /home/pi/software/Face-Recognition/src/TRetina.cpp -o TRetina.o
g++  -o FaceRecognition main.o TArcface.o TBlur.o TMtCNN.o TLive.o TRetina.o TWarp.o  -fopenmp -I/usr/local/include/opencv4 -L/usr/local/lib `pkg-config --cflags --libs opencv4` -ldl -lpthread -s  /usr/local/lib/ncnn/libncnn.a

You get the executable FaceRecognition.

.
├── bin
├── FaceRecognition
├── FaceRecognition.cbp
├── FaceRecognition.depend
├── FaceRecognition.layout
├── Graham Norton.jpg
├── img
├── include
├── LICENSE
├── main.o
├── models
├── Norton_2.mp4
├── Norton_A.mp4
├── obj
├── README.md
├── src
├── TArcface.o
├── TBlur.o
├── TLive.o
├── TRetina.o
└── TWarp.o
kevinzezel commented 2 years ago

Thank you very much!! It worked

roksage commented 2 years ago

Hello guys,

tried following your steps. But got bunch off errors. Are my path's wrong? Thanks

image

Qengineering commented 2 years ago

You are running the g++ commands is the wrong folder, as can be seen by the error No such file or directory In the command you have at the end the /home/pi/software/Face-Recognition/src/main.cpp part The mentioned location must match your directory structure.

Better to use cmake as explained here #12