ricardo238costa commited on
Commit
2ad5883
·
verified ·
1 Parent(s): 67a7f39

Create similarity.py

Browse files
Files changed (1) hide show
  1. similarity.py +40 -0
similarity.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ from typing import List
4
+ from skimage.metrics import structural_similarity as ssim
5
+ import cv2
6
+ import numpy as np
7
+ import requests
8
+ from PIL import Image
9
+
10
+ from models import RequestModel, ResponseModel
11
+
12
+
13
+ def load_image_url(source):
14
+ if source.startswith('http'):
15
+ img = Image.open(requests.get(source, stream=True).raw)
16
+ else:
17
+ img_data = base64.b64decode(source)
18
+ img = Image.open(io.BytesIO(img_data))
19
+
20
+ img = np.array(img.convert('L'))
21
+ return img
22
+
23
+
24
+ def check_similarity(images: List[RequestModel]):
25
+ print(f'checking similarity...')
26
+
27
+ original_image = load_image_url(images[0].source)
28
+ original_image_shape = original_image.shape
29
+
30
+ results = []
31
+
32
+ for i in range(1, len(images)):
33
+ image = load_image_url(images[i].source)
34
+ image = cv2.resize(image, original_image_shape[::-1])
35
+ s, _ = ssim(original_image, image, full=True)
36
+ similarity_score = (s + 1) * 50
37
+ response = ResponseModel(originId=images[i].originId, sequence=images[i].sequence,
38
+ assetCode=images[i].assetCode, similarity=similarity_score)
39
+ results.append(response)
40
+ return results