Skip to content

Commit

Permalink
feat: Support more Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Nov 1, 2023
1 parent 6711801 commit 29db35c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
51 changes: 28 additions & 23 deletions info.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@
"icon": "icon.png",
"display": "OCR Space",
"homepage": "https://github.com/pot-app/pot-app-recognize-plugin-template",
"needs": [{ "key": "apikey", "display": "Api Key" }],
"needs": [
{
"key": "engine",
"display": "Engine",
"type": "select",
"options": { "1": "Engine 1", "2": "Engine 2", "3": "Engine 3" }
},
{ "key": "apikey", "display": "Api Key", "type": "input" }
],
"language": {
"auto": "",
"zh_cn": "",
"zh_tw": "",
"en": "",
"ja": "",
"ko": "",
"fr": "",
"es": "",
"ru": "",
"de": "",
"it": "",
"tr": "",
"pt_pt": "",
"pt_br": "",
"vi": "",
"id": "",
"th": "",
"ms": "",
"ar": "",
"hi": "",
"mn_cy": "",
"km": ""
"auto": "eng",
"zh_cn": "chs",
"zh_tw": "cht",
"en": "eng",
"ja": "jpn",
"ko": "kor",
"fr": "fre",
"es": "spa",
"ru": "rus",
"de": "ger",
"it": "ita",
"tr": "tur",
"pt_pt": "por",
"pt_br": "por",
"vi": "vie",
"th": "tai",
"ar": "ara",
"hi": "hin",
"fa": "per"
}
}
23 changes: 16 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ pub fn recognize(
Some(apikey) => apikey.to_string(),
None => return Err("apikey not found".into()),
};

let engine = match needs.get("engine") {
Some(engine) => engine.to_string(),
None => "1".to_string(),
};
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());
form_data.insert("OCREngine", engine);
form_data.insert("language", lang.to_string());

let res: Value = client
.post("https://api.ocr.space/parse/image")
Expand All @@ -29,18 +33,22 @@ pub fn recognize(
.send()?
.json()?;

fn parse_result(res: Value) -> Option<String> {
fn parse_result(res: Value) -> Option<Result<Value, Box<dyn Error>>> {
println!("{res:?}");
if let Some(error) = res.as_object()?.get("ErrorMessage") {
return Some(Err(error.to_string().into()));
}
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);
}
Some(result)
Some(Ok(Value::String(result)))
}

if let Some(result) = parse_result(res) {
return Ok(Value::String(result));
return result;
} else {
return Err("Response Parse Error".into());
}
Expand All @@ -52,8 +60,9 @@ mod tests {
#[test]
fn try_request() {
let mut needs = HashMap::new();
needs.insert("apikey".to_string(), "".to_string());
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();
needs.insert("apikey".to_string(), "K86964409388957".to_string());
needs.insert("engine".to_string(), "1".to_string());
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=", "eng", needs).unwrap();
println!("{result}");
}
}

0 comments on commit 29db35c

Please sign in to comment.