1
1
import numpy as np
2
- from flask import Flask , request , jsonify , render_template ,json
2
+ from flask import Flask , request , jsonify , render_template , json
3
3
import cv2
4
4
from skimage .metrics import structural_similarity as ssim
5
5
6
6
7
-
8
7
app = Flask (__name__ )
9
8
10
- face_cascade = cv2 .CascadeClassifier (cv2 .data .haarcascades + "haarcascade_frontalface_default.xml" )
9
+ face_cascade = cv2 .CascadeClassifier (
10
+ cv2 .data .haarcascades + "haarcascade_frontalface_default.xml" )
11
+
11
12
12
13
@app .route ('/' )
13
14
def home ():
14
- return jsonify ({'message' :'Welcome to Flask Apis' })
15
+ return jsonify ({'message' : 'Welcome to Flask Apis' })
16
+
15
17
16
- @app .route ('/image_Compare' ,methods = ['POST' ])
18
+ @app .route ('/image_Compare' , methods = ['POST' ])
17
19
def predict ():
18
20
file1 = request .files ['file1' ]
19
21
file2 = request .files ['file2' ]
20
22
21
23
# Read the images using OpenCV
22
- img1 = cv2 .imdecode (np .frombuffer (file1 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
23
- img2 = cv2 .imdecode (np .frombuffer (file2 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
24
+ img1 = cv2 .imdecode (np .frombuffer (
25
+ file1 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
26
+ img2 = cv2 .imdecode (np .frombuffer (
27
+ file2 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
24
28
25
29
# Resize the images to 256x256 pixels
26
30
img1 = cv2 .resize (img1 , (256 , 256 ))
@@ -37,25 +41,30 @@ def predict():
37
41
similarity_percentage = score * 100
38
42
39
43
# Return the similarity percentage in a JSON response
40
- return jsonify ({'similarity_percentage' : similarity_percentage })
44
+ return jsonify ({'similarity_percentage' : similarity_percentage })
41
45
42
- @app .route ('/face_recognize' ,methods = ['POST' ])
46
+
47
+ @app .route ('/face_recognize' , methods = ['POST' ])
43
48
def predictface ():
44
- # Get the uploaded files from the request
49
+ # Get the uploaded files from the request
45
50
file1 = request .files ['file1' ]
46
51
file2 = request .files ['file2' ]
47
52
48
53
# Read the images using OpenCV directly from the request files
49
- img1 = cv2 .imdecode (np .frombuffer (file1 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
50
- img2 = cv2 .imdecode (np .frombuffer (file2 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
54
+ img1 = cv2 .imdecode (np .frombuffer (
55
+ file1 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
56
+ img2 = cv2 .imdecode (np .frombuffer (
57
+ file2 .read (), np .uint8 ), cv2 .IMREAD_COLOR )
51
58
52
59
# Convert the images to grayscale
53
60
gray_img1 = cv2 .cvtColor (img1 , cv2 .COLOR_BGR2GRAY )
54
61
gray_img2 = cv2 .cvtColor (img2 , cv2 .COLOR_BGR2GRAY )
55
62
56
63
# Detect faces in the images
57
- faces1 = face_cascade .detectMultiScale (gray_img1 , scaleFactor = 1.1 , minNeighbors = 5 )
58
- faces2 = face_cascade .detectMultiScale (gray_img2 , scaleFactor = 1.1 , minNeighbors = 5 )
64
+ faces1 = face_cascade .detectMultiScale (
65
+ gray_img1 , scaleFactor = 1.1 , minNeighbors = 5 )
66
+ faces2 = face_cascade .detectMultiScale (
67
+ gray_img2 , scaleFactor = 1.1 , minNeighbors = 5 )
59
68
60
69
# Compare only the first detected face in each image
61
70
if len (faces1 ) > 0 and len (faces2 ) > 0 :
@@ -81,5 +90,6 @@ def predictface():
81
90
else :
82
91
return jsonify ({'similarity_percentage' : 'Could not detect faces in both images.' })
83
92
93
+
84
94
if __name__ == '__main__' :
85
- app .run (debug = True )
95
+ app .run (debug = False )
0 commit comments