โมเดล Gemini สามารถประมวลผลรูปภาพได้ ซึ่งช่วยให้นักพัฒนาแอปจำนวนมากสามารถใช้กรณีการใช้งานขั้นสูงได้ ซึ่งก่อนหน้านี้ต้องใช้โมเดลเฉพาะโดเมน ความสามารถบางอย่างของ Gemini ด้านภาพ ได้แก่
- ใส่คำบรรยายและตอบคำถามเกี่ยวกับรูปภาพ
- ถอดเสียงและหาเหตุผลใน PDF รวมถึงโทเค็นสูงสุด 2 ล้านรายการ
- ตรวจหาวัตถุในรูปภาพและแสดงพิกัดของกรอบล้อมรอบ
- แบ่งกลุ่มวัตถุภายในรูปภาพ
Gemini สร้างขึ้นให้ทำงานได้หลายรูปแบบตั้งแต่ต้น และเราจะพัฒนาขีดความสามารถต่อไป คู่มือนี้จะแสดงวิธีใช้ Gemini API เพื่อสร้างคำตอบที่เป็นข้อความตามอินพุตรูปภาพและทำงานทั่วไปด้านการทําความเข้าใจรูปภาพ
ก่อนเริ่มต้น
ก่อนเรียกใช้ Gemini API โปรดตรวจสอบว่าคุณได้ติดตั้ง SDK ที่ต้องการ รวมถึงกําหนดค่าคีย์ API ของ Gemini ให้พร้อมใช้งานแล้ว
อินพุตรูปภาพ
คุณส่งรูปภาพเป็นอินพุตให้ Gemini ได้ดังนี้
- อัปโหลดไฟล์รูปภาพโดยใช้ File API ก่อนส่งคำขอไปยัง
generateContent
ใช้วิธีนี้กับไฟล์ที่มีขนาดใหญ่กว่า 20 MB หรือเมื่อคุณต้องการใช้ไฟล์ซ้ำในหลายคำขอ - ส่งข้อมูลรูปภาพในบรรทัดพร้อมกับคำขอไปยัง
generateContent
ใช้วิธีนี้กับไฟล์ขนาดเล็ก (<20 MB รวมขนาดคำขอ) หรือรูปภาพที่ดึงมาจาก URL โดยตรง
อัปโหลดไฟล์ภาพ
คุณสามารถใช้ Files API เพื่ออัปโหลดไฟล์ภาพ ใช้ Files API เสมอเมื่อขนาดคำขอทั้งหมด (รวมถึงไฟล์ พรอมต์ข้อความ วิธีการของระบบ ฯลฯ) มากกว่า 20 MB หรือหากคุณต้องการใช้รูปภาพเดียวกันในพรอมต์หลายรายการ
โค้ดต่อไปนี้จะอัปโหลดไฟล์ภาพ แล้วใช้ไฟล์ในการเรียกใช้ generateContent
Python
from google import genai
client = genai.Client(api_key="GOOGLE_API_KEY")
myfile = client.files.upload(file="path/to/sample.jpg")
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[myfile, "Caption this image."])
print(response.text)
JavaScript
import {
GoogleGenAI,
createUserContent,
createPartFromUri,
} from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
async function main() {
const myfile = await ai.files.upload({
file: "path/to/sample.jpg",
config: { mimeType: "image/jpeg" },
});
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: createUserContent([
createPartFromUri(myfile.uri, myfile.mimeType),
"Caption this image.",
]),
});
console.log(response.text);
}
await main();
Go
file, err := client.UploadFileFromPath(ctx, "path/to/sample.jpg", nil)
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)
model := client.GenerativeModel("gemini-2.0-flash")
resp, err := model.GenerateContent(ctx,
genai.FileData{URI: file.URI},
genai.Text("Caption this image."))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
REST
IMAGE_PATH="path/to/sample.jpg"
MIME_TYPE=$(file -b --mime-type "${IMAGE_PATH}")
NUM_BYTES=$(wc -c < "${IMAGE_PATH}")
DISPLAY_NAME=IMAGE
tmp_header_file=upload-header.tmp
# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files?key=${GOOGLE_API_KEY}" \
-D upload-header.tmp \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" \
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null
upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"
# Upload the actual bytes.
curl "${upload_url}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${IMAGE_PATH}" 2> /dev/null > file_info.json
file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri
# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"file_data":{"mime_type": "${MIME_TYPE}", "file_uri": '$file_uri'}},
{"text": "Caption this image."}]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq ".candidates[].content.parts[].text" response.json
ดูข้อมูลเพิ่มเติมเกี่ยวกับการทำงานกับไฟล์สื่อได้ที่ Files API
ส่งข้อมูลรูปภาพในบรรทัด
คุณสามารถส่งข้อมูลรูปภาพในบรรทัดในคำขอไปยัง generateContent
แทนการอัปโหลดไฟล์รูปภาพ ซึ่งเหมาะสำหรับรูปภาพขนาดเล็ก (ขนาดคำขอรวมน้อยกว่า 20 MB) หรือรูปภาพที่ดึงมาจาก URL โดยตรง
คุณสามารถระบุข้อมูลรูปภาพเป็นสตริงที่เข้ารหัส Base64 หรือโดยการอ่านไฟล์ในเครื่องโดยตรง (ขึ้นอยู่กับ SDK)
ไฟล์รูปภาพในเครื่อง:
Python
from google.genai import types
with open('path/to/small-sample.jpg', 'rb') as f:
img_bytes = f.read()
response = client.models.generate_content(
model='gemini-2.0-flash',
contents=[
types.Part.from_bytes(
data=img_bytes,
mime_type='image/jpeg',
),
'Caption this image.'
]
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const base64ImageFile = fs.readFileSync("path/to/small-sample.jpg", {
encoding: "base64",
});
const contents = [
{
inlineData: {
mimeType: "image/jpeg",
data: base64ImageFile,
},
},
{ text: "Caption this image." },
];
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: contents,
});
console.log(response.text);
Go
model := client.GenerativeModel("gemini-2.0-flash")
bytes, err := os.ReadFile("path/to/small-sample.jpg")
if err != nil {
log.Fatal(err)
}
prompt := []genai.Part{
genai.Blob{MIMEType: "image/jpeg", Data: bytes},
genai.Text("Caption this image."),
}
resp, err := model.GenerateContent(ctx, prompt...)
if err != nil {
log.Fatal(err)
}
for _, c := range resp.Candidates {
if c.Content != nil {
fmt.Println(*c.Content)
}
}
REST
IMG_PATH=/path/to/your/image1.jpg
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{
"inline_data": {
"mime_type":"image/jpeg",
"data": "'\$(base64 \$B64FLAGS \$IMG_PATH)'"
}
},
{"text": "Caption this image."},
]
}]
}' 2> /dev/null
รูปภาพจาก URL:
Python
from google import genai
from google.genai import types
import requests
image_path = "https://goo.gle/instrument-img"
image = requests.get(image_path)
client = genai.Client(api_key="GOOGLE_API_KEY")
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=["What is this image?",
types.Part.from_bytes(data=image.content, mime_type="image/jpeg")])
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
async function main() {
const ai = new GoogleGenAI({ apiKey: process.env.GOOGLE_API_KEY });
const imageUrl = "https://goo.gle/instrument-img";
const response = await fetch(imageUrl);
const imageArrayBuffer = await response.arrayBuffer();
const base64ImageData = Buffer.from(imageArrayBuffer).toString('base64');
const result = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: [
{
inlineData: {
mimeType: 'image/jpeg',
data: base64ImageData,
},
},
{ text: "Caption this image." }
],
});
console.log(result.text);
}
main();
Go
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GOOGLE_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-2.0-flash")
// Download the image.
imageResp, err := http.Get("https://goo.gle/instrument-img")
if err != nil {
panic(err)
}
defer imageResp.Body.Close()
imageBytes, err := io.ReadAll(imageResp.Body)
if err != nil {
panic(err)
}
// Create the request.
req := []genai.Part{
genai.ImageData("jpeg", imageBytes),
genai.Text("Caption this image."),
}
// Generate content.
resp, err := model.GenerateContent(ctx, req...)
if err != nil {
panic(err)
}
// Handle the response of generated text.
for _, c := range resp.Candidates {
if c.Content != nil {
fmt.Println(*c.Content)
}
}
}
REST
IMG_URL="https://goo.gle/instrument-img"
MIME_TYPE=$(curl -sIL "$IMG_URL" | grep -i '^content-type:' | awk -F ': ' '{print $2}' | sed 's/\r$//' | head -n 1)
if [[ -z "$MIME_TYPE" || ! "$MIME_TYPE" == image/* ]]; then
MIME_TYPE="image/jpeg"
fi
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{
"inline_data": {
"mime_type":"'"$MIME_TYPE"'",
"data": "'$(curl -sL "$IMG_URL" | base64 $B64FLAGS)'"
}
},
{"text": "Caption this image."}
]
}]
}' 2> /dev/null
สิ่งที่ควรคำนึงถึงเกี่ยวกับข้อมูลรูปภาพในบรรทัดมีดังนี้
- ขนาดคำขอทั้งหมดสูงสุดคือ 20 MB ซึ่งรวมถึงข้อความแจ้ง วิธีการของระบบ และไฟล์ทั้งหมดที่ส่งในบรรทัด หากขนาดไฟล์จะทำให้ขนาดคำขอทั้งหมดเกิน 20 MB ให้ใช้ Files API เพื่ออัปโหลดไฟล์ภาพสำหรับใช้ในคำขอ
- หากใช้ตัวอย่างรูปภาพหลายครั้ง การอัปโหลดไฟล์รูปภาพโดยใช้ File API จะมีประสิทธิภาพมากกว่า
พรอมต์ที่มีรูปภาพหลายรูป
คุณสามารถระบุรูปภาพหลายรูปในพรอมต์เดียวได้โดยใส่ออบเจ็กต์รูปภาพPart
หลายรายการในอาร์เรย์ contents
ซึ่งอาจเป็นได้ทั้งข้อมูลในบรรทัด (ไฟล์ในเครื่องหรือ URL) และการอ้างอิง File API
Python
from google import genai
from google.genai import types
client = genai.Client(api_key="GOOGLE_API_KEY")
# Upload the first image
image1_path = "path/to/image1.jpg"
uploaded_file = client.files.upload(file=image1_path)
# Prepare the second image as inline data
image2_path = "path/to/image2.png"
with open(image2_path, 'rb') as f:
img2_bytes = f.read()
# Create the prompt with text and multiple images
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[
"What is different between these two images?",
uploaded_file, # Use the uploaded file reference
types.Part.from_bytes(
data=img2_bytes,
mime_type='image/png'
)
]
)
print(response.text)
JavaScript
import {
GoogleGenAI,
createUserContent,
createPartFromUri,
} from "@google/genai";
import * as fs from "node:fs";
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
async function main() {
// Upload the first image
const image1_path = "path/to/image1.jpg";
const uploadedFile = await ai.files.upload({
file: image1_path,
config: { mimeType: "image/jpeg" },
});
// Prepare the second image as inline data
const image2_path = "path/to/image2.png";
const base64Image2File = fs.readFileSync(image2_path, {
encoding: "base64",
});
// Create the prompt with text and multiple images
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: createUserContent([
"What is different between these two images?",
createPartFromUri(uploadedFile.uri, uploadedFile.mimeType),
{
inlineData: {
mimeType: "image/png",
data: base64Image2File,
},
},
]),
});
console.log(response.text);
}
await main();
Go
+ // Upload the first image
image1Path := "path/to/image1.jpg"
uploadedFile, err := client.UploadFileFromPath(ctx, image1Path, nil)
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, uploadedFile.Name)
// Prepare the second image as inline data
image2Path := "path/to/image2.png"
img2Bytes, err := os.ReadFile(image2Path)
if err != nil {
log.Fatal(err)
}
// Create the prompt with text and multiple images
model := client.GenerativeModel("gemini-2.0-flash")
prompt := []genai.Part{
genai.Text("What is different between these two images?"),
genai.FileData{URI: uploadedFile.URI},
genai.Blob{MIMEType: "image/png", Data: img2Bytes},
}
resp, err := model.GenerateContent(ctx, prompt...)
if err != nil {
log.Fatal(err)
}
printResponse(resp)
REST
# Upload the first image
IMAGE1_PATH="path/to/image1.jpg"
MIME1_TYPE=$(file -b --mime-type "${IMAGE1_PATH}")
NUM1_BYTES=$(wc -c < "${IMAGE1_PATH}")
DISPLAY_NAME1=IMAGE1
tmp_header_file1=upload-header1.tmp
curl "https://generativelanguage.googleapis.com/upload/v1beta/files?key=${GOOGLE_API_KEY}" \
-D upload-header1.tmp \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM1_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME1_TYPE}" \
-H "Content-Type: application/json" \
-d "{'file': {'display_name': '${DISPLAY_NAME1}'}}" 2> /dev/null
upload_url1=$(grep -i "x-goog-upload-url: " "${tmp_header_file1}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file1}"
curl "${upload_url1}" \
-H "Content-Length: ${NUM1_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${IMAGE1_PATH}" 2> /dev/null > file_info1.json
file1_uri=$(jq ".file.uri" file_info1.json)
echo file1_uri=$file1_uri
# Prepare the second image (inline)
IMAGE2_PATH="path/to/image2.png"
MIME2_TYPE=$(file -b --mime-type "${IMAGE2_PATH}")
if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
B64FLAGS="--input"
else
B64FLAGS="-w0"
fi
IMAGE2_BASE64=$(base64 $B64FLAGS $IMAGE2_PATH)
# Now generate content using both images
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"text": "What is different between these two images?"},
{"file_data":{"mime_type": "'"${MIME1_TYPE}"'", "file_uri": '$file1_uri'}},
{
"inline_data": {
"mime_type":"'"${MIME2_TYPE}"'",
"data": "'"$IMAGE2_BASE64"'"
}
}
]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq ".candidates[].content.parts[].text" response.json
รับกล่องขอบเขตของวัตถุ
โมเดล Gemini ได้รับการฝึกให้ระบุวัตถุในรูปภาพและระบุพิกัดของกรอบที่ล้อมรอบ ระบบจะแสดงผลพิกัดตามขนาดของรูปภาพโดยปรับขนาดเป็น [0, 1000] คุณต้องปรับขนาดพิกัดเหล่านี้ตามขนาดรูปภาพเดิม
Python
prompt = "Detect the all of the prominent items in the image. The box_2d should be [ymin, xmin, ymax, xmax] normalized to 0-1000."
JavaScript
const prompt = "Detect the all of the prominent items in the image. The box_2d should be [ymin, xmin, ymax, xmax] normalized to 0-1000.";
Go
prompt := []genai.Part{
genai.FileData{URI: sampleImage.URI},
genai.Text("Detect the all of the prominent items in the image. The box_2d should be [ymin, xmin, ymax, xmax] normalized to 0-1000."),
}
REST
PROMPT="Detect the all of the prominent items in the image. The box_2d should be [ymin, xmin, ymax, xmax] normalized to 0-1000."
คุณสามารถใช้กรอบล้อมรอบเพื่อตรวจจับวัตถุและระบุตำแหน่งภายในรูปภาพและวิดีโอ การระบุและแบ่งเขตวัตถุด้วยกล่องขอบเขตที่แม่นยำจะช่วยให้คุณปลดล็อกแอปพลิเคชันต่างๆ มากมายและเพิ่มความอัจฉริยะของโปรเจ็กต์ได้
ประโยชน์สำคัญ
- ใช้งานง่าย: ผสานรวมความสามารถในการตรวจจับวัตถุเข้ากับแอปพลิเคชันได้อย่างง่ายดาย ไม่ว่าคุณจะมีความเชี่ยวชาญด้านคอมพิวเตอร์วิทัศน์มากน้อยเพียงใด
- ปรับแต่งได้: สร้างกรอบที่ล้อมรอบตามวิธีการที่กำหนดเอง (เช่น "ฉันต้องการดูกรอบที่ล้อมรอบของวัตถุสีเขียวทั้งหมดในรูปภาพนี้") โดยไม่ต้องฝึกโมเดลที่กำหนดเอง
รายละเอียดทางเทคนิค
- อินพุต: พรอมต์และรูปภาพหรือเฟรมวิดีโอที่เกี่ยวข้อง
- เอาต์พุต: กล่องขอบเขตในรูปแบบ
[y_min, x_min, y_max, x_max]
มุมซ้ายบนคือจุดเริ่มต้น โดยแกนx
และy
จะแสดงในแนวนอนและแนวตั้งตามลำดับ ระบบจะทําให้ค่าพิกัดเป็น 0-1000 สําหรับรูปภาพทุกรูป - การแสดงภาพ: ผู้ใช้ AI Studio จะเห็นกล่องขอบเขตที่ผังไว้ใน UI
สําหรับนักพัฒนาซอฟต์แวร์ Python ให้ลองใช้โน้ตบุ๊กการทําความเข้าใจเชิงพื้นที่ 2 มิติ หรือโน้ตบุ๊กการชี้แบบ 3 มิติเวอร์ชันทดลอง
ปรับพิกัดให้อยู่ในระดับเดียวกัน
โมเดลจะแสดงผลพิกัดของกล่องขอบเขตในรูปแบบ
[y_min, x_min, y_max, x_max]
หากต้องการแปลงพิกัดที่ปรับมาตรฐานเหล่านี้เป็นพิกัดพิกเซลของรูปภาพต้นฉบับ ให้ทำตามขั้นตอนต่อไปนี้
- หารพิกัดเอาต์พุตแต่ละรายการด้วย 1,000
- คูณพิกัด x กับความกว้างของรูปภาพต้นฉบับ
- คูณพิกัด y กับความสูงของรูปภาพต้นฉบับ
หากต้องการดูตัวอย่างการสร้างพิกัดของกล่องขอบเขตและการแสดงภาพพิกัดเหล่านั้นในรูปภาพอย่างละเอียด โปรดดูตัวอย่างตำราการตรวจจับวัตถุ
การแบ่งกลุ่มรูปภาพ
ตั้งแต่โมเดล Gemini 2.5 เป็นต้นไป โมเดล Gemini ได้รับการฝึกไม่เพียงเพื่อตรวจหาสิ่งของเท่านั้น แต่ยังแบ่งกลุ่มและสร้างมาสก์ของเส้นขอบด้วย
โมเดลจะคาดการณ์รายการ JSON โดยแต่ละรายการแสดงถึงมาสก์การแบ่งกลุ่ม
แต่ละรายการมีกล่องขอบเขต ("box_2d
") ในรูปแบบ [y0, x0, y1, x1]
ที่มีพิกัดที่แปลงค่าเป็นมาตรฐานระหว่าง 0 ถึง 1000, ป้ายกำกับ ("label
") ที่ระบุวัตถุ และสุดท้ายคือมาสก์การแบ่งกลุ่มภายในกล่องขอบเขตในรูปแบบ png ที่เข้ารหัส Base64 ซึ่งเป็นแผนที่ความน่าจะเป็นที่มีค่าระหว่าง 0 ถึง 255
คุณต้องปรับขนาดมาสก์ให้ตรงกับขนาดของกล่องขอบเขต จากนั้นจึงแปลงเป็นไบนารีที่เกณฑ์ความเชื่อมั่น (127 สำหรับจุดกึ่งกลาง)
Python
prompt = """
Give the segmentation masks for the wooden and glass items.
Output a JSON list of segmentation masks where each entry contains the 2D
bounding box in the key "box_2d", the segmentation mask in key "mask", and
the text label in the key "label". Use descriptive labels.
"""
JavaScript
const prompt = `
Give the segmentation masks for the wooden and glass items.
Output a JSON list of segmentation masks where each entry contains the 2D
bounding box in the key "box_2d", the segmentation mask in key "mask", and
the text label in the key "label". Use descriptive labels.
`;
Go
prompt := []genai.Part{
genai.FileData{URI: sampleImage.URI},
genai.Text(`
Give the segmentation masks for the wooden and glass items.
Output a JSON list of segmentation masks where each entry contains the 2D
bounding box in the key "box_2d", the segmentation mask in key "mask", and
the text label in the key "label". Use descriptive labels.
`),
}
REST
PROMPT='''
Give the segmentation masks for the wooden and glass items.
Output a JSON list of segmentation masks where each entry contains the 2D
bounding box in the key "box_2d", the segmentation mask in key "mask", and
the text label in the key "label". Use descriptive labels.
'''

ดูตัวอย่างโดยละเอียดได้ที่ตัวอย่างการแบ่งกลุ่มในคู่มือตำราอาหาร
รูปแบบรูปภาพที่รองรับ
Gemini รองรับประเภท MIME ของรูปแบบรูปภาพต่อไปนี้
- PNG -
image/png
- JPEG -
image/jpeg
- WEBP -
image/webp
- HEIC -
image/heic
- HEIF -
image/heif
รายละเอียดทางเทคนิคเกี่ยวกับรูปภาพ
- ขีดจํากัดไฟล์: Gemini 2.5 Pro, 2.0 Flash, 1.5 Pro และ 1.5 Flash รองรับไฟล์รูปภาพได้สูงสุด 3,600 ไฟล์ต่อคําขอ
- การคํานวณโทเค็น
- Gemini 1.5 Flash และ Gemini 1.5 Pro: โทเค็น 258 รายการหากทั้ง 2 มิติมีขนาดไม่เกิน 384 พิกเซล ระบบจะแบ่งรูปภาพขนาดใหญ่ออกเป็นส่วนๆ (ภาพย่อยขนาดขั้นต่ำ 256 พิกเซล สูงสุด 768 พิกเซล ปรับขนาดเป็น 768x768) โดยแต่ละภาพย่อยจะมีค่าใช้จ่าย 258 โทเค็น
- Gemini 2.0 Flash: โทเค็น 258 รายการหากทั้ง 2 ขนาดมีขนาดไม่เกิน 384 พิกเซล ระบบจะแบ่งรูปภาพขนาดใหญ่ออกเป็นไทล์ขนาด 768x768 พิกเซล โดยแต่ละไทล์จะมีราคา 258 โทเค็น
- แนวทางปฏิบัติแนะนำ
- ตรวจสอบว่ารูปภาพหมุนถูกต้อง
- ใช้รูปภาพที่ชัดเจนและไม่เบลอ
- เมื่อใช้รูปภาพเดียวที่มีข้อความ ให้วางพรอมต์ข้อความหลังส่วนของรูปภาพในอาร์เรย์
contents
ขั้นตอนถัดไป
คู่มือนี้จะแสดงวิธีอัปโหลดไฟล์รูปภาพและสร้างเอาต์พุตข้อความจากอินพุตรูปภาพ ดูข้อมูลเพิ่มเติมได้ที่แหล่งข้อมูลต่อไปนี้
- คำสั่งของระบบ: คำสั่งของระบบช่วยให้คุณควบคุมลักษณะการทํางานของโมเดลตามความต้องการและกรณีการใช้งานที่เฉพาะเจาะจงได้
- การทำความเข้าใจวิดีโอ: ดูวิธีใช้อินพุตวิดีโอ
- Files API: ดูข้อมูลเพิ่มเติมเกี่ยวกับการอัปโหลดและจัดการไฟล์เพื่อใช้กับ Gemini
- กลยุทธ์การแจ้งไฟล์: Gemini API รองรับการแจ้งด้วยข้อมูลข้อความ รูปภาพ เสียง และวิดีโอ หรือที่เรียกว่าการแจ้งแบบหลายสื่อ
- คำแนะนำด้านความปลอดภัย: บางครั้งโมเดล Generative AI อาจให้ผลลัพธ์ที่ไม่คาดคิด เช่น ผลลัพธ์ที่ไม่ถูกต้อง มีอคติ หรือไม่เหมาะสม ขั้นตอนหลังการประมวลผลและการประเมินจากเจ้าหน้าที่เป็นสิ่งจําเป็นในการจำกัดความเสี่ยงของอันตรายจากเอาต์พุตดังกล่าว