Generate Stunning Images with the mrhan1993/fooocus-api Cognitive Actions

In the realm of AI-driven creativity, the mrhan1993/fooocus-api offers powerful Cognitive Actions designed to enhance image generation through customizable parameters. These pre-built actions streamline the process of creating unique images, allowing developers to harness the power of AI while focusing on their application’s core functionality. In this article, we will delve into the primary action available in this API — generating images with custom parameters.
Prerequisites
Before you start integrating the Cognitive Actions into your applications, ensure you have the following:
- API Key: You will need an API key to authenticate your requests to the Cognitive Actions platform. This key is typically passed in the headers of your API calls.
- Basic Knowledge of JSON: The API expects input in JSON format, so familiarity with structuring JSON payloads is essential.
Cognitive Actions Overview
Generate Image with Custom Parameters
The Generate Image with Custom Parameters action allows developers to create images tailored to specific requirements. With the ability to set parameters like image seed, sharpness, and guidance scale, this action provides a high degree of customization, enabling automated refinement through customizable LoRAs and inpainting settings.
Input
The action requires a structured input JSON payload, which includes several fields to customize the image generation process:
{
"prompt": "",
"imageSeed": -1,
"sharpness": 2,
"imageNumber": 1,
"upscaleValue": 0,
"guidanceScale": 7,
"refinerSwitch": 0.5,
"negativePrompt": "",
"controlNetType1": "ImagePrompt",
"controlNetType2": "ImagePrompt",
"controlNetType3": "ImagePrompt",
"controlNetType4": "ImagePrompt",
"lorasCustomUrls": "",
"styleSelections": "Fooocus V2,Fooocus Enhance,Fooocus Sharp",
"useDefaultLoras": true,
"outpaintSelections": "",
"outpaintDistanceTop": 0,
"outpaintDistanceLeft": 0,
"performanceSelection": "Speed",
"aspectRatiosSelection": "1152*896",
"outpaintDistanceRight": 0,
"outpaintDistanceBottom": 0,
"inpaintAdditionalPrompt": "",
"upscaleOrVariationMethod": "Disabled"
}
Key Fields:
- prompt: Text-based input for image generation.
- imageSeed: Integer seed for randomization (use -1 for a random seed).
- sharpness: Controls the sharpness of the generated image (0-30).
- imageNumber: Number of images to generate (1-8).
- guidanceScale: Influences the adherence to the prompt (1-30).
- styleSelections: Styles to apply for image generation, comma-separated.
Output
Upon successful execution, the action returns a JSON response containing the paths of the generated images and the seeds used:
{
"paths": [
"https://assets.cognitiveactions.com/invocations/d5680289-50b7-44eb-b0e5-578a9d5e7345/4b4a1dc3-0c36-4be3-bd03-c4f6abdf5eca.png"
],
"seeds": [
2515510486718185500
]
}
Output Structure:
- paths: Array of URLs pointing to the generated images.
- seeds: Array of integer seeds used in the generation process.
Conceptual Usage Example (Python)
Here’s how you might call the Generate Image with Custom Parameters action using a conceptual Python snippet:
import requests
import json
# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint
action_id = "e9c2f0d9-a759-4e68-a78d-a9a87ffad65d" # Action ID for Generate Image with Custom Parameters
# Construct the input payload based on the action's requirements
payload = {
"prompt": "",
"imageSeed": -1,
"sharpness": 2,
"imageNumber": 1,
"upscaleValue": 0,
"guidanceScale": 7,
"refinerSwitch": 0.5,
"negativePrompt": "",
"controlNetType1": "ImagePrompt",
"controlNetType2": "ImagePrompt",
"controlNetType3": "ImagePrompt",
"controlNetType4": "ImagePrompt",
"lorasCustomUrls": "",
"styleSelections": "Fooocus V2,Fooocus Enhance,Fooocus Sharp",
"useDefaultLoras": True,
"outpaintSelections": "",
"outpaintDistanceTop": 0,
"outpaintDistanceLeft": 0,
"performanceSelection": "Speed",
"aspectRatiosSelection": "1152*896",
"outpaintDistanceRight": 0,
"outpaintDistanceBottom": 0,
"inpaintAdditionalPrompt": "",
"upscaleOrVariationMethod": "Disabled"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload} # Hypothetical structure
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body: {e.response.text}")
In this code snippet, replace the placeholder YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key to authenticate your request successfully. The payload variable contains the data structure needed to invoke the image generation action. This example showcases how to send a request to the hypothetical Cognitive Actions endpoint and handle the response.
Conclusion
The mrhan1993/fooocus-api provides a robust method to generate images using customizable parameters, making it an indispensable tool for developers looking to integrate AI-driven image generation into their applications. With the ability to control various aspects of the image creation process, the possibilities are endless. Explore this action further, experiment with different parameters, and enhance your applications with stunning visuals!