Skip to content

Commit f4646d0

Browse files
committed
Final Push
1 parent a5d7b20 commit f4646d0

14 files changed

+460
-18
lines changed

FaceRecognition/.app.py.swp

12 KB
Binary file not shown.

FaceRecognition/FaceRecognitionAPI.py FaceRecognition/Face Recognition API.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# import required pacakges
21
from flask import Flask, request
32
import cv2
43
from PIL import Image
@@ -8,23 +7,22 @@
87
import json
98
import face_recognition
109

11-
# convert a base64 string to image
10+
1211
def getI420FromBase64(codec, image_path=""):
1312
base64_data = re.sub('^data:image/.+;base64,', '', codec)
1413
byte_data = base64.b64decode(base64_data)
1514
image_data = BytesIO(byte_data)
1615
img = Image.open(image_data)
1716
img.save('input' + '.png', "PNG")
1817

19-
# detect the user face in the given image
2018
def authenticate(user,img):
2119
try:
2220
input_face_encoding = face_recognition.face_encodings(img)[0]
2321
except:
2422
return -1
2523
image_encodings = []
2624
filename = 'data/'+user+'/'
27-
for i in range(10):
25+
for i in range(5):
2826
unknown_image = face_recognition.load_image_file(filename+str(i)+'.jpg')
2927
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
3028
image_encodings.append(unknown_face_encoding)
@@ -33,23 +31,22 @@ def authenticate(user,img):
3331
results = face_recognition.compare_faces(image_encodings, input_face_encoding)
3432
response = results.count(True)
3533
print(response,results)
36-
if response >= 7:
34+
if response >= 3:
3735
return 1
3836
else:
3937
return 0
4038

4139
app=Flask(__name__)
4240

43-
4441
@app.route('/',methods=['GET', 'POST'])
4542
def cardpayapi():
46-
r = request.get_json() # receive the json (base64 string of image, user name) from client
43+
r = request.get_json()
4744
# print(r['image'])
48-
getI420FromBase64(r['image']) # convert base64 string to image
49-
user = r['user']
45+
getI420FromBase64(r['image'])
46+
user = r['user']
5047
user = user.lower()
5148
img = cv2.imread('input.png')
52-
response = authenticate(user,img) # get the response from authenticate function
49+
response = authenticate(user,img)
5350
result=""
5451
if response == -1:
5552
result = "NOT FOUND"

FaceRecognition/FaceSignup.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import required pacakges
1+
# import required pacakges
22
import cv2
33
import numpy as np
44
from skimage.transform import rotate, AffineTransform, warp
@@ -12,72 +12,84 @@
1212
import re, base64
1313
from flask import jsonify
1414
import json
15+
1516
#Lets define functions for each operation
1617
def anticlockwise_rotation(image):
1718
angle = random.randint(0,180)
1819
return rotate(image, angle)
20+
1921
def clockwise_rotation(image):
2022
angle = random.randint(0,180)
2123
return rotate(image, -angle)
24+
2225
def h_flip(image):
2326
return np.fliplr(image)
27+
2428
def v_flip(image):
2529
return np.flipud(image)
30+
2631
def add_noise(image):
2732
return random_noise(image)
33+
2834
def blur_image(image):
2935
return cv2.GaussianBlur(image, (9,9),0)
36+
3037
#classifying blur and non-blur images
3138
def warp_shift(image):
3239
transform = AffineTransform(translation=(0,40)) #chose x,y values according to your convinience
3340
warp_image = warp(image, transform, mode="wrap")
3441
return warp_image
42+
3543
# generate images
3644
def generate_images(img,destination,num_images,user):
3745
directory = user
3846
parent_dir = "data/" # Parent Directory path
3947
path = os.path.join(parent_dir, directory)
4048
os.mkdir(path)
49+
4150
transformations = {
4251
'warp shift': warp_shift,
4352
'adding noise': add_noise,
4453
'blurring image':blur_image
4554
} #use dictionary to store names of functions
55+
4656
# images_path = source #path to original images
4757
augmented_path = destination # path to store aumented images
4858
images = [] # to store paths of images from folder
59+
4960
images_to_generate = num_images #you can change this value according to your requirement
5061
i = 1 # variable to iterate till images_to_generate
5162
while i <= images_to_generate:
5263
original_image = img
5364
transformed_image = None
5465
# print(i)
5566
n = 0 #variable to iterate till number of transformation to apply
56-
transformation_count = random.randint(1, len(transformations)) #choose random number of transformation to ap
57-
ply on the image
67+
transformation_count = random.randint(1, len(transformations)) #choose random number of transformation to apply on the image
5868

5969
while n <= transformation_count:
6070
key = random.choice(list(transformations)) #randomly choosing method to call
6171
transformed_image = transformations[key](original_image)
6272
n = n + 1
6373

