Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DetectOrientationScript method #299

Merged
merged 7 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ func TestClient_HTML(t *testing.T) {
})
}

func TestClient_DetectOrientationScript(t *testing.T) {
client := NewClient()
defer client.Close()
client.SetImage("./test/data/004-rotated-text.png")
deg, _, script_name, _, err := client.DetectOrientationScript()
Expect(t, err).ToBe(nil)
Expect(t, deg).ToBe(180)
Expect(t, script_name).ToBe("Latin")
}

func TestGetAvailableLangs(t *testing.T) {
t.Skip("TODO")
// langs, err := GetAvailableLanguages()
Expand Down
49 changes: 48 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,42 @@
return nil
}

// Initialize tesseract::TessBaseAPI for OSD (Orientation and Script Detection)
func (client *Client) initOsd() error {
var tessdataPrefix *C.char
if client.TessdataPrefix != "" {
tessdataPrefix = C.CString(client.TessdataPrefix)
}

Check warning on line 320 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L319-L320

Added lines #L319 - L320 were not covered by tests
defer C.free(unsafe.Pointer(tessdataPrefix))

res := C.Init(client.api, tessdataPrefix, C.CString("osd"), nil, nil)
if res != 0 {
return fmt.Errorf("failed to initialize TessBaseAPI with code %d", res)
}

Check warning on line 326 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L325-L326

Added lines #L325 - L326 were not covered by tests

if err := client.setVariablesToInitializedAPI(); err != nil {
return err
}

Check warning on line 330 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L329-L330

Added lines #L329 - L330 were not covered by tests

if client.pixImage == nil {
return fmt.Errorf("PixImage is not set, use SetImage or SetImageFromBytes before DetectOrientationScript")
}

Check warning on line 334 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L333-L334

Added lines #L333 - L334 were not covered by tests

C.SetPixImage(client.api, client.pixImage)

client.shouldInit = true

return nil
}

// This method flag the current instance to be initialized again on the next call to a function that
// requires a gosseract API initialized: when user change the config file or the languages
// the instance needs to init a new gosseract api
func (client *Client) flagForInit() {
client.shouldInit = true
}

// This method sets all the sspecified variables to TessBaseAPI structure.
// This method sets all the specified variables to TessBaseAPI structure.
// Because `api->SetVariable` must be called after `api->Init()`,
// gosseract.Client.SetVariable cannot call `api->SetVariable` directly.
// See https://zdenop.github.io/tesseract-doc/classtesseract_1_1_tess_base_a_p_i.html#a2e09259c558c6d8e0f7e523cbaf5adf5
Expand Down Expand Up @@ -376,6 +404,25 @@
return
}

func (client *Client) DetectOrientationScript() (int, float32, string, float32, error) {
if err := client.initOsd(); err != nil {
return 0, 0, "", 0, err
}

Check warning on line 410 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L409-L410

Added lines #L409 - L410 were not covered by tests
var (
orient_deg C.int
orient_conf C.float
script_name *C.char
script_conf C.float
)
defer C.free(unsafe.Pointer(script_name))
C.DetectOrientationScript(client.api, &orient_deg, &orient_conf, &script_name, &script_conf)
if script_name == nil {
return int(orient_deg), float32(orient_conf), "", float32(script_conf), fmt.Errorf("script name is null")
}

Check warning on line 421 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L420-L421

Added lines #L420 - L421 were not covered by tests

return int(orient_deg), float32(orient_conf), C.GoString(script_name), float32(script_conf), nil
}

// BoundingBox contains the position, confidence and UTF8 text of the recognized word
type BoundingBox struct {
Box image.Rectangle
Expand Down
5 changes: 5 additions & 0 deletions tessbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ char* HOCRText(TessBaseAPI a) {
return api->GetHOCRText(0);
}

void DetectOrientationScript(TessBaseAPI a, int* orient_deg, float* orient_conf, const char** script_name, float* script_conf) {
tesseract::TessBaseAPI* api = (tesseract::TessBaseAPI*)a;
api->DetectOrientationScript(orient_deg, orient_conf, script_name, script_conf);
}

bounding_boxes* GetBoundingBoxesVerbose(TessBaseAPI a) {
using namespace tesseract;
tesseract::TessBaseAPI* api = (tesseract::TessBaseAPI*)a;
Expand Down
1 change: 1 addition & 0 deletions tessbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void SetPageSegMode(TessBaseAPI, int);
int GetPageSegMode(TessBaseAPI);
char* UTF8Text(TessBaseAPI);
char* HOCRText(TessBaseAPI);
void DetectOrientationScript(TessBaseAPI, int*, float*, const char**, float*);
const char* Version(TessBaseAPI);
const char* GetDataPath();

Expand Down
Binary file added test/data/004-rotated-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/runtimes/alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN apk add \
musl-dev \
go \
tesseract-ocr-dev
RUN apk add tesseract-ocr-data-eng
RUN apk add tesseract-ocr-data-osd tesseract-ocr-data-eng

ENV GOPATH=/root/go

Expand Down
Loading