Build and Push / build (push) Successful in 5m59s
原来那版是「RotNet(内圈) − RotNet(外圈) 取模」,纯粹自己凑的,与上游毫无对应关系,
所以结果 344 无法解释也无法校验。
按上游 solve_double_rotate 逐行移植(pkg/opencv/opencv.cpp 新增 cv_double_rotate):
1) 内圈:rot90 四次数「整行只有纯黑/纯白」的行数 → 上下左右剪边 → 按短边取偶数直径 resize
2) 外圈:从中心沿四条射线找第一个非纯黑非纯白像素 → 外扩 resize_check_pixel → 剪边 resize
3) 两边各做圆环 mask(半径 R / 内半径 R-check_pixel)得到环带
4) 内圈环带按候选角 getRotationMatrix2D+warpAffine 旋转,与外圈环带 matchTemplate
(TM_CCOEFF_NORMED) 取最大相似度;粗搜 1..360 step 10,命中更好就追加 angle±10 细化窗口,
最多保留 5 个候选(与上游边遍历边追加的行为一致)
5) inner_angle = raw_angle * speed_ratio/(speed_ratio+1)
返回结构也对齐上游:{angle(兼容), inner_angle, raw_angle, similarity};
剪不出有效环带时返回 -2,HTTP 层映射成 400。
279 lines
8.1 KiB
Go
279 lines
8.1 KiB
Go
package opencv
|
||
|
||
/*
|
||
#cgo CXXFLAGS: -std=c++17 -I/usr/include/opencv4 -I/usr/local/onnxruntime/include
|
||
#cgo linux LDFLAGS: -L/usr/lib/x86_64-linux-gnu -L/usr/local/onnxruntime/lib -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_dnn -lopencv_calib3d -lonnxruntime -lstdc++
|
||
#cgo darwin LDFLAGS: -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_dnn -lopencv_calib3d -lonnxruntime -lstdc++
|
||
|
||
#include <stdlib.h>
|
||
|
||
// 图像结构体 - 在这里定义让 Go 可以访问
|
||
typedef struct Image {
|
||
unsigned char* data;
|
||
int width;
|
||
int height;
|
||
int channels;
|
||
} Image;
|
||
|
||
typedef struct {
|
||
float x1, y1, x2, y2;
|
||
float confidence;
|
||
int class_id;
|
||
char class_name[64];
|
||
} YOLODetection;
|
||
|
||
// OpenCV 函数
|
||
Image* cv_imdecode(const unsigned char* buf, size_t size);
|
||
void cv_image_free(Image* img);
|
||
int cv_yolo_load(const char* name, const char* model_path, const char* classes_path);
|
||
int cv_yolo_detect(const char* name, const unsigned char* img_data, int width, int height, int channels, YOLODetection** detections, int* count);
|
||
void cv_yolo_detections_free(YOLODetection* detections);
|
||
void cv_yolo_unload(const char* name);
|
||
int cv_slider_match(const Image* target, const Image* background, int* out_x);
|
||
int cv_slider_comparison(const Image* target, const Image* background, int* out_x, int* out_y);
|
||
int cv_double_rotate(const Image* inner, const Image* outer, int check_pixel, double speed_ratio,
|
||
int grayscale, int anticlockwise, int cut_pixel_value,
|
||
int* out_raw_angle, double* out_inner_angle, double* out_similarity);
|
||
float cv_detect_rotation(const Image* img);
|
||
float cv_compare_similarity(const Image* img1, const Image* img2);
|
||
const char* cv_get_last_error();
|
||
*/
|
||
import "C"
|
||
import (
|
||
"encoding/base64"
|
||
"errors"
|
||
"fmt"
|
||
"unsafe"
|
||
)
|
||
|
||
// Image OpenCV 图像
|
||
type Image struct {
|
||
img *C.Image
|
||
}
|
||
|
||
// YOLODetection YOLO 检测结果
|
||
type YOLODetection struct {
|
||
X1, Y1, X2, Y2 float32
|
||
Confidence float32
|
||
ClassID int
|
||
ClassName string
|
||
}
|
||
|
||
// DecodeFromBase64 从 Base64 解码图像
|
||
func DecodeFromBase64(base64Str string) (*Image, error) {
|
||
data, err := base64.StdEncoding.DecodeString(base64Str)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
img := C.cv_imdecode((*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data)))
|
||
if img == nil {
|
||
return nil, errors.New(C.GoString(C.cv_get_last_error()))
|
||
}
|
||
|
||
return &Image{img: img}, nil
|
||
}
|
||
|
||
// Free 释放图像
|
||
func (i *Image) Free() {
|
||
if i.img != nil {
|
||
C.cv_image_free(i.img)
|
||
i.img = nil
|
||
}
|
||
}
|
||
|
||
// Width 获取宽度
|
||
func (i *Image) Width() int {
|
||
return int(i.img.width)
|
||
}
|
||
|
||
// Height 获取高度
|
||
func (i *Image) Height() int {
|
||
return int(i.img.height)
|
||
}
|
||
|
||
// Channels 获取通道数
|
||
func (i *Image) Channels() int {
|
||
return int(i.img.channels)
|
||
}
|
||
|
||
// Data 获取图像数据
|
||
func (i *Image) Data() []byte {
|
||
size := int(i.img.width) * int(i.img.height) * int(i.img.channels)
|
||
return C.GoBytes(unsafe.Pointer(i.img.data), C.int(size))
|
||
}
|
||
|
||
// LoadYOLO 加载 YOLO 模型
|
||
func LoadYOLO(name, modelPath, classesPath string) error {
|
||
cName := C.CString(name)
|
||
cModelPath := C.CString(modelPath)
|
||
defer C.free(unsafe.Pointer(cName))
|
||
defer C.free(unsafe.Pointer(cModelPath))
|
||
|
||
var cClassesPath *C.char
|
||
if classesPath != "" {
|
||
cClassesPath = C.CString(classesPath)
|
||
defer C.free(unsafe.Pointer(cClassesPath))
|
||
}
|
||
|
||
ret := C.cv_yolo_load(cName, cModelPath, cClassesPath)
|
||
if ret != 0 {
|
||
return fmt.Errorf("加载 YOLO 模型失败: %s", C.GoString(C.cv_get_last_error()))
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// DetectYOLO YOLO 检测
|
||
func DetectYOLO(name string, img *Image) ([]YOLODetection, error) {
|
||
if img == nil || img.img == nil {
|
||
return nil, errors.New("图像为空")
|
||
}
|
||
|
||
var detections *C.YOLODetection
|
||
var count C.int
|
||
|
||
cName := C.CString(name)
|
||
defer C.free(unsafe.Pointer(cName))
|
||
|
||
ret := C.cv_yolo_detect(cName, img.img.data, img.img.width, img.img.height, img.img.channels,
|
||
&detections, &count)
|
||
if ret != 0 {
|
||
return nil, fmt.Errorf("YOLO 检测失败: %s", C.GoString(C.cv_get_last_error()))
|
||
}
|
||
|
||
if count == 0 {
|
||
return []YOLODetection{}, nil
|
||
}
|
||
|
||
defer C.cv_yolo_detections_free(detections)
|
||
|
||
// 转换为 Go 类型
|
||
detectionSlice := (*[1 << 20]C.YOLODetection)(unsafe.Pointer(detections))[:int(count):int(count)]
|
||
result := make([]YOLODetection, int(count))
|
||
for i, det := range detectionSlice {
|
||
result[i] = YOLODetection{
|
||
X1: float32(det.x1),
|
||
Y1: float32(det.y1),
|
||
X2: float32(det.x2),
|
||
Y2: float32(det.y2),
|
||
Confidence: float32(det.confidence),
|
||
ClassID: int(det.class_id),
|
||
ClassName: C.GoString(&det.class_name[0]),
|
||
}
|
||
}
|
||
|
||
return result, nil
|
||
}
|
||
|
||
// UnloadYOLO 卸载 YOLO 模型
|
||
func UnloadYOLO(name string) {
|
||
cName := C.CString(name)
|
||
defer C.free(unsafe.Pointer(cName))
|
||
C.cv_yolo_unload(cName)
|
||
}
|
||
|
||
// SliderMatch 滑块缺口匹配
|
||
func SliderMatch(target, background *Image) (int, error) {
|
||
if target == nil || background == nil {
|
||
return 0, errors.New("图像为空")
|
||
}
|
||
if target.Width() > background.Width() || target.Height() > background.Height() {
|
||
return 0, fmt.Errorf("%w:滑块 %dx%d 大于背景 %dx%d",
|
||
ErrSizeMismatch, target.Width(), target.Height(), background.Width(), background.Height())
|
||
}
|
||
|
||
var x C.int
|
||
ret := C.cv_slider_match(target.img, background.img, &x)
|
||
if ret != 0 {
|
||
return 0, fmt.Errorf("滑块匹配失败: %s", C.GoString(C.cv_get_last_error()))
|
||
}
|
||
|
||
return int(x), nil
|
||
}
|
||
|
||
// ErrSizeMismatch 输入尺寸不符合算法前提。
|
||
var ErrSizeMismatch = errors.New("尺寸不匹配")
|
||
|
||
// DoubleRotate 双图旋转(环形旋转验证码):按上游 AntiCAP 的环带模板匹配实现。
|
||
//
|
||
// 返回 rawAngle(粗到细搜索出的最佳角度)、innerAngle(按 speed_ratio 折算后的小数角)、
|
||
// similarity(最佳匹配的 TM_CCOEFF_NORMED 相似度)。
|
||
// 内圈或外圈剪不出有效环带时返回 ErrSizeMismatch,调用方按参数错误处理。
|
||
func DoubleRotate(inner, outer *Image, checkPixel int, speedRatio float64,
|
||
grayscale, anticlockwise bool, cutPixelValue int) (rawAngle int, innerAngle float64, similarity float32, err error) {
|
||
if inner == nil || outer == nil {
|
||
return 0, 0, 0, errors.New("图像为空")
|
||
}
|
||
if checkPixel <= 0 {
|
||
checkPixel = 10
|
||
}
|
||
if speedRatio <= 0 {
|
||
speedRatio = 1
|
||
}
|
||
|
||
var cRaw C.int
|
||
var cInner, cSim C.double
|
||
gs, acw := C.int(0), C.int(0)
|
||
if grayscale {
|
||
gs = 1
|
||
}
|
||
if anticlockwise {
|
||
acw = 1
|
||
}
|
||
|
||
ret := C.cv_double_rotate(inner.img, outer.img, C.int(checkPixel), C.double(speedRatio),
|
||
gs, acw, C.int(cutPixelValue), &cRaw, &cInner, &cSim)
|
||
if ret != 0 {
|
||
msg := C.GoString(C.cv_get_last_error())
|
||
if ret == -2 {
|
||
return 0, 0, 0, fmt.Errorf("%w:%s", ErrSizeMismatch, msg)
|
||
}
|
||
return 0, 0, 0, fmt.Errorf("双图旋转匹配失败: %s", msg)
|
||
}
|
||
|
||
return int(cRaw), float64(cInner), float32(cSim), nil
|
||
}
|
||
|
||
// SliderComparison 阴影滑块匹配。
|
||
//
|
||
// 语义(与上游 AntiCAP solve_slider_comparison 一致):target 与 background 是
|
||
// **同一张图的两份**,只有滑块阴影所在区域不同,因此要求两者尺寸完全一致;
|
||
// 上游用 PIL 的 ImageChops.difference,尺寸不一致会直接抛异常。
|
||
func SliderComparison(target, background *Image) (int, int, error) {
|
||
if target == nil || background == nil {
|
||
return 0, 0, errors.New("图像为空")
|
||
}
|
||
if target.Width() != background.Width() || target.Height() != background.Height() {
|
||
return 0, 0, fmt.Errorf("%w:target %dx%d 与 background %dx%d 必须一致(阴影滑块是同一张图的两份)",
|
||
ErrSizeMismatch, target.Width(), target.Height(), background.Width(), background.Height())
|
||
}
|
||
|
||
var x, y C.int
|
||
ret := C.cv_slider_comparison(target.img, background.img, &x, &y)
|
||
if ret != 0 {
|
||
return 0, 0, fmt.Errorf("阴影滑块匹配失败: %s", C.GoString(C.cv_get_last_error()))
|
||
}
|
||
|
||
return int(x), int(y), nil
|
||
}
|
||
|
||
// DetectRotation 检测旋转角度
|
||
func DetectRotation(img *Image) (float32, error) {
|
||
if img == nil {
|
||
return 0, errors.New("图像为空")
|
||
}
|
||
|
||
angle := C.cv_detect_rotation(img.img)
|
||
return float32(angle), nil
|
||
}
|
||
|
||
// CompareSimilarity 比较图像相似度
|
||
func CompareSimilarity(img1, img2 *Image) (float32, error) {
|
||
if img1 == nil || img2 == nil {
|
||
return 0, errors.New("图像为空")
|
||
}
|
||
|
||
similarity := C.cv_compare_similarity(img1.img, img2.img)
|
||
return float32(similarity), nil
|
||
}
|