1.该项目只实现与百度云API通信传输数据;

2.该项目请在ai的指导下实现;

3.该项目为学习项目,采用Linus的Ubuntu系统和C++语言编程;

4.代码如下:

#include <opencv2/opencv.hpp>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include "face.h"


using namespace cv;
using namespace std;
using namespace aip;

int main(int, char**)
{
    Mat frame;
    Mat grayimg;
    Mat eqimg;
    CascadeClassifier Classifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml"); 
    if (Classifier.empty()) {
    cerr << "ERROR: 无法加载人脸检测模型!" << endl;
    return -1;
}
    
    vector< Rect >allface;
    Mat faceimg;
    vector< uchar>jpgBuf;
    
        // 设置APPID/AK/SK
    std::string app_id = " 122206928";
    std::string api_key = "DbDmvVsBVZC4OPrQiydmUoeO";
    std::string secret_key = "TrEzP00dqkOe4QoiX1B0yt6enokLqzee";

    aip::Face client(app_id, api_key, secret_key);
    
    std::string base64img;
    Json::Value result;
// 构造选项参数

    
    //--- INITIALIZE VIDEOCAPTURE
    VideoCapture cap;
    // open the default camera using default API
    // cap.open(0);
    // OR advance usage: select any API backend
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    // open selected camera using selected API
    cap.open(deviceID, apiID);
    // check if we succeeded
    
    
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    
    cout << "camera open success!" <<endl;
    
    //--- GRAB AND WRITE LOOP
    cout << "Start grabbing" << endl
        << "Press any key to terminate" << endl;
        
        
     
        

        
    for (;;)
    {
        // wait for a new frame from camera and store it into 'frame'
        cap.read(frame);
        // check if we succeeded
        
   
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        
        cvtColor(frame,grayimg,COLOR_BGR2GRAY );
        equalizeHist(grayimg,eqimg ); 
        Classifier.detectMultiScale ( eqimg,allface);//detect picture
	if(allface.size())
	{
		 rectangle (eqimg,allface[0],Scalar(255,255,255));
		 faceimg=eqimg(allface[0]);
		 imencode( ".jpg",faceimg,jpgBuf); 
	
		 base64img=base64_encode((char*)jpgBuf.data(), jpgBuf.size());
		 



// 正确调用:3个参数(图片数据、图片类型、选项)
		result = client.face_search_v3(base64img,"BASE64","zhou",aip::json_null);
		 
		 cout<<result<<endl;
}

	 	  
        	
              
        // show live and wait for a key with timeout long enough to show images
        imshow("Live", eqimg);
        if (waitKey(50) >= 0)
            break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
	return 0;
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