-
Notifications
You must be signed in to change notification settings - Fork 6
[SWEP-56] 이미지 자동 라벨링 API 구현 #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import {UserModel} from '../src/models/user.model.ts'; | ||
| import 'express'; | ||
|
|
||
| declare global { | ||
|
|
||
This file contains hidden or 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 hidden or 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,131 @@ | ||
| import {Request, Response} from 'express'; | ||
| import {detectLabels} from '../services/tags-ai.service.js'; | ||
| import {StatusCodes} from 'http-status-codes'; | ||
| import { | ||
| DataValidationError, | ||
| LabelDetectionError, | ||
| LabelNotFoundError, | ||
| } from '../errors.js'; | ||
|
|
||
| export const labelDetectionController = async ( | ||
| req: Request, | ||
| res: Response, | ||
| ): Promise<void> => { | ||
| /* | ||
| #swagger.tags = ['label-detection'] | ||
| #swagger.summary = '이미지 라벨링' | ||
| #swagger.description = 'Base64 데이터를 JSON으로 받아 이미지를 분석하여 상위 3개의 라벨과 정확도를 반환합니다.' | ||
| #swagger.requestBody = { | ||
| required: true, | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| properties: { | ||
| base64_image: { | ||
| type: "string", | ||
| description: "Base64 인코딩된 이미지 데이터", | ||
| example: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD..." | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #swagger.responses[200] = { | ||
| description: "라벨링 결과 반환", | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| properties: { | ||
| topLabels: { | ||
| type: "array", | ||
| items: { | ||
| type: "object", | ||
| properties: { | ||
| description: { type: "string", example: "Mountain" }, | ||
| score: { type: "number", example: 0.95 } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #swagger.responses[400] = { | ||
| description: "잘못된 요청 데이터", | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| properties: { | ||
| error: { type: "string", example: "Base64 이미지 데이터가 제공되지 않았습니다." } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #swagger.responses[500] = { | ||
| description: "서버 내부 오류", | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| properties: { | ||
| error: { type: "string", example: "라벨링 중 오류가 발생했습니다." } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| */ | ||
|
|
||
| try { | ||
| // Base64 이미지 데이터가 요청에 포함되었는지 확인 | ||
| const {base64_image} = req.body; | ||
|
|
||
| if (!base64_image) { | ||
| throw new DataValidationError({ | ||
| reason: 'Base64 이미지 데이터가 제공되지 않았습니다.', | ||
| }); | ||
| } | ||
|
|
||
| // Base64 데이터에서 MIME 타입 제거 | ||
| const base64Data = base64_image.replace(/^data:image\/\w+;base64,/, ''); | ||
|
|
||
| // 서비스 호출 | ||
| const labels = await detectLabels(base64Data); | ||
|
|
||
| // 라벨 반환 | ||
| res.status(StatusCodes.OK).json({topLabels: labels}); | ||
| } catch (error) { | ||
| console.error('Error in labelDetectionController:', error); | ||
|
|
||
| // 커스텀 에러 처리 | ||
| if ( | ||
| error instanceof DataValidationError || | ||
| error instanceof LabelNotFoundError | ||
| ) { | ||
| res.status(error.statusCode).json({ | ||
| errorCode: error.code, | ||
| reason: error.message, | ||
| details: error.details, | ||
| }); | ||
| } else if (error instanceof LabelDetectionError) { | ||
| res.status(error.statusCode).json({ | ||
| errorCode: error.code, | ||
| reason: error.message, | ||
| details: error.details, | ||
| }); | ||
| } else { | ||
| // 기타 예상치 못한 에러 처리 | ||
| res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ | ||
| errorCode: 'unknown', | ||
| reason: '예상치 못한 서버 오류가 발생했습니다.', | ||
| details: null, | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| import {ImageAnnotatorClient} from '@google-cloud/vision'; | ||
| import {LabelDetectionError, LabelNotFoundError} from '../errors.js'; | ||
|
|
||
| import path from 'path'; | ||
|
|
||
| const keyFilename = path.resolve('../sweepicai-00d515e813ea.json'); | ||
|
|
||
| const visionClient = new ImageAnnotatorClient({keyFilename}); | ||
| export const detectLabels = async ( | ||
| base64Image: string, | ||
| ): Promise<{description: string; score: number}[]> => { | ||
| try { | ||
| // Vision API 호출 | ||
| const [result] = await visionClient.labelDetection({ | ||
| image: {content: base64Image}, | ||
| }); | ||
|
|
||
| const labels = result.labelAnnotations; | ||
|
|
||
| // 라벨이 없으면 LabelNotFoundError 발생 | ||
| if (!labels || labels.length === 0) { | ||
| throw new LabelNotFoundError({ | ||
| reason: '이미지에서 라벨을 감지하지 못했습니다.', | ||
| }); | ||
| } | ||
|
|
||
| // 상위 3개의 라벨 반환 | ||
| return labels | ||
| .sort((a, b) => (b.score || 0) - (a.score || 0)) // 정확도 내림차순 정렬 | ||
| .slice(0, 3) // 상위 3개만 선택 | ||
| .map(label => ({ | ||
| description: label.description || 'Unknown', | ||
| score: label.score || 0, | ||
| })); | ||
| } catch (error) { | ||
| console.error('Error in detectLabels service:', error); | ||
| throw new LabelDetectionError({ | ||
| reason: '라벨링 처리 중 오류가 발생했습니다.', | ||
| }); | ||
| } | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분에서 에러들이 baseError의 인스턴스니까 err innstanxeof BaseError 로 그냥 통합 하시고 아래 else if 문을 없애시면 좀더 깔끔해 보일거같습니다