Skip to content

Commit

Permalink
Google OCR Plugin First Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Sep 6, 2023
1 parent 8afd2aa commit e28658b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
Binary file added google-cloud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed icon.png
Binary file not shown.
10 changes: 5 additions & 5 deletions info.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "[plugin].com.pot-app.ocrspace",
"id": "[plugin].cn.org.integral.google",
"plugin_type": "recognize",
"icon": "icon.png",
"display": "OCR Space",
"homepage": "https://github.com/pot-app/pot-app-recognize-plugin-template",
"needs": [{ "key": "apikey", "display": "Api Key" }],
"icon": "google-cloud.png",
"display": "Google OCR",
"homepage": "https://github.com/Integral-Tech/pot-app-recognize-plugin-google",
"needs": [{ "key": "api_key", "display": "API 密钥" }],
"language": {
"auto": "",
"zh_cn": "",
Expand Down
67 changes: 38 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error;
use serde_json::{json, Value};
use std::{collections::HashMap, error::Error};

#[no_mangle]
pub fn recognize(
base64: &str, // 图像Base64
lang: &str, // 识别语言
// (pot会根据info.json 中的 language 字段传入插件需要的语言代码,无需再次转换)
needs: HashMap<String, String>, // 插件需要的其他参数,由info.json定义
base64: &str, // Base64 Code of the Image
lang: &str, // Language
needs: HashMap<String, String>, // Other arguments defined in info.json
) -> Result<String, Box<dyn Error>> {
let client = reqwest::blocking::ClientBuilder::new().build()?;

let apikey = match needs.get("apikey") {
Some(apikey) => apikey.to_string(),
None => return Err("apikey not found".into()),
let api_key = match needs.get("api_key") {
Some(api_key) => api_key.to_string(),
None => return Err("API key not found!".into()),
};

let base64 = format!("data:image/png;base64,{}", base64);
let mut form_data = HashMap::new();
form_data.insert("base64Image", base64);
form_data.insert("OCREngine", "2".to_string());
let json_data = json!({
"requests": [ {
"image": { "content": base64 },
"features": { "type": "TEXT_DETECTION" },
}]
});

let res: Value = client
.post("https://api.ocr.space/parse/image")
.header("apikey", apikey)
.header("content-type", "application/x-www-form-urlencoded")
.form(&form_data)
let res = client
.post(format!(
"https://vision.googleapis.com/v1/images:annotate?key={api_key}"
))
.header("content-type", "application/json; charset=utf-8")
.json(&json_data)
.send()?
.json()?;

fn parse_result(res: Value) -> Option<String> {
let result_list = res.as_object()?.get("ParsedResults")?.as_array()?;
let mut result = String::new();
for i in result_list {
let parsed_text = i.as_object()?.get("ParsedText")?.as_str()?;
result.push_str(parsed_text);
}
let result = res
.as_object()?
.get("responses")?
.as_array()?
.get(0)?
.as_object()?
.get("fullTextAnnotation")?
.as_object()?
.get("text")?
.to_string();

Some(result)
}

if let Some(result) = parse_result(res) {
return Ok(result);
Ok(result)
} else {
return Err("Response Parse Error".into());
Err("Response Parse Error".into())
}
}

Expand All @@ -52,7 +57,11 @@ mod tests {
#[test]
fn try_request() {
let mut needs = HashMap::new();
needs.insert("apikey".to_string(), "".to_string());
needs.insert(
"api_key".to_string(),
"".to_string(), // Fill in your API key here
);

let result = recognize("iVBORw0KGgoAAAANSUhEUgAAADsAAAAeCAYAAACSRGY2AAAAAXNSR0IArs4c6QAAArNJREFUWEftl19IU1Ecxz+O5uQiNTCJkNj0ZWhkSOyh7CEy0CWZQQoTWYgvk17KFAdr9GBBYGb/qD0oUpgSCZViGkTRQ/hwEVOYIIhlMF8kUjbGZGPFdGtrGvcWzTa79/Gec+79fb7fc36/38nQ6/Xf+E+eDAV2mzqdns6WtDNRqYP5UQ71D8i2RoGVLdW/mqg4K6287G3sqHtEdYEP8clrdpZXYdCCxzWE/dkHjp5poXa/AMEVZodvU+ea2/Dn0n2NnK8wYsgVQAWEAng+TfHiZTddy75NI83LtdBRfSS2xruIONKNNftccs9sFPbLkpqcXUCmei1At2uO3YU6CKnR7AhDLDJ204bdH4u/tKSdjkodmvCrEKz6A2iE9fWEVhAftmF1JwBnmxm0msjPinzHH2A1U42GFcSJZYzGJCaodVhYnRqgZngUCmw8rStC419gzOnA7iuio8HG8b3wccTC2clIkFkWhppPkKcK4H7bTev7cWbDQ5kHcZxqorpQAO8M929dp+eHPgJtNXepNajh6wx9j+9E3BeoONBCc7mOnCx18rJxFDYGYmbwson85Sm67nXSB9SXO7loFPCIDzj2anwtdOPhTpxlueB+h7W3BzF+w6pM9F8wYxACTPc30jAfHTTR22ymeMP78HicEMkqPX8Ku5kAMV6Ba/VOKvQJu4GIkCzx5sYlWuOOxE8CphcsbBQxjBOFXeD5VQftiekr2aUnOc4qsNvV2W12ZuVlYx9irxWrO82zMXLqbFz5WseVqLNlOnKyU7DOhkP/qx2Uysf05BLFJVvQQf1uUxHdmIY9Fq5UxfW5wQCezxK9sbYKx+mTGPMi/fRW9cbSd4rUnyH71pP6KNIRKrDSGqXnDMXZ9PRNOmrF2USNtFotXq+XYDAoLV8Kz5DlrAKbwg7+KrTvuhRWXxXeDuUAAAAASUVORK5CYII=", "", needs).unwrap();
println!("{result}");
}
Expand Down

0 comments on commit e28658b

Please sign in to comment.