MarioPrzBasto commited on
Commit
b4c89f9
·
verified ·
1 Parent(s): a079428

Update similarity.py

Browse files
Files changed (1) hide show
  1. similarity.py +47 -6
similarity.py CHANGED
@@ -1,16 +1,52 @@
1
  import base64
2
- from typing import List
3
- from skimage.metrics import structural_similarity as ssim
4
  import cv2
5
  import numpy as np
6
  import requests
 
 
 
 
 
 
 
 
7
  from models import RequestModel, ResponseModel
8
  from PIL import Image
9
  from io import BytesIO
10
- import logging
11
 
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def orb_sim(img1, img2):
15
  score = 0
16
 
@@ -31,6 +67,10 @@ def orb_sim(img1, img2):
31
 
32
  return score
33
 
 
 
 
 
34
 
35
  def load_image_url(source):
36
  Image.MAX_IMAGE_PIXELS = None
@@ -59,16 +99,17 @@ def check_similarity(images: List[RequestModel]):
59
  try:
60
  image = load_image_url(images[i].source)
61
  image = cv2.resize(image, original_image_shape[::-1])
62
- s, _ = ssim(original_image, image, full=True)
63
- similarity_score = (s + 1) * 50
64
  similarity_orb_score = orb_sim(original_image, image) * 100
 
65
  except Exception as e:
66
  logging.error(f"Error loading image for resource id {images[i].originId} : {e}")
67
  similarity_score = 0
68
  similarity_orb_score = 0
69
 
70
  response = ResponseModel(originId=images[i].originId, source=images[i].source, sequence=images[i].sequence,
71
- assetCode=images[i].assetCode, similarity=similarity_score, similarityOrb=similarity_orb_score)
72
  results.append(response)
73
 
74
  return results
 
1
  import base64
 
 
2
  import cv2
3
  import numpy as np
4
  import requests
5
+ import logging
6
+ from typing import List
7
+ from tensorflow.keras.applications import MobileNetV2
8
+ from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
9
+ from tensorflow.keras.models import Model
10
+ from tensorflow.keras.preprocessing.image import img_to_array
11
+ from sklearn.metrics.pairwise import cosine_similarity
12
+ from skimage.metrics import structural_similarity as ssim
13
  from models import RequestModel, ResponseModel
14
  from PIL import Image
15
  from io import BytesIO
 
16
 
17
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
18
 
19
+ def preprocess_image_for_mobilenet(image):
20
+ if len(image.shape) == 2:
21
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
22
+ elif image.shape[2] == 1:
23
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
24
+ else:
25
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
+
27
+ image = cv2.resize(image, (224, 224))
28
+ image = img_to_array(image)
29
+ image = np.expand_dims(image, axis=0)
30
+ image = preprocess_input(image)
31
+
32
+ return image
33
+
34
+ def mobilenet_sim(img1, img2):
35
+ try:
36
+ img1_proc = preprocess_image_for_mobilenet(img1)
37
+ img2_proc = preprocess_image_for_mobilenet(img2)
38
+
39
+ feat1 = mobilenet.predict(img1_proc, verbose=0)
40
+ feat2 = mobilenet.predict(img2_proc, verbose=0)
41
+
42
+ sim = cosine_similarity(feat1, feat2)[0][0]
43
+ sim_score = (sim + 1) * 50
44
+ print(f"MobileNet similarity score is {sim_score}")
45
+ return float(sim_score)
46
+ except Exception as e:
47
+ logging.error("Erro ao calcular similaridade com MobileNet", exc_info=True)
48
+ return 0
49
+
50
  def orb_sim(img1, img2):
51
  score = 0
52
 
 
67
 
68
  return score
69
 
70
+ def ssim_sim(img1, img2):
71
+ s, _ = ssim(img1, img2, full=True)
72
+ return (s + 1) * 50
73
+
74
 
75
  def load_image_url(source):
76
  Image.MAX_IMAGE_PIXELS = None
 
99
  try:
100
  image = load_image_url(images[i].source)
101
  image = cv2.resize(image, original_image_shape[::-1])
102
+
103
+ similarity_score = ssim_sim(original_image, image)
104
  similarity_orb_score = orb_sim(original_image, image) * 100
105
+ similarity_mobilenet_score = mobilenet_sim(original_image, image)
106
  except Exception as e:
107
  logging.error(f"Error loading image for resource id {images[i].originId} : {e}")
108
  similarity_score = 0
109
  similarity_orb_score = 0
110
 
111
  response = ResponseModel(originId=images[i].originId, source=images[i].source, sequence=images[i].sequence,
112
+ assetCode=images[i].assetCode, similarity=similarity_score, similarityOrb=similarity_orb_score, similarityMobileNet=similarity_mobilenet_score)
113
  results.append(response)
114
 
115
  return results