import os
import requests
# Constants
ENDPOINT = "https://hyperhuman.deemos.com/api/v2/rodin"
API_KEY = "your api key" # Replace with your actual API key
IMAGE_PATH = "/path/to/your/image.jpg" # Replace with the path to your image
# Read the image file
with open(IMAGE_PATH, 'rb') as image_file:
image_data = image_file.read()
# Prepare the multipart form data
files = {
'images': (os.path.basename(IMAGE_PATH), image_data, 'image/jpeg')
}
# Prepare the headers
headers = {
'Authorization': f'Bearer {API_KEY}',
}
# Make the POST request
response = requests.post(ENDPOINT, files=files, headers=headers)
# Parse and return the JSON response
print(response.json())
package main
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
const BaseURI = "https://hyperhuman.deemos.com/api"
type CommonError struct {
Error *string `json:"error,omitempty"`
Message *string `json:"message,omitempty"`
}
type JobSubmissionResponse struct {
Uuids []string `json:"uuids"`
SubscriptionKey string `json:"subscription_key"`
}
type RodinAllInOneResponse struct {
CommonError
Uuid *string `json:"uuid,omitempty"`
Jobs JobSubmissionResponse `json:"jobs,omitempty"`
}
func RunRodin(token string, filePath string) (*RodinAllInOneResponse, error) {
var err error
var buffer bytes.Buffer
// Create the form data for Rodin API
writer := multipart.NewWriter(&buffer)
// Read the image
image, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
// Add the image as a form entry
fieldWriter, err := writer.CreateFormFile("images", filepath.Base(filePath))
if err != nil {
return nil, err
}
if _, err = fieldWriter.Write(image); err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
// Create the request
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v2/rodin", BaseURI), &buffer)
if err != nil {
return nil, err
}
// Set headers
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var responseData RodinAllInOneResponse
err = json.NewDecoder(resp.Body).Decode(&responseData)
if err != nil {
return nil, err
}
if responseData.Error != nil {
return nil, fmt.Errorf("%s: %s", *responseData.Error, *responseData.Message)
}
return &responseData, nil
}
func main() {
// Replace with your actual API key
token := "your api key"
// Replace with the path to your image
resp, _ := RunRodin(token, "/path/to/your/image.jpg")
fmt.Println(resp)
}
响应
当您向 Rodin API 发送 POST 请求时,服务器会返回一个 JSON 响应。以下是响应的结构及其组成部分的解释。
生成 API 是耗时且资源密集型的,因此我们将其设计为异步。这意味着您提交一个任务后不会立即获得结果。
通过状态检查 和 下载 API 节点, 您可以检查已提交作业的状态并下载任务的结果。 建议您使用状态检查 API 以确保任务准备好下载,如我们在最小示例 中演示的,以避免意外结果。
作为一个快速示例,我们将展示如何获取下载结果的 URL 列表,假设您的任务已经准备好下载。
export RODIN_API_KEY="your api key"
curl -X 'POST' \
'https://hyperhuman.deemos.com/api/v2/download' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"task_uuid": "{REPLACE TASK UUID YOU GOT FROM LAST STEP}"
}'
unset RODIN_API_KEY
import requests
# Constants
ENDPOINT = "https://hyperhuman.deemos.com/api/v2/download"
API_KEY = "your api key" # Replace with your actual API key
TASK_UUID = "your-task-uuid" # Replace with your actual task UUID
# Prepare the headers
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {API_KEY}',
}
# Prepare the JSON payload
data = {
"task_uuid": TASK_UUID
}
# Make the POST request
response = requests.post(ENDPOINT, headers=headers, json=data)
# Parse and return the JSON response
print(response.json())