728x90
반응형
SMALL
1. 아래와 같이 코딩합니다.
##############################
## 이미지 기반 질의 응답 수행 ##
##############################
import google.generativeai as genai
from PIL import Image
from config import *
# Step 1: Gemini API 키 설정
genai.configure(api_key=API_KEY)
# Step 2: 이미지와 질문 입력
def image_based_qna(image_path, question):
"""
Gemini 2.0 Flash API를 통해 이미지 기반 질의응답 수행
:param image_path: 분석할 이미지 파일 경로
:param question: 이미지와 관련된 질문
:return: Gemini의 답변
"""
try:
# 이미지 로드
image = Image.open(image_path)
# Gemini 모델 로드
model = genai.GenerativeModel("gemini-1.5-flash")
# 이미지와 질문을 함께 전달
response = model.generate_content([question, image])
# 응답 반환
return response.text
except Exception as e:
return f"오류 발생: {e}"
# Step 3: 실행 예시
if __name__ == "__main__":
# 이미지 파일 경로와 질문 입력
image_path = "cat2.png" # 분석할 이미지 파일 경로
question = "이 사진에 나오는 고양이의 품종은 무엇인가요?"
# 질의응답 실행
answer = image_based_qna(image_path, question)
print("Gemini의 답변:", answer)
2.아래와 같이 응답결과를 얻을 수 있습니다.
3.참고로 cat2.png 이미지는 다음과 같습니다.
4. 아래와 같이 코딩합니다.(이미지 설명해줘)
import google.generativeai as genai
import PIL.Image
from config import *
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-1.5-flash")
organ = PIL.Image.open("cat.jpg")
response = model.generate_content(["첨부안 이미지에 대해 설명해줘", organ])
print(response.text)
5.아래와 같이 응답결과를 얻을 수 있습니다.
6.아래와 같이 코딩하면 url 기반으로 인터넷의 이미지에 대해 분석을 요청할 수 있습니다.
import httpx
import os
import base64
import google.generativeai as genai
from config import *
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel(model_name = "gemini-1.5-pro")
image_path = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Palace_of_Westminster_from_the_dome_on_Methodist_Central_Hall.jpg/2560px-Palace_of_Westminster_from_the_dome_on_Methodist_Central_Hall.jpg"
image = httpx.get(image_path)
prompt = "이 이미지의 제목 달아줘"
response = model.generate_content([{'mime_type':'image/jpeg', 'data': base64.b64encode(image.content).decode('utf-8')}, prompt])
print(response.text)
7. 실행결과는 아래와 같습니다.
8.아래와 같이 코딩하면 url 기반으로 여러개의 인터넷의 이미지에 대해 분석을 요청할 수 있습니다.
import httpx
import os
import base64
import google.generativeai as genai
from config import *
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel(model_name = "gemini-1.5-pro")
image_path_1 = "https://blog.malcang.com/wp-content/uploads/2024/03/1-1.png" # Replace with the actual path to your first image
image_path_2 = "http://image.dongascience.com/Photo/2020/03/5bddba7b6574b95d37b6079c199d7101.jpg" # Replace with the actual path to your second image
image_1 = httpx.get(image_path_1)
image_2 = httpx.get(image_path_2)
prompt = "두 이미지에 포함된 내용을 비교해서 설명해줘"
response = model.generate_content([
{'mime_type':'image/jpeg', 'data': base64.b64encode(image_1.content).decode('utf-8')},
{'mime_type':'image/jpeg', 'data': base64.b64encode(image_2.content).decode('utf-8')}, prompt])
print(response.text)
9. 실행결과는 아래와 같습니다.
728x90
반응형
LIST
'퀵포스팅 > 제미나이2.0 ai api로 할 수 있는 것들' 카테고리의 다른 글
제미나이 2.0 ai api로 할 수 있는 것들 - 5.실시간 번역 (0) | 2024.12.22 |
---|---|
제미나이 2.0 ai api로 할 수 있는 것들 - 4.여행계획 짜기 (3) | 2024.12.22 |
제미나이 2.0 ai api로 할 수 있는 것들 - 3.동영상에 대한 설명 얻기 (1) | 2024.12.22 |
제미나이 2.0 ai api로 할 수 있는 것들 - 1.기본 API 사용법 (0) | 2024.12.22 |
제미나이 2.0 ai api로 할 수 있는 것들 - 0.소개, 특징 (0) | 2024.12.22 |