Liunx树莓派(ARM)开发篇——基于翔云人工智能OCR人脸识别项目(附代码)

使用的OCR平台

翔云_人工智能API服务平台,类似的平台还有百度、腾讯等等,翔云平台如图:

在这里插入图片描述

需要的外设:

树莓派、摄像头(CSI视频接口)
在这里插入图片描述

实验步骤:

1、翔云平台购买API使用次数,以及调取API接口,可参照翔云人工智能OCR的使用

2、树莓派上实现程序,程序见下文demo.c。

3、调试摄像头(主要是设定拍照取得照片的大小),完成功能。

人脸识别demo.c

#include <stdio.h>
#include <curl/curl.h>
#include<string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define true 1
#define false 0
char buf[10240]={
    
    '\0'};

typedef unsigned int bool;
char* getPicBase64FromFile(char* filePath)
{
    
    
    char* bufPic;
    char cmd[128]={
    
    '\0'};
    sprintf(cmd,"base64 %s > tmpFile",filePath);
    system(cmd);
    int fd =open("./tmpFile",O_RDWR);
    int filelen=lseek(fd,0,SEEK_END);
    lseek(fd,0,SEEK_SET);
    bufPic=(char *)malloc(filelen+2);
    memset(bufPic,0,filelen+2);

    int readnum=read(fd,bufPic,filelen+2);
    if(readnum==-1){
    
    
        printf("read error\n");
    }
    printf("readnum=%d\n",readnum);
    close(fd);
    system("rm -f tmpFile");
    return bufPic;
}
size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
    
    
        strncpy(buf,ptr,10240);
        printf("read buf=%s\n",buf);
}
bool postUrl()
{
    
    
    CURL *curl;
    CURLcode res;
    char img1[12];
    char img2[12];
    char *key = "9mgQsbx1t8z6oyo88grS5U";
    char *secret = "e2eb32b0c608451eadb8f70827df2fff";
    int typeid = 21;
    char *format = "xml";

    char* buf1=getPicBase64FromFile("./xin1.jpg");
    char* buf2=getPicBase64FromFile("./image.jpg");

    int len=strlen(key)+strlen(secret)+strlen(buf1)+strlen(buf2)+124;
    char *postString;
    postString = (char *)malloc(len);
    memset(postString,'\0',len);
    sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
                  //  "","",key,secret,21,format);
                    buf1,buf2,key,secret,21,format);
    curl = curl_easy_init();
    if (curl)
    {
    
    
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // 指定post内容
        curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // 指定url
        curl_easy_setopt(curl,  CURLOPT_WRITEFUNCTION, readData); //将返回的http头输出到fp指向的文件
        res = curl_easy_perform(curl);
        //printf("OK!%d\n",res);
        if(strstr(buf,"是")!=NULL){
    
    
                printf("两张人像为同一人\n");
        }
        else if(strstr(buf,"否")!=NULL){
    
    
                printf("两张人像不是同一人\n");
        }
        curl_easy_cleanup(curl);
    }
    return true;
}
int main(void)
{
    
    
    char cmd1[128]={
    
    '\0'};
    sprintf(cmd1," raspistill -t 2000 -o image.jpg -w 250 -h 200");
    system(cmd1);

    postUrl();
}

猜你喜欢

转载自blog.csdn.net/weixin_44933419/article/details/113774662