6474
new_image_path = "data/"+user+"/"+"%s.jpg" %(i-1)
65-
transformed_image = img_as_ubyte(transformed_image) #Convert an image to unsigned byte format, with values
66-
in [0, 255].
67-
transformed_image = cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB) #convert image to RGB before saving i
68-
t
75+
transformed_image = img_as_ubyte(transformed_image) #Convert an image to unsigned byte format, with values in [0, 255].
76+
transformed_image = cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB) #convert image to RGB before saving it
6977
cv2.imwrite(new_image_path, transformed_image) # save transformed image to path
7078
i =i+1
7179
print('Finished!!')
7280
#to generate more images, put above 3 statement inside while n<... loop
81+
7382
# convert a base64 string to image
7483
def getI420FromBase64(codec, image_path=""):
7584
base64_data = re.sub('^data:image/.+;base64,', '', codec)
7685
byte_data = base64.b64decode(base64_data)
7786
image_data = BytesIO(byte_data)
7887
img = Image.open(image_data)
7988
img.save('input' + '.png', "PNG")
89+
8090
app=Flask(__name__)
91+
92+
8193
@app.route('/',methods=['GET', 'POST'])
8294
def cardpayapi():
8395
r = request.get_json() # receive the json (base64 string of image, user name) from client
@@ -93,4 +105,4 @@ def cardpayapi():
93105
result = "failed"
94106
return {"result":result}
95107
if __name__=='__main__':
96-
app.run(host='0.0.0.0', debug=True,use_reloader=False,port=5001)
108+
app.run(host='0.0.0.0', debug=True,use_reloader=False,port=5001)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"os.getcwd()\n",
11+
"os.chdir('/home/tharun/a1.PROJECTS/CardPay/data/rohith4282/')"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 3,
17+
"metadata": {},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"IMG20200712145156.jpg\n",
24+
"IMG20200712145136.jpg\n",
25+
"IMG20200712145209.jpg\n",
26+
"IMG20200712145129.jpg\n",
27+
"IMG20200712145130.jpg\n",
28+
"IMG20200712145121.jpg\n",
29+
"IMG20200712145200.jpg\n",
30+
"IMG20200712145145.jpg\n",
31+
"IMG20200712145137.jpg\n",
32+
"IMG20200712145142.jpg\n",
33+
"IMG20200712145127.jpg\n",
34+
"IMG20200712145139.jpg\n",
35+
"IMG20200712145203.jpg\n",
36+
"IMG20200712145045.jpg\n",
37+
"IMG20200712145206.jpg\n",
38+
"IMG20200712145125.jpg\n",
39+
"IMG20200712145118.jpg\n",
40+
"IMG20200712145150.jpg\n",
41+
"IMG20200712145152.jpg\n",
42+
"IMG20200712145148.jpg\n"
43+
]
44+
}
45+
],
46+
"source": [
47+
"collection = \"/home/tharun/a1.PROJECTS/CardPay/data/rohith4282/\"\n",
48+
"for i , filename in enumerate(os.listdir(collection)):\n",
49+
" print(filename)"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 5,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"\n",
59+
"collection = \"/home/tharun/a1.PROJECTS/CardPay/data/rohith4282/\"\n",
60+
"for i, filename in enumerate(os.listdir(collection)):\n",
61+
" os.rename(\"/home/tharun/a1.PROJECTS/CardPay/data/rohith4282/\"+filename,str(i) + \".jpg\")"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": []
70+
}
71+
],
72+
"metadata": {
73+
"kernelspec": {
74+
"display_name": "Python 3",
75+
"language": "python",
76+
"name": "python3"
77+
},
78+
"language_info": {
79+
"codemirror_mode": {
80+
"name": "ipython",
81+
"version": 3
82+
},
83+
"file_extension": ".py",
84+
"mimetype": "text/x-python",
85+
"name": "python",
86+
"nbconvert_exporter": "python",
87+
"pygments_lexer": "ipython3",
88+
"version": "3.7.4"
89+
}
90+
},
91+
"nbformat": 4,
92+
"nbformat_minor": 2
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"import cv2\n",
11+
"import numpy as np\n",
12+
"import os"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"def detectface(img):\n",
22+
" face_cascade = cv2.CascadeClassifier('/home/tharun/a1.PROJECTS/CardPay/facedetection/haarcascade_frontalface_default.xml') \n",
23+
" gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) \n",
24+
" faces = face_cascade.detectMultiScale(gray, 1.3, 5)\n",
25+
" if faces is ():\n",
26+
" return False\n",
27+
" for (x, y, w, h) in faces:\n",
28+
" roi_gray = img[y:y + h, x:x + w]\n",
29+
" cropped_img = np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (224, 224)), -1), 0)\n",
30+
" return roi_gray"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 3,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"os.chdir('/home/tharun/a1.PROJECTS/CardPay/data/rohith4282/') "
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 4,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"data": {
49+
"text/plain": [
50+
"'/home/tharun/a1.PROJECTS/CardPay/data/rohith4282'"
51+
]
52+
},
53+
"execution_count": 4,
54+
"metadata": {},
55+
"output_type": "execute_result"
56+
}
57+
],
58+
"source": [
59+
"\n",
60+
"os.getcwd()"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 5,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"\n",
70+
"collection = '/home/tharun/a1.PROJECTS/CardPay/data/rohith4282/'\n",
71+
"for i , filename in enumerate(os.listdir(collection)):\n",
72+
" image = cv2.imread(collection+filename)\n",
73+
" img=detectface(image)\n",
74+
" cv2.imwrite(filename, img) \n",
75+
" "
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "Python 3",
89+
"language": "python",
90+
"name": "python3"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.7.4"
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 2
107+
}

0 commit comments

Comments
 (0)