C++ Install and configure opencv project in ubuntu
Content
I. Install JVM openjdk-8-jdk
II. Install OpenCV
III. Configure project for OpenCV
IV. Example
I. Intall open JDK 8
1. sudo add-apt-repository ppa:openjdk-r/ppa
2. sudo apt-get update
3. sudo apt-get install openjdk-8-jdk
4. sudo update-alternatives --config java
II. Install OpenCV
1. sudo apt-get upgrade
2. sudo apt-get update
3. sudo apt-get install cmake
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
4. sudo apt-get install libgtk2.0-dev
5. sudo apt-get install python-numpy
6. mkdir release
7. cd release
8. cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_NEW_PYTHON_SUPPORT=ON -D CMAKE_INSTALL_PREFIX=/usr/local .
Or
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local .
or
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" .
include #include #include #include #include //============================================== // Library of C++ #include #include #include #include #include #include #include #include //============================================== // Library of Socket #include #include #include using namespace cv; using namespace std; //============================================== //Declare Function to process string to_string(int i); void receiveImage(int sock, int idimage); void recognizeGender(Mat image, string hardKey); void sendPOST(string harwareKey, int id_gender); //============================================== //Declare global variables static int idImage = 0; static string face_cascade_name = "/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml"; static string eyes_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; //String to create POST message static string man = "gender=man&nPersons=1&HK="; static string woman = "gender=woman&nPersons=1&HK="; static string crowed = "gender=crowed&nPersons=1&HK="; static string length; static string space = "\r\n\r\n"; static string content, contentSend; static int lengthContent; static const char* httpRequest1; static const char* httpRequest2; static const char* httpRequest3; static string httpRequest = "POST /xibo-cms/listener.php HTTP/1.1\r\n" "Host: dcn402.asuscomm.com \r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: "; // Can move to Main functions //-----------------------begin-------------------------- static Ptr model = createLBPHFaceRecognizer(); // Load Database trained //model->load("database_gender.xml"); //Create model to recognize Face // Detect Face to recognize static CascadeClassifier face_cascade; //face_cascade.load(face_cascade_name); static CascadeClassifier eyes_cascade; //eyes_cascade.load(eyes_cascade_name) //if (!face_cascade.load(face_cascade_name)) // printf("--(!)Error loading\n"); //if (!eyes_cascade.load(eyes_cascade_name)) // printf("--(!)Error loading\n"); //lbph_cascade.load(face_cascade_name); // Check for invalid input //------------------------end------------------------- //============================================== // Main Function int main() { // Preprocessing for recognize face model->load("database_gender.xml"); face_cascade.load(face_cascade_name); eyes_cascade.load(eyes_cascade_name); //===================== //Socket to receive Image int sockfd, newsockfd, pid; socklen_t clilen; // For Local Network int portno = 22222; // For DCN Cloud //int portno = 20222 struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) perror("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("ERROR on binding"); listen(sockfd, 10); clilen = sizeof(cli_addr); for (;;) { idImage++; newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) { perror("ERROR on accept"); exit(1); } // Create child process pid = fork(); if (pid < 0) { perror("ERROR on fork"); exit(1); } if (pid == 0) { /* This is the client process */ close(sockfd); receiveImage(newsockfd, idImage); exit(0); } else { close(newsockfd); } } return 0; } //============================================== void receiveImage(int newsockfd, int idimage) { //idImage++; cout << "=======================BEGIN================ " << endl; // Creat variables for processing string keyHardware; int ptr = 0; int bytes; char key[255]; Mat img = Mat::zeros(480, 640, CV_8UC3); int imgSize = img.total() * img.elemSize(); uchar sockData[imgSize]; for (int i = 0; i < imgSize; i += bytes) { if ((bytes = recv(newsockfd, sockData + i, imgSize - i, 0)) == -1) { perror("ERROR on accept"); } } // Assign pixel value to img for (int i = 0; i < img.rows; i++) { for (int j = 0; j < img.cols; j++) { img.at(i, j) = cv::Vec3b(sockData[ptr + 0], sockData[ptr + 1], sockData[ptr + 2]); ptr = ptr + 3; } } // Get Key Hardware bytes = recv(newsockfd, key, 255, 0); key[bytes] = '\0'; keyHardware = key; cout << "ID Client = " << keyHardware << endl; //For Local Network string pidImage = to_string(idImage) + ".jpg"; cout << "Image received: " << pidImage << endl; imwrite(pidImage, img); // For DCN Cloud recognizeGender(img, keyHardware); cout << "===============END====================" << endl; } //============================================== void recognizeGender(Mat image, string key) { cout << "-----------Begin Recognizing------------" << endl; /* // Can move to Main functions //-----------------------begin-------------------------- Ptr model = createLBPHFaceRecognizer(); // Load Database trained model->load("database_gender.xml"); //Create model to recognize Face // Detect Face to recognize CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; if (!face_cascade.load(face_cascade_name)) printf("--(!)Error loading\n"); if (!eyes_cascade.load(eyes_cascade_name)) printf("--(!)Error loading\n"); //lbph_cascade.load(face_cascade_name); // Check for invalid input //------------------------end------------------------- */ if (!image.data) { cout << "Could not open or find the image" << endl; } Mat testSample, grayImage; cvtColor(image, grayImage, CV_BGR2GRAY); equalizeHist(grayImage, testSample); vector > faces; face_cascade.detectMultiScale(testSample, faces, 1.1, 4, CV_HAAR_DO_CANNY_PRUNING | CV_HAAR_SCALE_IMAGE, Size(20, 20), Size(200, 200)); if (faces.size() == 1) { cout << "Size = 1" << endl; // Process face by face Rect face_i = faces[0]; Mat face = testSample(face_i); vector eyes; eyes_cascade.detectMultiScale(face, eyes); if (eyes.size() > 0) { Mat face_resized; resize(face, face_resized, Size(200, 200), 1.0, 1.0, INTER_CUBIC); //rectangle(image, face_i, CV_RGB(0, 255, 0), 10); int predictedLabel = model->predict(face_resized); //Send POST message to DS Server sendPOST(key, predictedLabel); if (predictedLabel == 0) cout << "\n Gender = MAN" << endl; else cout << "\n Gender = WOMAN" << endl; } else { cout << "DO NOT HAVE ANYONE" << endl; cout << "DO NOT SEND MESSAGE" << endl; } } else if (faces.size() > 1) { cout << "\n Size = " << faces.size() << endl; //sendPOST(key, 2); Mat face_resized; unsigned int i, n = 0; for (i = 0; i < faces.size(); i++) { // For the face processing Rect face_i = faces[i]; Mat face = testSample(face_i); // To recognize eyes vector eyes; eyes_cascade.detectMultiScale(face, eyes); if (eyes.size() > 0) { n++; resize(face, face_resized, Size(200, 200), 1.0, 1.0, INTER_CUBIC); } } if (n > 1) { cout << "\n CROWDED" << endl; sendPOST(key, 2); } if (n == 1) { int predictedLabel = model->predict(face_resized); sendPOST(key, predictedLabel); if (predictedLabel == 0) cout << "\n Gender = MAN instead CROWED" << endl; else cout << "\n Gender = WOMAN instead CROWED" << endl; } } else { cout << "DO NOT HAVE ANYONE" << endl; cout << "DO NOT SEND MESSAGE" << endl; } cout << "-----------End Recognizing------------" << endl; } //============================================== void sendPOST(string harwareKey, int id_gender) { //Create new socket variables int sockfd; int portno = 20180; struct sockaddr_in serv_addr; struct hostent *server; //Create socket to send POST message sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) perror("ERROR opening socket"); server = gethostbyname("dcn402.asuscomm.com"); if (server == NULL) { fprintf(stderr, "ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *) server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("ERROR connecting"); //Send data to DS server switch (id_gender) { case 0: content = man + harwareKey; lengthContent = int(content.length()); length = to_string(lengthContent); contentSend = httpRequest + length + space + content; httpRequest1 = contentSend.c_str(); send(sockfd, httpRequest1, strlen(httpRequest1), 0); //cout << httpRequest1 << endl; break; case 1: content = woman + harwareKey; lengthContent = int(content.length()); length = to_string(lengthContent); contentSend = httpRequest + length + space + content; httpRequest2 = contentSend.c_str(); send(sockfd, httpRequest2, strlen(httpRequest2), 0); //cout << httpRequest2 << endl; break; default: content = crowed + harwareKey; lengthContent = int(content.length()); length = to_string(lengthContent); contentSend = httpRequest + length + space + content; httpRequest3 = contentSend.c_str(); //cout << "httpRequest3" << strlen(httpRequest3) << endl; send(sockfd, httpRequest3, strlen(httpRequest3), 0); //cout << httpRequest3 << endl; } close(sockfd); //cout << "Connected.\n" << endl; //-------------END------------- } //============================================== string to_string(int i) { std::stringstream ss; ss << i; return ss.str(); }
File đính kèm:
- ubuntu_c_eclipse_install_and_configuration_opencv_6235.docx