stable-diffusion-client
Version:
smart stable-diffusion-webui client
64 lines (63 loc) • 2.38 kB
JavaScript
import { toBase64 } from "../utils.js";
import StableDiffusionResult from "./StableDiffusionResult.js";
/**
* @class ControlNetApi
* @classdesc ControlNet API, a translation layer for Mikubill's ControlNet API
* @param {StableDiffusionApi} Stable Diffusion parent API
*/
export default class ControlNetApi {
constructor(sd) {
Object.defineProperty(this, "sd", {
enumerable: true,
configurable: true,
writable: true,
value: sd
});
}
/**
* Uses the selected ControlNet proprocessor module to predict a detection
* on the input image
* @param {ControlNetDetectOptions} options
* @returns {Promise<StableDiffusionResult>} ApiResult with the detection result
* @example
* const api = new StableDiffusionApi();
* const image = sharp("image.png");
*
* const result = await api.controlnet.detect({
* controlnet_input_images: [image],
* controlnet_module: "depth",
* controlnet_processor_res: 512,
* controlnet_threshold_a: 64,
* controlnet_threshold_b: 64,
* });
*
* result.image.toFile("result.png");
*/
async detect(options) {
const input_images = await Promise.all(options.controlnet_input_images.map(async (image) => await toBase64(image)));
const response = await this.sd.post("/controlnet/detect", {
controlnet_module: options.controlnet_module ?? "none",
controlnet_input_images: input_images,
controlnet_processor_res: options.controlnet_processor_res ?? 512,
controlnet_threshold_a: options.controlnet_threshold_a ?? 64,
controlnet_threshold_b: options.controlnet_threshold_b ?? 64,
});
return new StableDiffusionResult(response);
}
/**
* Returns a list of available ControlNet models
* @returns {Promise<string[]>} List of available ControlNet models
*/
async getModels() {
const response = await this.sd.get("/controlnet/model_list");
return response.model_list;
}
/**
* Returns a list of available ControlNet modules
* @returns {Promise<string[]>} List of available ControlNet modules
*/
async getModules() {
const response = await this.sd.get("/controlnet/module_list");
return response.module_list;
}
}