추후 ViT모델로 교체될 예정입니다.
위 모델이 성능 면에서 훨신 압도적 입니다.
따라서, 더이상 CNN모델은 사용하지 않습니다.
허깅 페이스에서 받아 사용할 수 있도록 config 파일을 만들었으며, 잘 작동하는것을 확인했습니다.
로맨스 스캠에서 카메라에 주로 비춰진 사진을 조사해 집어넣어 유사도 판단에 사용할 수 있도록 구성했습니다.
모델 학습에 활용된 데이터는 전부 직접 공수한 것이며, 로맨스스캠, 몸캠 피싱 피해를 입은 지인을 통해 얻은 데이터와, 그 특징을 통해 추가로 만든 데이터를 통해 만들었습니다.
11월 초에 업로드 할 새로운 버젼은 데이터를 얻기 위해 직접 몸캠 피싱을 당해 새로운 데이터를 얻고, 이를 모델학습하여 올릴것 입니다.
1import tensorflow as tf
2import numpy as np
3from PIL import Image
4import requests
5
6# CNN 모델 다운로드 및 로드
7model_url = "https://huggingface.co/gihakkk/CNN_modle/resolve/main/cnn_similarity_model.keras"
8model_path = "cnn_similarity_model.keras"
9
10# 모델 파일 다운로드
11response = requests.get(model_url)
12with open(model_path, "wb") as f:
13 f.write(response.content)
14
15# Keras 모델 로드
16cnn_model = tf.keras.models.load_model(model_path)
17
18# 이미지 전처리 함수
19def preprocess_image(image_path):
20 try:
21 img = Image.open(image_path).convert('RGB')
22 img = img.resize((152, 152)) # 모델이 요구하는 크기
23 img_array = np.array(img) / 255.0 # 이미지를 0-1 사이로 정규화
24 img_array = np.expand_dims(img_array, axis=0) # 배치 차원 추가
25 return img_array
26 except Exception as e:
27 print(f"Error processing image: {e}")
28 return None
29
30# 유사도 예측 함수
31def predict_similarity(image_path):
32 img_array = preprocess_image(image_path)
33 if img_array is not None:
34 predictions = cnn_model.predict(img_array) # 모델을 통해 예측
35 similarity_score = np.mean(predictions) # 유사도 점수의 평균 계산
36 if similarity_score > 0.5: # 임계값을 기준으로 유사도 판단
37 return "로맨스 스캠 이미지입니다."
38 else:
39 return "로맨스 스캠 이미지가 아닙니다."
40 else:
41 return "이미지 전처리에 실패했습니다."
42
43# 테스트 이미지 예측
44image_path = r'사진 위치 입력' # 테스트할 이미지 경로
45result = predict_similarity(image_path)
46print(result)
47
48