หน้านี้อธิบายวิธีใช้ความสามารถในการปรับแต่งจาก Imagen เพื่อแก้ไขหรือสร้างรูปภาพตามสไตล์ที่ระบุ โดยใช้ SDK ของ Firebase AI Logic
วิธีการทำงาน: คุณระบุพรอมต์ข้อความและรูปภาพอ้างอิงอย่างน้อย 1 รูปที่แสดงสไตล์ที่เฉพาะเจาะจง (เช่น รูปแบบ พื้นผิว หรือสไตล์การออกแบบ) โมเดลจะใช้ข้อมูลที่ป้อนเหล่านี้เพื่อสร้างรูปภาพใหม่ตามสไตล์ที่ระบุในรูปภาพอ้างอิง
เช่น คุณสร้างรูปภาพใหม่ของห้องครัวโดยอิงตามรูปภาพจากแคตตาล็อกค้าปลีกยอดนิยมที่คุณระบุได้
ก่อนเริ่มต้น
ใช้ได้เมื่อใช้ Vertex AI Gemini API เป็นผู้ให้บริการ API เท่านั้น |
หากยังไม่ได้ดำเนินการ ให้ทำตามคู่มือเริ่มต้นใช้งาน ซึ่งอธิบายวิธีตั้งค่าโปรเจ็กต์ Firebase, เชื่อมต่อแอปกับ Firebase, เพิ่ม SDK, เริ่มต้นบริการแบ็กเอนด์สำหรับผู้ให้บริการ API ที่เลือก และสร้างอินสแตนซ์ ImagenModel
รุ่นที่รองรับความสามารถนี้
Imagen มีบริการแก้ไขรูปภาพผ่านโมเดล capability
ดังนี้
imagen-3.0-capability-001
โปรดทราบว่าสำหรับโมเดล Imagen ระบบไม่รองรับตำแหน่ง global
ส่งคำขอปรับแต่งสไตล์
ตัวอย่างต่อไปนี้แสดงคำขอปรับแต่งสไตล์ที่ขอให้โมเดล สร้างรูปภาพใหม่ที่มีสไตล์ของรูปภาพอ้างอิงที่ระบุ (ในตัวอย่างนี้คือ "Starry Night" ของ Van Gogh)
ดูเทมเพลตพรอมต์ในส่วนท้ายของหน้านี้เพื่อดูข้อมูลเกี่ยวกับการเขียนพรอมต์และวิธีใช้รูปภาพอ้างอิงภายในพรอมต์
Swift
Swift ไม่รองรับการแก้ไขรูปภาพด้วยโมเดล Imagen โปรดกลับมาดูอีกครั้งในปลายปีนี้
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun customizeImage() {
// Initialize the Vertex AI Gemini API backend service
// Optionally specify the location to access the model (for example, `us-central1`)
val ai = Firebase.ai(backend = GenerativeBackend.vertexAI(location = "us-central1"))
// Create an `ImagenModel` instance with an Imagen "capability" model
val model = ai.imagenModel("imagen-3.0-capability-001")
// This example assumes 'referenceImage' is a pre-loaded Bitmap.
// In a real app, this might come from the user's device or a URL.
val referenceImage: Bitmap = TODO("Load your reference image Bitmap here")
// Define the style reference using the reference image.
val styleReference = ImagenStyleReference(
image = referenceImage,
referenceID = 1,
description = "Van Gogh style"
)
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the style reference with ID 1.
val prompt = "A cat flying through outer space, in the Van Gogh style[1]"
// Use the editImage API to perform the style customization.
// Pass the list of references, the prompt, and an editing configuration.
val editedImage = model.editImage(
references = listOf(styleReference),
prompt = prompt,
config = ImagenEditingConfig(
editSteps = 50 // Number of editing steps, a higher value can improve quality
)
)
// Process the result
}
Java
// Initialize the Vertex AI Gemini API backend service
// Optionally specify the location to access the model (for example, `us-central1`)
// Create an `ImagenModel` instance with an Imagen "capability" model
ImagenModel imagenModel = FirebaseAI.getInstance(GenerativeBackend.vertexAI("us-central1"))
.imagenModel(
/* modelName */ "imagen-3.0-capability-001");
ImagenModelFutures model = ImagenModelFutures.from(imagenModel);
// This example assumes 'referenceImage' is a pre-loaded Bitmap.
// In a real app, this might come from the user's device or a URL.
Bitmap referenceImage = null; // TODO("Load your image Bitmap here");
// Define the style reference using the reference image.
ImagenStyleReference subjectReference = new ImagenStyleReference.Builder()
.setImage(referenceImage)
.setReferenceID(1)
.setDescription("Van Gogh style")
.build();
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the style reference with ID 1.
String prompt = "A cat flying through outer space, in the Van Gogh style[1]";
// Define the editing configuration.
ImagenEditingConfig imagenEditingConfig = new ImagenEditingConfig.Builder()
.setEditSteps(50) // Number of editing steps, a higher value can improve quality
.build();
// Use the editImage API to perform the style customization.
// Pass the list of references, the prompt, and the editing configuration.
Futures.addCallback(model.editImage(Collections.singletonList(styleReference), prompt, imagenEditingConfig), new FutureCallback<ImagenGenerationResponse>() {
@Override
public void onSuccess(ImagenGenerationResponse result) {
if (result.getImages().isEmpty()) {
Log.d("TAG", "No images generated");
}
Bitmap bitmap = result.getImages().get(0).asBitmap();
// Use the bitmap to display the image in your UI
}
@Override
public void onFailure(Throwable t) {
// ...
}
}, Executors.newSingleThreadExecutor());
Web
แอปบนเว็บไม่รองรับการแก้ไขรูปภาพด้วยโมเดล Imagen โปรดกลับมาดูอีกครั้งในปลายปีนี้
Dart
import 'dart:typed_data';
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI Gemini API backend service
// Optionally specify a location to access the model (for example, `us-central1`)
final ai = FirebaseAI.vertexAI(location: 'us-central1');
// Create an `ImagenModel` instance with an Imagen "capability" model
final model = ai.imagenModel(model: 'imagen-3.0-capability-001');
// This example assumes 'referenceImage' is a pre-loaded Uint8List.
// In a real app, this might come from the user's device or a URL.
final Uint8List referenceImage = Uint8List(0); // TODO: Load your reference image data here
// Define the style reference using the reference image.
final styleReference = ImagenStyleReference(
image: referenceImage,
referenceId: 1,
description: 'Van Gogh style',
);
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the style reference with ID 1.
final prompt = "A cat flying through outer space, in the Van Gogh style[1]";
try {
// Use the editImage API to perform the style customization.
// Pass the list of references, the prompt, and an editing configuration.
final response = await model.editImage(
[styleReference],
prompt,
config: ImagenEditingConfig(
editSteps: 50, // Number of editing steps, a higher value can improve quality
),
);
// Process the result.
if (response.images.isNotEmpty) {
final editedImage = response.images.first.bytes;
// Use the editedImage (a Uint8List) to display the image, save it, etc.
print('Image successfully generated!');
} else {
// Handle the case where no images were generated.
print('Error: No images were generated.');
}
} catch (e) {
// Handle any potential errors during the API call.
print('An error occurred: $e');
}
Unity
Unity ไม่รองรับการแก้ไขรูปภาพด้วยโมเดล Imagen โปรดกลับมาดูอีกครั้งในปลายปีนี้
เทมเพลตพรอมต์
ในคำขอ คุณจะระบุรูปภาพอ้างอิง (สูงสุด 4 รูปภาพ) ได้โดยกำหนดImagenStyleReference
ซึ่งคุณจะระบุรหัสอ้างอิงสำหรับรูปภาพ
(และคำอธิบายสไตล์ (ไม่บังคับ) ได้ด้วย) โปรดทราบว่ารูปภาพหลายรูปอาจมีรหัสอ้างอิงเดียวกันได้ (เช่น รูปภาพหลายรูปของลวดลายเดียวกัน)
จากนั้นเมื่อเขียนพรอมต์ ให้อ้างอิงถึงรหัสเหล่านี้ เช่น คุณใช้
[1]
ในพรอมต์เพื่ออ้างอิงถึงรูปภาพที่มีรหัสอ้างอิง 1
หากคุณระบุ
คำอธิบายเรื่อง คุณก็ใส่คำอธิบายนั้นในพรอมต์ได้ด้วยเพื่อให้มนุษย์อ่านพรอมต์ได้ง่ายขึ้น
ตารางต่อไปนี้แสดงเทมเพลตพรอมต์ที่สามารถใช้เป็นจุดเริ่มต้น ในการเขียนพรอมต์เพื่อการปรับแต่งตามสไตล์
กรณีการใช้งาน | รูปภาพอ้างอิง | เทมเพลตพรอมต์ | ตัวอย่าง |
---|---|---|---|
รูปแบบออบเจ็กต์ | รูปภาพตัวแบบ (1-4) | สร้างรูปภาพใน STYLE_DESCRIPTION [1] ตาม คำบรรยายภาพต่อไปนี้ IMAGE_DESCRIPTION | สร้างรูปภาพใน neon sign style [1] ตาม คำบรรยายภาพต่อไปนี้ a sign saying have a great day |
การปรับแต่งสไตล์รูปภาพบุคคลโดยไม่ต้องป้อนข้อมูล Face Mesh | รูปภาพตัวแบบ (1-4) | สร้างรูปภาพเกี่ยวกับ SUBJECT_DESCRIPTION [1] ให้ตรงกับ คำอธิบาย: ภาพบุคคลของ SUBJECT_DESCRIPTION [1] ${PROMPT} | สร้างรูปภาพเกี่ยวกับa woman with short hair[1]ให้ตรงกับ คำอธิบาย: ภาพบุคคลของa woman with short hair[1] ใน สไตล์การ์ตูน 3 มิติที่มีพื้นหลังเบลอ ตัวละครน่ารัก มีใบหน้ายิ้มแย้ม มองกล้อง โทนสีพาสเทล ... |
การปรับแต่งสไตล์รูปภาพบุคคลด้วยอินพุต Face Mesh |
รูปภาพวัตถุ (1-3) รูปภาพควบคุม Facemesh (1) |
สร้างรูปภาพเกี่ยวกับ SUBJECT_DESCRIPTION [1] ในท่าทางของ CONTROL_IMAGE [2] ให้ตรงกับคำอธิบาย: ภาพบุคคลของ SUBJECT_DESCRIPTION [1] ${PROMPT} | สร้างรูปภาพเกี่ยวกับa woman with short hair [1]ในท่าทาง ของcontrol image [2]ให้ตรงกับคำอธิบาย: ภาพบุคคล ของa woman with short hair [1] ในสไตล์การ์ตูน 3 มิติที่มี พื้นหลังเบลอ ตัวละครน่ารักน่าเอ็นดูที่มีใบหน้ายิ้มแย้ม มองกล้อง โทนสีพาสเทล ... |
แนวทางปฏิบัติแนะนำและข้อจำกัด
Use Case
ความสามารถในการปรับแต่งช่วยให้คุณป้อนพรอมต์ได้อย่างอิสระ ซึ่งอาจทำให้เกิดความเข้าใจว่าโมเดลทำได้มากกว่าที่ได้รับการฝึกมา ส่วนต่อไปนี้จะอธิบายUse Case ที่ตั้งใจสำหรับการปรับแต่ง และตัวอย่างUse Case ที่ไม่ได้ตั้งใจ
เราขอแนะนำให้ใช้ความสามารถนี้สำหรับกรณีการใช้งานที่ต้องการ เนื่องจากเราได้ ฝึกโมเดลในกรณีการใช้งานเหล่านั้นและคาดว่าจะได้ผลลัพธ์ที่ดี ในทางกลับกัน หากคุณพยายามให้โมเดลทําสิ่งต่างๆ นอกเหนือจากกรณีการใช้งานที่ตั้งใจไว้ คุณก็ควรคาดหวังผลลัพธ์ที่ไม่ดี
กรณีการใช้งานที่ตั้งใจไว้
กรณีการใช้งานที่ตั้งใจไว้สำหรับการปรับแต่งตามสไตล์มีดังนี้
สร้างรูปภาพจากข้อความที่ป้อนตามสไตล์เฉพาะที่ระบุโดย รูปภาพอ้างอิง
ดัดแปลงรูปภาพของบุคคล
ดัดแปลงรูปภาพบุคคลและคงสีหน้าของบุคคลนั้นไว้
ตัวอย่างกรณีการใช้งานที่ไม่พึงประสงค์
ต่อไปนี้คือรายการ Use Case ที่ไม่พึงประสงค์โดยสังเขปสำหรับการ ปรับแต่งตามสไตล์ โมเดลไม่ได้ผ่านการฝึกสำหรับกรณีการใช้งานเหล่านี้ และมีแนวโน้มที่จะให้ผลลัพธ์ที่ไม่ดี
สร้างรูปภาพจากข้อความและใช้รูปภาพอ้างอิงโดยมีจุดประสงค์เพื่อ ควบคุมองค์ประกอบที่สร้างขึ้นจากรูปภาพอ้างอิงได้ในระดับหนึ่ง
สร้างรูปภาพบุคคลจากรูปภาพอ้างอิงที่มีบุคคลซึ่งมี สีหน้าเฉพาะ
วางบุคคล 2 คนในฉากที่แตกต่างกัน คงเอกลักษณ์ของบุคคลเหล่านั้นไว้ และในขณะที่ ระบุสไตล์ของรูปภาพเอาต์พุต (เช่น ภาพวาดสีน้ำมัน) โดยใช้ รูปภาพอ้างอิง
จัดรูปแบบรูปภาพสัตว์เลี้ยงและเปลี่ยนเป็นภาพวาด พร้อมทั้งคงหรือ ระบุองค์ประกอบของรูปภาพ
วางผลิตภัณฑ์ เช่น คุกกี้หรือโซฟา ในฉากต่างๆ โดยมีมุมผลิตภัณฑ์ที่แตกต่างกัน และทำตามสไตล์รูปภาพที่เฉพาะเจาะจง (เช่น สมจริงที่มีสี สไตล์แสง หรือภาพเคลื่อนไหวที่เฉพาะเจาะจง)