Documentation
¶
Index ¶
- func CleanRegistry()
- func Destroy()
- func Execute[T any](ctx context.Context, input *types.ImageData, ep types.ExecutionProvider, ...) (T, error)
- func Initialize(name string, onProgress types.DownloadProgress) error
- func Process(ctx context.Context, input *types.ImageData, ep types.ExecutionProvider, ...) (*types.ImageData, error)
- func SuggestEnhancements(input *types.ImageData) []types.ModelType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanRegistry ¶
func CleanRegistry()
CleanRegistry releases all resources held by registered models. It iterates through all models in the registry and calls their Destroy method to clean up memory and other resources.
This function should be called when the application is shutting down or when all model instances are no longer needed to prevent resource leaks.
func Destroy ¶
func Destroy()
Destroy cleans up resources used by the model runtime.
This function performs cleanup operations to free memory and resources allocated during initialization. It should be called when the application is shutting down or when the AI functionalities are no longer needed.
It's recommended to use this function with defer for proper cleanup:
Example: ¶
if err := opai.Initialize("myapp", nil); err != nil {
log.Fatal("Initialization failed:", err)
}
defer opai.Destroy() // Ensure cleanup on exit
func Execute ¶
func Execute[T any]( ctx context.Context, input *types.ImageData, ep types.ExecutionProvider, onProgress types.InferenceProgress, operation types.Operation, ) (T, error)
Execute executes a single image operation and returns the result as a generic data type.
The function selects the appropriate AI model for the operation and runs its inference on the image. The output is not an image, but the information data returned by the model.
Parameters: ¶
- ctx: A context object that can be used to cancel the operation.
- input: The input image data to be processed.
- ep: The execution provider (CPU, CUDA, etc.) to use for inference.
- onProgress: A callback function called with the progress of the current operation (0-1).
- operation: The operation to apply to the image.
Returns: ¶
- T: The result of the operation with the specified generic type
- error: An error if model selection fails, the operation fails, or the operation type is not supported
Example: ¶
faces, err := Execute[[]types.Face](ctx, inputImage, progressCallback, faceDetectionOp)
func Initialize ¶
func Initialize(name string, onProgress types.DownloadProgress) error
Initialize sets up the model runtime by ensuring all required dependencies are available.
This function performs a two-step initialization process:
- Installs the ONNX runtime if not already present in the user's config directory
- Initializes the ONNX runtime
The name parameter specifies the application name used to create a dedicated config directory under the user's standard configuration path (e.g., ~/.config/name on Linux). It's important that you reuse the same name on later calls to Initialize() to ensure that the same config directory is used.
Returns an error if any step fails, including:
- Unable to create config directories or files
- ONNX runtime initialization failures
Example: ¶
err := opai.Initialize("myapp", nil)
if err != nil {
log.Fatal("Failed to initialize:", err)
}
defer opai.Destroy() // Clean up resources
func Process ¶
func Process( ctx context.Context, input *types.ImageData, ep types.ExecutionProvider, onProgress types.InferenceProgress, operations ...types.Operation, ) (*types.ImageData, error)
Process processes an image through a sequence of image operations.
The function selects the appropriate AI model for each operation and runs its inference on the image. If multiple operations are provided, they are applied in the order specified. The output is the final processed image after all operations are applied.
Parameters: ¶
- ctx: A context object that can be used to cancel the operation.
- input: The input image data to be processed.
- ep: The execution provider (CPU, CUDA, etc.) to use for inference.
- onProgress: A callback function called with the progress of the current operation (0-1).
- operations: A variable number of operations to apply sequentially.
Returns: ¶
- *types.ImageData: The final processed image data after all operations
- error: An error if model selection fails, any operation fails, or no operations are provided
Example: ¶
output, err := Process(ctx, inputImage, faceRecoveryOp, upscaleOp)
func SuggestEnhancements ¶
SuggestEnhancements analyzes the input image and returns a list of recommended enhancement.
It evaluates the image for potential face recovery, light adjustment, and upscaling improvements based on image characteristics such as detected faces and resolution.
Types ¶
This section is empty.