การตอบคำถามด้วยภาพ
ถามคำถามเฉพาะเกี่ยวกับเนื้อหา ปริมาณ และคุณลักษณะของภาพ
การตอบคำถามด้วยภาพ เป็นบทเรียน AI Prompt Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Prompt Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน
การตอบคำถามด้วยภาพ
การตอบคำถามด้วยภาพ (VQA) คือภารกิจการตอบคำถามภาษาธรรมชาติเกี่ยวกับรูปภาพ ต่างจากคำอธิบายรูปภาพซึ่งอธิบายทุกอย่าง การตอบคำถามด้วยภาพจะมุ่งให้โมเดลตอบคำถามเฉพาะเจาะจง
พรอมป์ต์ VQA มีความแม่นยำและตรงประเด็น และมักต้องอาศัยการนับ การระบุ การเปรียบเทียบ หรือการให้เหตุผลเกี่ยวกับเนื้อหาทางภาพ คุณภาพของพรอมป์ต์เป็นตัวกำหนดว่าจะได้คำตอบที่แม่นยำและมีประโยชน์ หรือคำตอบทั่วไปที่กำกวม
โครงสร้างพื้นฐานของพรอมป์ต์ VQA
พรอมป์ต์ VQA จะจับคู่รูปภาพกับคำถามเฉพาะเจาะจง สิ่งสำคัญคือการทำให้คำถามแม่นยำเพียงพอที่จะสร้างคำตอบที่ตรงประเด็นและนำไปใช้ได้:
import anthropic, base64
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def ask_about_image(image_path, question, answer_format='Direct answer. No extra explanation.'):
with open(image_path, 'rb') as f:
img_b64 = base64.standard_b64encode(f.read()).decode('utf-8')
prompt = f'{question}\n\n{answer_format}'
r = client.messages.create(
model='claude-opus-4-5', max_tokens=150,
messages=[{'role': 'user', 'content': [
{'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': img_b64}},
{'type': 'text', 'text': prompt}
]}]
)
return r.content[0].text
# Example VQA calls (replace image.jpg with actual image)
print('VQA function defined. Ready for image questions.')คำถามเกี่ยวกับการนับ
การนับเป็นงาน VQA ที่พบบ่อย พรอมป์ต์การนับที่แม่นยำจะให้ผลลัพธ์ถูกต้องกว่าพรอมป์ต์ที่กำกวม:
# Vague (bad):
vague_prompt = 'How many people are there?'
# Precise (good): specifies what counts and what does not
counting_prompt = '''
How many people are visible in this image?
Count only: people whose faces OR bodies are at least 50% visible.
Do NOT count: people who are heavily cropped, cut off at the edge, or only partially visible.
Return a single number.
'''
# Even more precise: handles partial visibility explicitly
precise_count = '''
Count the number of distinct individuals visible in this image.
If a person is partially obscured, count them if more than half their body is visible.
Return JSON: {"count": integer, "partially_visible": integer, "notes": "string or null"}
'''
print('Counting prompts: vague vs precise.')
print('Precise prompts define edge cases explicitly.')การระบุแบรนด์และโลโก้
การระบุโลโก้แบรนด์ในรูปภาพเป็นงานวิเคราะห์ผลิตภัณฑ์ที่พบบ่อย พรอมป์ต์ต้องระบุสิ่งที่ควรมองหาและ format ที่ต้องการให้ส่งคืน:
logo_prompt = '''
Identify all visible brand logos, company names, and product labels in this image.
For each, note:
- Brand/company name
- Where it appears in the image (top-left, center, on a product, etc.)
- Confidence: high (clearly legible) | medium (partially visible) | low (partially obscured)
Return JSON: {"brands": [{"name": str, "location": str, "confidence": str}]}
If no logos are visible, return: {"brands": []}
'''
import anthropic, base64, json
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def identify_brands(image_path):
with open(image_path, 'rb') as f:
img_b64 = base64.standard_b64encode(f.read()).decode('utf-8')
r = client.messages.create(
model='claude-opus-4-5', max_tokens=200,
messages=[{'role': 'user', 'content': [
{'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': img_b64}},
{'type': 'text', 'text': logo_prompt}
]}]
)
return json.loads(r.content[0].text)
print('Brand identification function defined.')การจดจำอารมณ์และการแสดงออก
การระบุการแสดงอารมณ์ในรูปภาพจำเป็นต้องออกแบบพรอมป์ต์อย่างรอบคอบ โดยยอมรับว่าผลลัพธ์อาจไม่แน่นอน:
emotion_prompt = '''
Describe the emotional expression of the person in this image.
Assess:
- Primary emotion: (happy, sad, angry, surprised, fearful, disgusted, neutral, or other)
- Intensity: (low, moderate, high)
- Confidence: (high if expression is clear, medium if subtle, low if face is obscured or turned away)
- Evidence: which specific facial features support your assessment
Return JSON:
{
"primary_emotion": str,
"intensity": str,
"confidence": str,
"evidence": str,
"secondary_emotion": str or null
}
If no person or face is clearly visible, return: {"primary_emotion": null, "confidence": "none", "reason": str}
'''
print(emotion_prompt)คำถามเกี่ยวกับความสัมพันธ์เชิงพื้นที่
คำถามเกี่ยวกับตำแหน่งของวัตถุเมื่อเทียบกับวัตถุอื่นจำเป็นต้องใช้คำศัพท์เชิงพื้นที่อย่างชัดเจนในพรอมป์ต์:
spatial_prompt = '''
Answer questions about the spatial relationships of objects in this image.
Use these spatial terms consistently:
- Position in frame: top-left, top-center, top-right, middle-left, center, middle-right, bottom-left, bottom-center, bottom-right
- Relative position: in front of, behind, to the left of, to the right of, above, below, overlapping
- Distance: in the foreground, in the midground, in the background
Question: {question}
Answer in one or two sentences using the spatial vocabulary above.
'''
# Example questions:
questions = [
'Where is the red cup relative to the laptop?',
'Is the plant in the foreground or background?',
'What object is to the left of the person?'
]
for q in questions:
print(spatial_prompt.replace('{question}', q)[:200])
print('---')การประเมินคุณภาพและสภาพ
การประเมินคุณภาพหรือสภาพของวัตถุในรูปภาพมีประโยชน์สำหรับการตรวจสอบผลิตภัณฑ์ การประเมินอสังหาริมทรัพย์ และการควบคุมคุณภาพ:
condition_prompt = '''
Assess the condition of the main subject in this image.
Rate on these dimensions (1-5 scale, 5=excellent):
- Physical condition: (1=heavily damaged, 5=like new)
- Cleanliness: (1=very dirty, 5=spotless)
- Completeness: (1=major parts missing, 5=fully intact)
For each rating, provide one-sentence evidence.
Return JSON:
{
"physical_condition": {"score": int, "evidence": str},
"cleanliness": {"score": int, "evidence": str},
"completeness": {"score": int, "evidence": str},
"overall_grade": "excellent|good|fair|poor",
"recommendation": str
}
'''
print('Condition assessment prompt defined.')
print('Useful for: product inspection, real estate, equipment maintenance.')คำถาม VQA แบบใช่/ไม่ใช่
คำถามแบบสองทางที่ตอบว่าใช่หรือไม่ใช่จำเป็นต้องใช้พรอมป์ต์ที่ป้องกันไม่ให้โมเดลตอบเป็นร้อยแก้วแบบสงวนท่าที ในกรณีที่ต้องการค่าบูลีนอย่างง่าย:
def yes_no_question(image_path, question):
with open(image_path, 'rb') as f:
img_b64 = base64.standard_b64encode(f.read()).decode('utf-8')
prompt = f'''
Answer this yes/no question about the image.
Return JSON: {{"answer": "yes|no", "confidence": "high|medium|low", "reason": str}}
Do NOT answer with maybe, possibly, or a hedged statement.
If you genuinely cannot determine the answer, return {{"answer": "unclear", "confidence": "low", "reason": str}}
Question: {question}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=100,
messages=[{'role': 'user', 'content': [
{'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': img_b64}},
{'type': 'text', 'text': prompt}
]}]
)
return json.loads(r.content[0].text)
# Example: 'Is there a safety helmet visible in the image?'
print('Yes/no VQA function defined.')การเชื่อมคำถาม VQA
สามารถเชื่อมคำถาม VQA หลายข้อเกี่ยวกับรูปภาพเดียวกันไว้ในพรอมป์ต์เดียว เพื่อลดจำนวนการเรียก API:
multi_question_prompt = '''
Answer all of the following questions about this image.
Return a JSON object where each key is the question ID.
Questions:
1. How many people are visible?
2. What is the approximate age range of the youngest person?
3. Is there any food visible in the image?
4. What is the dominant color in the image?
5. Is the setting indoors or outdoors?
Return JSON:
{
"q1": {"answer": str},
"q2": {"answer": str},
"q3": {"answer": "yes|no", "details": str or null},
"q4": {"answer": str},
"q5": {"answer": "indoors|outdoors|unclear"}
}
'''
print('Multi-question VQA prompt — answers 5 questions in one API call.')การจัดการความไม่แน่นอนของ VQA
บางครั้งคำถาม VQA ไม่สามารถตอบได้อย่างแน่นอน รูปภาพอาจเบลอ องค์ประกอบที่เกี่ยวข้องอาจถูกบดบังบางส่วน หรือคำตอบอาจกำกวมอย่างแท้จริง ควรขอให้แสดงความไม่แน่นอนอย่างชัดเจน แทนการบังคับให้เดา:
uncertainty_vqa_prompt = '''
Answer this question about the image as precisely as possible.
If the answer is not clearly visible or is ambiguous, say so explicitly.
Question: {question}
Return JSON:
{
"answer": str,
"confidence": "high|medium|low|cannot_determine",
"limitation": str or null
}
For confidence levels:
- high: Answer is clearly visible and unambiguous
- medium: Visible but some uncertainty
- low: Partially visible or requires inference
- cannot_determine: Not enough visual information
Question: What brand is printed on the water bottle?
'''
print(uncertainty_vqa_prompt)พรอมป์ต์ VQA เฉพาะโดเมน
แต่ละโดเมนต้องใช้คำศัพท์ VQA และมาตรฐานการวัดที่แตกต่างกัน พรอมป์ต์เฉพาะโดเมนจะให้คำตอบที่แม่นยำและนำไปใช้ได้มากกว่า:
# Manufacturing quality control VQA
qc_prompt = '''
Inspect this product image for quality defects.
Answer each question:
1. Are there any visible scratches or surface damage? (yes/no + location)
2. Is the product alignment within expected tolerance? (yes/no)
3. Are all required labels/markings present? (yes/no + list missing ones)
4. Overall QC result: PASS or FAIL?
Return JSON:
{"scratches": {"present": bool, "location": str or null},
"alignment_ok": bool,
"labels_complete": bool, "missing_labels": [str],
"qc_result": "PASS|FAIL",
"fail_reasons": [str]}
'''
# Food safety VQA
food_prompt = '''
Inspect this food preparation image.
1. Are gloves being worn? 2. Is hair covered? 3. Any visible contamination risk?
Return JSON: {"gloves": bool, "hair_covered": bool, "contamination_risk": bool, "details": str}
'''
print("Domain-specific QC and food safety VQA prompts defined.")ตรวจสอบอย่างรวดเร็ว
พรอมป์ต์ VQA ใดมีแนวโน้มมากที่สุดที่จะให้คำตอบที่แม่นยำและนำไปใช้ได้ เมื่อใช้สำหรับนับวัตถุในรูปภาพ
พรอมต์ VQA — ประเด็นสำคัญ
การตอบคำถามจากภาพที่มีประสิทธิภาพจำเป็นต้องออกแบบพรอมต์อย่างแม่นยำ:
- ตั้งคำถามให้เฉพาะเจาะจงและตรงประเด็น — หลีกเลี่ยงคำกำกวม เช่น บางส่วน หรือ หลากหลาย
- กำหนดกรณีขอบเขตอย่างชัดเจนสำหรับคำถามเกี่ยวกับการนับ (สิ่งใดจึงจะนับว่าแสดงให้เห็นเพียงบางส่วน)
- ระบุรูปแบบผลลัพธ์ที่แน่นอน — เจสัน ตัวเลขเดียว หรือใช่/ไม่ใช่ — เพื่อป้องกันคำตอบแบบร่ายยาวที่ไม่ยืนยันแน่ชัด
- ระบุระดับความมั่นใจสำหรับคำตอบทั้งหมด เพื่อให้สามารถทำเครื่องหมายผลลัพธ์ที่ไม่แน่นอนได้
- รวมคำถามหลายข้อเกี่ยวกับภาพเดียวกันไว้ในการเรียกใช้ครั้งเดียว เพื่อลดค่าใช้จ่ายของส่วนเชื่อมต่อโปรแกรม
- สำหรับคำถามแบบใช่/ไม่ใช่ ให้ป้องกันคำตอบที่ไม่ยืนยันแน่ชัดอย่างชัดเจน และเปิดทางเลือกสำหรับกรณี ไม่ชัดเจน
- VQA เฉพาะด้าน (การแพทย์ กฎหมาย ผลิตภัณฑ์) จำเป็นต้องมีคำศัพท์เฉพาะด้านอยู่ในพรอมต์
คำถามที่พบบ่อย
บทเรียน “การตอบคำถามด้วยภาพ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตอบคำถามด้วยภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Prompt Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตอบคำถามด้วยภาพ”
ถามคำถามเฉพาะเกี่ยวกับเนื้อหา ปริมาณ และคุณลักษณะของภาพ คุณปฏิบัติ AI Prompt Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Prompt Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Prompt Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การตอบคำถามด้วยภาพ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Prompt Engineering นี้ได้ไหม
ได้ บทเรียน AI Prompt Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พรอมป์ตสำหรับการบรรยายและใส่คำบรรยายภาพ
- การตอบคำถามด้วยภาพ
- พรอมป์ตสำหรับเปรียบเทียบหลายภาพ
- พรอมป์ตสำหรับ OCR และการวิเคราะห์เอกสาร