在医院中,有大量的X光、CT等医学影像图片。识别影像中的病变特征、人体器官等信息进行改名,将患者的病情诊断摘要、检查日期等信息导出到表格,可以提高医疗影像资料的管理效率,方便医生快速查阅和对比患者的影像资料。
要实现批量图片文字识别并根据识别结果自动重命名图片的功能,你可以使用腾讯云的 OCR(光学字符识别)API。以下是详细的步骤和示例代码:
你需要使用libcurl
来进行 HTTP 请求,使用jsoncpp
来处理 JSON 数据。在 Ubuntu 系统上,可以使用以下命令安装:
bash
sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <curl/curl.h>
#include <json/json.h>
#include <filesystem>
namespace fs = std::filesystem;
// 回调函数,用于处理HTTP响应
size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *s) {
size_t newLength = size * nmemb;
try {
s->append((char*)contents, newLength);
} catch(std::bad_alloc &e) {
return 0;
}
return newLength;
}
// 读取图片文件为Base64编码
std::string readFileAsBase64(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
std::ostringstream oss;
oss << file.rdbuf();
std::string data = oss.str();
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string ret;
int val = 0;
int val_bits = -6;
for (unsigned char c : data) {
val = (val << 8) + c;
val_bits += 8;
while (val_bits >= 0) {
ret.push_back(base64_chars[(val >> val_bits) & 0x3F]);
val_bits -= 6;
}
}
if (val_bits > -6) {
ret.push_back(base64_chars[((val << 8) >> (val_bits + 8)) & 0x3F]);
}
while (ret.size() % 4) {
ret.push_back('=');
}
return ret;
}
// 调用腾讯云OCR API进行文字识别
std::string recognizeText(const std::string& imageBase64, const std::string& secretId, const std::string& secretKey) {
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
std::string url = "https://ocr.tencentcloudapi.com";
std::string postFields = "{\"ImageBase64\":\"" + imageBase64 + "\"}";
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, ("X-TC-Action: GeneralBasicOCR"));
headers = curl_slist_append(headers, ("X-TC-Version: 2018-11-19"));
headers = curl_slist_append(headers, ("X-TC-Region: ap-guangzhou"));
headers = curl_slist_append(headers, ("X-TC-Timestamp: " + std::to_string(time(nullptr))));
headers = curl_slist_append(headers, ("X-TC-Credential: " + secretId + "/2025-02-25/ap-guangzhou/ocr/tc3_request"));
headers = curl_slist_append(headers, ("Authorization: TC3-HMAC-SHA256 Credential=" + secretId + "/2025-02-25/ap-guangzhou/ocr/tc3_request, SignedHeaders=content-type;host, Signature=" + std::string("这里需要进行签名计算,可参考腾讯云文档")));
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
// 解析JSON响应
Json::Reader reader;
Json::Value root;
if (reader.parse(readBuffer, root)) {
if (root["Response"]["TextDetections"].isArray()) {
std::string text;
for (const auto& detection : root["Response"]["TextDetections"]) {
text += detection["DetectedText"].asString();
}
return text;
}
}
return "";
}
// 批量处理图片
void batchProcessImages(const std::string& directory, const std::string& secretId, const std::string& secretKey) {
for (const auto& entry : fs::directory_iterator(directory)) {
if (entry.is_regular_file() && (entry.path().extension() == ".jpg" || entry.path().extension() == ".png")) {
std::string imagePath = entry.path().string();
std::string imageBase64 = readFileAsBase64(imagePath);
std::string recognizedText = recognizeText(imageBase64, secretId, secretKey);
if (!recognizedText.empty()) {
// 生成新的文件名
std::string newName = recognizedText + entry.path().extension().string();
std::string newPath = entry.path().parent_path().string() + "/" + newName;
// 重命名文件
try {
fs::rename(imagePath, newPath);
std::cout << "Renamed " << imagePath << " to " << newPath << std::endl;
} catch (const fs::filesystem_error& e) {
std::cerr << "Error renaming file: " << e.what() << std::endl;
}
}
}
}
}
int main() {
std::string directory = "./images"; // 图片所在目录
std::string secretId = "your_secret_id";
std::string secretKey = "your_secret_key";
batchProcessImages(directory, secretId, secretKey);
return 0;
}
使用以下命令编译代码:
bash
g++ -o image_ocr image_ocr.cpp -lcurl -ljsoncpp
bash
./image_ocr
通过以上步骤,你可以实现批量图片文字识别并根据识别结果自动重命名图片的功能。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。