-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaptchaocr.go
More file actions
48 lines (39 loc) · 894 Bytes
/
captchaocr.go
File metadata and controls
48 lines (39 loc) · 894 Bytes
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
package captchaocr
/*
#cgo pkg-config: python3
#include "pkg/python/wrapper/python_wrapper.h"
#include <stdlib.h>
*/
import "C"
import (
"errors"
"sync"
"unsafe"
)
var (
initialized bool
initializeMux sync.Mutex
)
// Initialize 初始化Python解释器和OCR引擎
func Initialize() error {
initializeMux.Lock()
defer initializeMux.Unlock()
if initialized {
return nil
}
C.init_python()
initialized = true
return nil
}
// RecognizeCaptcha 识别验证码图片
// imageData 参数应该是base64编码的图片数据
func RecognizeCaptcha(imageData string) (string, error) {
if !initialized {
return "", errors.New("captchaocr not initialized, call Initialize() first")
}
cImageData := C.CString(imageData)
defer C.free(unsafe.Pointer(cImageData))
result := C.recognize_captcha(cImageData)
defer C.free(unsafe.Pointer(result))
return C.GoString(result), nil
}