1+ import numpy as np
2+ from flask import Flask , request , jsonify , render_template ,json
3+ import cv2
4+ from skimage .metrics import structural_similarity as ssim
5+
6+
7+
8+ app = Flask (__name__ )
9+
10+ @app .route ('/' )
11+ def home ():
12+ return jsonify ({'message' :'Welcome to Flask Apis' })
13+
14+ @app .route ('/img' ,methods = ['POST' ])
15+ def predict ():
16+ file1 = request .files ['file1' ]
17+ file2 = request .files ['file2' ]
18+
19+ # Read the images using OpenCV
20+ img1 = cv2 .imdecode (np .frombuffer (file1 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
21+ img2 = cv2 .imdecode (np .frombuffer (file2 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
22+
23+ # Resize the images to 256x256 pixels
24+ img1 = cv2 .resize (img1 , (256 , 256 ))
25+ img2 = cv2 .resize (img2 , (256 , 256 ))
26+
27+ # Convert the images to grayscale
28+ gray_img1 = cv2 .cvtColor (img1 , cv2 .COLOR_BGR2GRAY )
29+ gray_img2 = cv2 .cvtColor (img2 , cv2 .COLOR_BGR2GRAY )
30+
31+ # Calculate the structural similarity index between the images
32+ score = ssim (gray_img1 , gray_img2 , full = True )[0 ]
33+
34+ # Convert the similarity score to a percentage
35+ similarity_percentage = score * 100
36+
37+ # Return the similarity percentage in a JSON response
38+ return jsonify ({'similarity_percentage' : similarity_percentage })
39+
40+
41+ if __name__ == '__main__' :
42+ app .run (debug = True )
0 commit comments