-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosd.cpp
63 lines (50 loc) · 1.67 KB
/
osd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <leptonica/allheaders.h>
#include <tesseract/baseapi.h>
#include <emscripten.h>
#include "emscripten/bind.h"
#include <string>
#include <sstream>
using namespace emscripten;
tesseract::TessBaseAPI* api;
int main() {
api = new tesseract::TessBaseAPI();
printf("Tesseract-ocr version: %s\n", api->Version());
// Init osd engine with osd.traineddata
api->Init("model/", "osd");
api->SetPageSegMode(tesseract::PSM_OSD_ONLY);
// tell JS the engine is ready
EM_ASM(
Module.onOsdEngineReady();
);
return 0;
}
std::string detectOrientationScript(std::string filename)
{
PIX* image = pixRead(filename.c_str());
api->SetImage(image);
int orient_deg;
float orient_conf;
const char* script_name;
float script_conf;
api->DetectOrientationScript(&orient_deg, &orient_conf, &script_name, &script_conf);
// printf("************\n Orientation in degrees: %d\n Orientation confidence: %.2f\n"
// " Script: %s\n Script confidence: %.2f\n",
// orient_deg, orient_conf,
// script_name, script_conf);
pixDestroy(&image);
std::stringstream json;
json.precision(5); // 设置浮点数精度为2位小数
json << std::fixed;
json << "{";
json << "\"orient_deg\":" << orient_deg << ",";
json << "\"orient_conf\":" << orient_conf << ",";
json << "\"script_name\":\"" << script_name << "\",";
json << "\"script_conf\":" << script_conf;
json << "}";
return json.str();
}
EMSCRIPTEN_BINDINGS(my_module)
{
function("detectOrientationScript", &detectOrientationScript);
}