调试使用百度OCR人工智能平台的通用物体和场景识别API
使用平台
一、如何发起http请求
这是本人学习是学的文章,可参考,但写的或许不是很好。
【Linux使用libcurl库进行Http请求】
可也参考其他文章
这个比较详细
二、如何使用百度AI平台
1.控制台
2.创建应用
3.进入API在线调试获取Tokens
4.填入你的token
再上传你需要识别的照片,就可以进行识别了
实现代码
/*此代码功能是将百度AI平台识别过后返回的JSON数据处理好后,只打印出得分最高那项JSON*/
#include "cJSON.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <curl/curl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char buf[10240] = {'\0'};
size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
strncpy(buf,ptr,1024);
printf("=========================get Data======================================\n");
printf("%s\n",buf);
}
char *getBase64FromFile(char *filePath)
{
char *bufPic;
char cmd[128] = {'\0'};
sprintf(cmd, "base64 %s > tempFile", filePath);
system(cmd);
int fd = open("./tempFile",O_RDWR);
int filelen = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
bufPic = (char*)malloc(filelen+8);
memset(bufPic, '\0', filelen+8);
read(fd, bufPic, filelen);
close(fd);
system("rm -f tempFlie");
return bufPic;
}
char* url_encode_with_curl(const char *input)
{
CURL *curl = curl_easy_init();
char *encoded = NULL;
if (curl)
{
encoded = curl_easy_escape(curl, input, 0);
curl_easy_cleanup(curl);
}
return encoded; // 注意:调用者负责释放这个字符串
}
void parse_complex_json(char *json_data)
{
cJSON *root = cJSON_Parse(json_data);
if (root == NULL)
{
printf("Error parsing JSON.\n");
return;
}
// 解析 result 数组,并找出最高分数的项
cJSON *result = cJSON_GetObjectItem(root, "result");
double highest_score = -1.0;
cJSON *highest_score_item = NULL;
if (cJSON_IsArray(result))
{
int size = cJSON_GetArraySize(result);
for (int i = 0; i < size; i++)
{
cJSON *item = cJSON_GetArrayItem(result, i);
cJSON *score = cJSON_GetObjectItem(item, "score");
if (cJSON_IsNumber(score))
{
if (score->valuedouble > highest_score)
{
highest_score = score->valuedouble;
highest_score_item = item;
}
}
}
}
// 打印分数最高的项
if (highest_score_item != NULL)
{
cJSON *keyword = cJSON_GetObjectItem(highest_score_item, "keyword");
cJSON *root_category = cJSON_GetObjectItem(highest_score_item, "root");
if (cJSON_IsString(keyword))
{
printf("Keyword with highest score: %s\n", keyword->valuestring);
}
printf("Highest Score: %f\n", highest_score);
if (cJSON_IsString(root_category))
{
printf("Root Category: %s\n", root_category->valuestring);
}
}
// 清理资源
cJSON_Delete(root);
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char *bufpic = getBase64FromFile("./baishi.jpg");//生成base64编码
char *encoded = url_encode_with_curl(bufpic);//将base64编码再进行url编码
int len = strlen(encoded)+1024;
postString = (char *)malloc(len);
memset(postString, '\0', len);
sprintf(postString, "image=%s", encoded);
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://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=YOUR_TOKEN");// 指定url 写入你自己Token
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "Accept: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);
res = curl_easy_perform(curl);
printf("OK:%d\n",res);
curl_easy_cleanup(curl);
}
free(postString);
free(encoded);
free(bufpic);
return true;
}
int main(void)
{
postUrl();
parse_complex_json(buf);
}