-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: 이미지 검열 API 구현
- Loading branch information
Showing
7 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from google.cloud import vision | ||
from logs.logger_config import get_logger | ||
from errors.server_exception import ExternalAPIError | ||
|
||
logger = get_logger() | ||
|
||
# 이미지 검열을 위한 GCP API 이용 | ||
def detect_safe_search(member_id, image_base64): | ||
|
||
try: | ||
# 클라이언트 생성 | ||
client = vision.ImageAnnotatorClient() | ||
image = vision.Image(content=image_base64) | ||
|
||
response = client.safe_search_detection(image=image) | ||
safe = response.safe_search_annotation | ||
|
||
# 검증 결과 확인 | ||
categories = { | ||
"adult": safe.adult, | ||
"medical": safe.medical, | ||
"spoofed": safe.spoof, | ||
"violence": safe.violence, | ||
"racy": safe.racy, | ||
} | ||
|
||
# "LIKELY" 또는 "VERY_LIKELY" 판정 | ||
for category, likelihood in categories.items(): | ||
# LIKELY 이상의 경우 | ||
if likelihood >= 4: | ||
logger.info(f"유해한 이미지({category})를 업로드한 유저: {member_id}") | ||
return False | ||
|
||
return True | ||
except Exception as e: | ||
logger.error(f"GCP API 연결 오류 발생: {e}") | ||
raise ExternalAPIError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from fastapi import APIRouter, Depends, File, UploadFile | ||
from auth.decoded_token import get_current_member | ||
from errors.business_exception import InvalidFileFormat | ||
from logs.logger_config import get_logger | ||
from swagger.response_config import cencoring_image_responses | ||
from apis.food_image import process_image_to_base64 | ||
from apis.image_censor import detect_safe_search | ||
|
||
# 공용 로거 | ||
logger = get_logger() | ||
|
||
|
||
router = APIRouter( | ||
prefix="/ai/v1", | ||
tags=["이미지 검열"] | ||
) | ||
|
||
# 이미지 검열 API | ||
@router.post("/censor", responses=cencoring_image_responses) | ||
async def analyze_food_image(file: UploadFile = File(...), member_id: int = Depends(get_current_member)): | ||
|
||
# 지원하는 파일 형식 | ||
ALLOWED_FILE_TYPES = ["image/jpeg", "image/png"] | ||
|
||
# 파일 형식 검증 | ||
if file.content_type not in ALLOWED_FILE_TYPES: | ||
raise InvalidFileFormat(allowed_types=ALLOWED_FILE_TYPES) | ||
|
||
# 이미지 처리 및 Base64 인코딩 진행 | ||
image_base64 = await process_image_to_base64(file) | ||
|
||
# 이미지 검열 진행 | ||
censor = detect_safe_search(member_id, image_base64) | ||
|
||
# 응답값 구성 | ||
response = { | ||
"success": True, | ||
"response": censor, | ||
"error": None | ||
} | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters