Skip to content

Commit

Permalink
[BE#200]이미지 유해성 검사 기능 추가
Browse files Browse the repository at this point in the history
- clova greeneye api를 활용한 이미지 유해성 검사
  • Loading branch information
victolee0 authored Nov 27, 2023
1 parent df8f7f8 commit 1975419
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
31 changes: 16 additions & 15 deletions BE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"typeorm": "^0.3.17",
"uuid": "^9.0.1",
"winston": "^3.11.0"
},
"devDependencies": {
Expand Down
19 changes: 19 additions & 0 deletions BE/src/users/interface/greeneye.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface GreenEyeResponse {
version: string;
requestId: string;
timestamp: number;
images: [
{
message: string;
name: string;
result: {
adult: { confidence: number };
normal: { confidence: number };
porn: { confidence: number };
sexy: { confidence: number };
};
latency: number;
confidence: number;
},
];
}
48 changes: 48 additions & 0 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { Repository } from 'typeorm';
import { UsersModel } from './entity/users.entity';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { ConfigService } from '@nestjs/config';
import { v4 } from 'uuid';
import { GreenEyeResponse } from './interface/greeneye.interface';

@Injectable()
export class UsersService {
constructor(
@InjectRepository(UsersModel)
private usersRepository: Repository<UsersModel>,
private config: ConfigService,
) {}

async createUser(user: CreateUserDto): Promise<UsersModel> {
Expand Down Expand Up @@ -62,4 +66,48 @@ export class UsersService {

return selectedUser;
}

async isNormalImage(image_url: string): Promise<boolean> {
const THRESHOLD = 0.5;
const response = await this.requestClovaGreenEye(image_url);
const result = response.images[0].result;
const message = response.images[0].message;
if (message !== 'SUCCESS') {
throw new BadRequestException('이미지 인식 실패');
}
const normalScore = result.normal.confidence;
const isNormal = normalScore > THRESHOLD ? true : false;

return isNormal;
}

async requestClovaGreenEye(image_url: string): Promise<GreenEyeResponse> {
const APIURL = this.config.get<string>('GREENEYE_URL');
const CLIENT_SECRET = this.config.get<string>('GREENEYE_SECRET');
const UUID = v4();
try {
const response = await fetch(APIURL, {
method: 'POST',
headers: {
'X-GREEN-EYE-SECRET': CLIENT_SECRET,
'Content-Type': 'application/json',
},
body: JSON.stringify({
version: 'V1',
requestId: UUID,
timestamp: Date.now(),
images: [
{
name: `${UUID}_profile`,
url: image_url,
},
],
}),
});

return response.json();
} catch (error) {
throw new BadRequestException('이미지 검사 요청 실패');
}
}
}

0 comments on commit 1975419

Please sign in to comment.