Files
likedream/internal/application/recognizer/init.go
T
sugarBecomer 00bc3432bd 🐛 修复onnx环境
2025-11-11 21:48:08 +08:00

60 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package recognizer
import (
"embed"
_ "embed"
"likedream-goframe/internal/framework/logger"
"os"
"path/filepath"
"runtime"
"strings"
ort "github.com/yalue/onnxruntime_go"
"go.uber.org/zap"
)
//go:embed static/*
var fs embed.FS
// OnnxIsInit 初始化onnx配置
var OnnxIsInit = false
func InitOnnx() {
name, bytes := getOnnxRuntimeName()
// 检测文件是否存在
exePath := strings.ToLower(os.Args[0])
// 获取绝对路径
absPath, _ := filepath.Abs(exePath)
filePath := filepath.Join(filepath.Dir(absPath), name)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
os.WriteFile(filePath, bytes, 0644)
}
ort.SetSharedLibraryPath(filePath)
err := ort.InitializeEnvironment()
if err != nil {
logger.Logger.Panic("初始化onnx:", zap.String("绝对路径", filePath))
return
}
OnnxIsInit = true
}
func getOnnxRuntimeName() (onnxRuntimeFileName string, onnxRuntimeFileBytes []byte) {
systemOS := runtime.GOOS
switch systemOS {
case "windows":
onnxRuntimeFileName = "onnxruntime.dll"
onnxRuntimeFileBytes, _ = fs.ReadFile("static/onnxruntime.dll")
return
case "darwin":
onnxRuntimeFileName = "onnxruntime.dylib"
onnxRuntimeFileBytes, _ = fs.ReadFile("static/libonnxruntime.1.23.2.dylib")
return
case "linux":
onnxRuntimeFileName = "onnxruntime.so"
onnxRuntimeFileBytes, _ = fs.ReadFile("static/libonnxruntime.so.1.23.2")
return
}
logger.Logger.Panic("暂不支持的操作系统:", zap.String("系统名称", systemOS))
return "", nil
}