File size: 2,520 Bytes
fba948b
 
 
9812e57
0c40141
 
 
 
 
fba948b
 
d814921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fba948b
d814921
914790b
 
 
d814921
 
 
914790b
d814921
914790b
 
d814921
 
914790b
 
 
 
 
 
d814921
914790b
d814921
 
914790b
 
 
 
d814921
fba948b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
---
license: unknown
---
์ถ”ํ›„ ViT๋ชจ๋ธ๋กœ ๊ต์ฒด๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.  
ํ—ˆ๊น… ํŽ˜์ด์Šค์—์„œ ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก config ํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ์œผ๋ฉฐ, ์ž˜ ์ž‘๋™ํ•˜๋Š”๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.  
๋กœ๋งจ์Šค ์Šค์บ ์—์„œ ์นด๋ฉ”๋ผ์— ์ฃผ๋กœ ๋น„์ถฐ์ง„ ์‚ฌ์ง„์„ ์กฐ์‚ฌํ•ด ์ง‘์–ด๋„ฃ์–ด ์œ ์‚ฌ๋„ ํŒ๋‹จ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.  
๋ชจ๋ธ ํ•™์Šต์— ํ™œ์šฉ๋œ ๋ฐ์ดํ„ฐ๋Š” ์ „๋ถ€ ์ง์ ‘ ๊ณต์ˆ˜ํ•œ ๊ฒƒ์ด๋ฉฐ, ๋กœ๋งจ์Šค์Šค์บ , ๋ชธ์บ  ํ”ผ์‹ฑ ํ”ผํ•ด๋ฅผ ์ž…์€ ์ง€์ธ์„ ํ†ตํ•ด ์–ป์€ ๋ฐ์ดํ„ฐ์™€, ๊ทธ ํŠน์ง•์„ ํ†ตํ•ด ์ถ”๊ฐ€๋กœ ๋งŒ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.  
11์›” ์ดˆ์— ์—…๋กœ๋“œ ํ•  ์ƒˆ๋กœ์šด ๋ฒ„์ ผ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ์ง์ ‘ ๋ชธ์บ  ํ”ผ์‹ฑ์„ ๋‹นํ•ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ณ , ์ด๋ฅผ ๋ชจ๋ธํ•™์Šตํ•˜์—ฌ ์˜ฌ๋ฆด๊ฒƒ ์ž…๋‹ˆ๋‹ค.  

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
```python
import tensorflow as tf
import numpy as np
from PIL import Image
import requests

# CNN ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ ๋ฐ ๋กœ๋“œ
model_url = "https://huggingface.co/gihakkk/CNN_modle/resolve/main/cnn_similarity_model.keras"
model_path = "cnn_similarity_model.keras"

# ๋ชจ๋ธ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
response = requests.get(model_url)
with open(model_path, "wb") as f:
    f.write(response.content)

# Keras ๋ชจ๋ธ ๋กœ๋“œ
cnn_model = tf.keras.models.load_model(model_path)

# ์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜
def preprocess_image(image_path):
    try:
        img = Image.open(image_path).convert('RGB')
        img = img.resize((152, 152))  # ๋ชจ๋ธ์ด ์š”๊ตฌํ•˜๋Š” ํฌ๊ธฐ
        img_array = np.array(img) / 255.0  # ์ด๋ฏธ์ง€๋ฅผ 0-1 ์‚ฌ์ด๋กœ ์ •๊ทœํ™”
        img_array = np.expand_dims(img_array, axis=0)  # ๋ฐฐ์น˜ ์ฐจ์› ์ถ”๊ฐ€
        return img_array
    except Exception as e:
        print(f"Error processing image: {e}")
        return None

# ์œ ์‚ฌ๋„ ์˜ˆ์ธก ํ•จ์ˆ˜
def predict_similarity(image_path):
    img_array = preprocess_image(image_path)
    if img_array is not None:
        predictions = cnn_model.predict(img_array)  # ๋ชจ๋ธ์„ ํ†ตํ•ด ์˜ˆ์ธก
        similarity_score = np.mean(predictions)  # ์œ ์‚ฌ๋„ ์ ์ˆ˜์˜ ํ‰๊ท  ๊ณ„์‚ฐ
        if similarity_score > 0.5:  # ์ž„๊ณ„๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์œ ์‚ฌ๋„ ํŒ๋‹จ
            return "๋กœ๋งจ์Šค ์Šค์บ  ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค."
        else:
            return "๋กœ๋งจ์Šค ์Šค์บ  ์ด๋ฏธ์ง€๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."
    else:
        return "์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."

# ํ…Œ์ŠคํŠธ ์ด๋ฏธ์ง€ ์˜ˆ์ธก
image_path = r'์‚ฌ์ง„ ์œ„์น˜ ์ž…๋ ฅ'  # ํ…Œ์ŠคํŠธํ•  ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ
result = predict_similarity(image_path)
print(result)


```