Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload similarity.py
Browse files- similarity.py +33 -3
similarity.py
CHANGED
|
@@ -51,26 +51,56 @@ def mobilenet_sim(img1, img2, img1AssetCode, img2AssetCode):
|
|
| 51 |
logging.error("Erro ao calcular similaridade com MobileNet", exc_info=True)
|
| 52 |
return 0
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def orb_sim(img1, img2, img1AssetCode, img2AssetCode):
|
| 55 |
score = 0
|
| 56 |
-
|
| 57 |
try:
|
| 58 |
orb = cv2.ORB_create()
|
| 59 |
kp_a, desc_a = orb.detectAndCompute(img1, None)
|
| 60 |
kp_b, desc_b = orb.detectAndCompute(img2, None)
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
| 63 |
matches = bf.match(desc_a, desc_b)
|
|
|
|
| 64 |
similar_regions = [i for i in matches if i.distance < 20]
|
| 65 |
if len(matches) > 0:
|
| 66 |
score = (len(similar_regions) / len(matches)) * 100
|
| 67 |
-
if
|
| 68 |
logging.info(f"Orb score from {img1AssetCode} and {img2AssetCode} is {score}")
|
| 69 |
except Exception as e:
|
| 70 |
logging.error("Erro ao verificar similaridade ORB", exc_info=True)
|
| 71 |
|
| 72 |
return 1 if 0 < score < 1 else score
|
| 73 |
|
|
|
|
| 74 |
def ssim_sim(img1, img2):
|
| 75 |
s, _ = ssim(img1, img2, full=True)
|
| 76 |
return (s + 1) * 50
|
|
|
|
| 51 |
logging.error("Erro ao calcular similaridade com MobileNet", exc_info=True)
|
| 52 |
return 0
|
| 53 |
|
| 54 |
+
# def orb_sim(img1, img2, img1AssetCode, img2AssetCode):
|
| 55 |
+
# score = 0
|
| 56 |
+
#
|
| 57 |
+
# try:
|
| 58 |
+
# orb = cv2.ORB_create()
|
| 59 |
+
# kp_a, desc_a = orb.detectAndCompute(img1, None)
|
| 60 |
+
# kp_b, desc_b = orb.detectAndCompute(img2, None)
|
| 61 |
+
#
|
| 62 |
+
# bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
| 63 |
+
# matches = bf.match(desc_a, desc_b)
|
| 64 |
+
# similar_regions = [i for i in matches if i.distance < 20]
|
| 65 |
+
# if len(matches) > 0:
|
| 66 |
+
# score = (len(similar_regions) / len(matches)) * 100
|
| 67 |
+
# if (score > 0):
|
| 68 |
+
# logging.info(f"Orb score from {img1AssetCode} and {img2AssetCode} is {score}")
|
| 69 |
+
# except Exception as e:
|
| 70 |
+
# logging.error("Erro ao verificar similaridade ORB", exc_info=True)
|
| 71 |
+
#
|
| 72 |
+
# return 1 if 0 < score < 1 else score
|
| 73 |
+
|
| 74 |
+
|
| 75 |
def orb_sim(img1, img2, img1AssetCode, img2AssetCode):
|
| 76 |
score = 0
|
|
|
|
| 77 |
try:
|
| 78 |
orb = cv2.ORB_create()
|
| 79 |
kp_a, desc_a = orb.detectAndCompute(img1, None)
|
| 80 |
kp_b, desc_b = orb.detectAndCompute(img2, None)
|
| 81 |
+
|
| 82 |
+
if desc_a is None or desc_b is None:
|
| 83 |
+
logging.warning(f"ORB descriptors are None for {img1AssetCode} or {img2AssetCode}, skipping.")
|
| 84 |
+
return 0
|
| 85 |
+
|
| 86 |
+
if desc_a.dtype != desc_b.dtype or desc_a.shape[1] != desc_b.shape[1]:
|
| 87 |
+
logging.warning(f"ORB descriptors incompatible for {img1AssetCode} and {img2AssetCode}, skipping.")
|
| 88 |
+
return 0
|
| 89 |
+
|
| 90 |
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
| 91 |
matches = bf.match(desc_a, desc_b)
|
| 92 |
+
|
| 93 |
similar_regions = [i for i in matches if i.distance < 20]
|
| 94 |
if len(matches) > 0:
|
| 95 |
score = (len(similar_regions) / len(matches)) * 100
|
| 96 |
+
if score > 0:
|
| 97 |
logging.info(f"Orb score from {img1AssetCode} and {img2AssetCode} is {score}")
|
| 98 |
except Exception as e:
|
| 99 |
logging.error("Erro ao verificar similaridade ORB", exc_info=True)
|
| 100 |
|
| 101 |
return 1 if 0 < score < 1 else score
|
| 102 |
|
| 103 |
+
|
| 104 |
def ssim_sim(img1, img2):
|
| 105 |
s, _ = ssim(img1, img2, full=True)
|
| 106 |
return (s + 1) * 50
|