-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_rec_athira.py
182 lines (155 loc) · 8.05 KB
/
face_rec_athira.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import streamlit as st
import cv2
import mediapipe as mp
import numpy as np
import face_recognition
from PIL import Image
# Selfie Segmentation
mp_drawing = mp.solutions.drawing_utils
mp_selfie_segmentation = mp.solutions.selfie_segmentation
#Face Detection
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
st.subheader("Project on face recognition and detection")
st.write("""
\nDone by: Athira""")
add_selectbox = st.sidebar.selectbox(
"How would you like to edit your image?",
("Project features", "Background Changer", "Face Detection", "Face Recognition")
)
if add_selectbox == "Project features":
st.write("""The following features can be examined:""")
st.write("\n1. Background Changer \n2. Face Detection \n3. Face Recognition")
im = cv2.imread("/Users/lakshmipriya/Desktop/project/athie/face.jpg")
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
st.image(im)
# Background Changer
elif add_selectbox == "Background Changer":
with mp_selfie_segmentation.SelfieSegmentation(model_selection=0) as selfie_segmentation:
fg_image = st.sidebar.file_uploader("Upload a FOREGROUND IMAGE")
st.sidebar.write("Here, you can change the background of the image that you upload")
if fg_image is not None:
fimage = np.array(Image.open(fg_image))
st.sidebar.image(fimage)
results = selfie_segmentation.process(fimage)
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.1
add_bg = st.sidebar.selectbox(
"Change your Background",
("Image of your choice", "Inbuilt Image", "Colors")
)
if add_bg == "Image of your choice":
bg_image = st.sidebar.file_uploader("Upload a BACKGROUND IMAGE")
if bg_image is not None:
bimage = np.array(Image.open(bg_image))
st.sidebar.image(bimage)
bimage = cv2.resize(bimage, (fimage.shape[1], fimage.shape[0]))
output_image = np.where(condition, fimage, bimage)
st.image(output_image)
elif add_bg == "Inbuilt Image":
add_ib_bg = st.sidebar.selectbox(
"Choose background?",
("camera","forest","city","bottleship")
)
if add_ib_bg == "camera":
bg_image = cv2.imread("/Users/lakshmipriya/Desktop/project/athie/1.jpg")
bg_image = cv2.cvtColor(bg_image, cv2.COLOR_RGB2BGR)
if bg_image is not None:
st.sidebar.image(bg_image)
bimage = cv2.resize(bg_image, (fimage.shape[1], fimage.shape[0]))
output_image = np.where(condition, fimage, bimage)
st.image(output_image)
elif add_ib_bg == "forest":
bg_image = cv2.imread("/Users/lakshmipriya/Desktop/project/athie/2.jpg")
bg_image = cv2.cvtColor(bg_image, cv2.COLOR_RGB2BGR)
if bg_image is not None:
st.sidebar.image(bg_image)
bimage = cv2.resize(bg_image, (fimage.shape[1], fimage.shape[0]))
output_image = np.where(condition, fimage, bimage)
st.image(output_image)
elif add_ib_bg == "city":
bg_image = cv2.imread("/Users/lakshmipriya/Desktop/project/athie/3.jpg")
bg_image = cv2.cvtColor(bg_image, cv2.COLOR_RGB2BGR)
if bg_image is not None:
st.sidebar.image(bg_image)
bimage = cv2.resize(bg_image, (fimage.shape[1], fimage.shape[0]))
output_image = np.where(condition, fimage, bimage)
st.image(output_image)
elif add_ib_bg == "bottleship":
bg_image = cv2.imread("/Users/lakshmipriya/Desktop/project/athie/4.jpg")
bg_image = cv2.cvtColor(bg_image, cv2.COLOR_RGB2BGR)
if bg_image is not None:
st.sidebar.image(bg_image)
bimage = cv2.resize(bg_image, (fimage.shape[1], fimage.shape[0]))
output_image = np.where(condition, fimage, bimage)
st.image(output_image)
else:
st.write("Choose some other option")
elif add_bg == "Colors":
add_c_bg = st.sidebar.selectbox(
"Choose a color as a background",
("Red","Green","Blue","Gray")
)
if add_c_bg == "Red":
BG_COLOR = (255,0,0)
bg_image = np.zeros(fimage.shape, dtype=np.uint8)
bg_image[:] = BG_COLOR
output_image = np.where(condition, fimage, bg_image)
st.image(output_image)
elif add_c_bg == "Green":
BG_COLOR = (0,255,0)
bg_image = np.zeros(fimage.shape, dtype=np.uint8)
bg_image[:] = BG_COLOR
output_image = np.where(condition, fimage, bg_image)
st.image(output_image)
elif add_c_bg == "Blue":
BG_COLOR = (0,0,255)
bg_image = np.zeros(fimage.shape, dtype=np.uint8)
bg_image[:] = BG_COLOR
output_image = np.where(condition, fimage, bg_image)
st.image(output_image)
elif add_c_bg == "Gray":
BG_COLOR = (192,192,192)
bg_image = np.zeros(fimage.shape, dtype=np.uint8)
bg_image[:] = BG_COLOR
output_image = np.where(condition, fimage, bg_image)
st.image(output_image)
# Face Detection
elif add_selectbox == "Face Detection":
with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.6) as face_detection:
fd_image = st.sidebar.file_uploader("Upload a FOREGROUND IMAGE")
if fd_image is not None:
fdimage = np.array(Image.open(fd_image))
st.sidebar.image(fdimage)
results = face_detection.process(fdimage)
for landmark in results.detections:
mp_drawing.draw_detection(fdimage, landmark)
st.image(fdimage)
# Face Recognition
elif add_selectbox == "Face Recognition":
st.write("Upload TWO IMAGES")
image = st.sidebar.file_uploader("Upload the first image")
if image is not None:
train_image = np.array(Image.open(image))
st.sidebar.image(train_image)
image_train = face_recognition.load_image_file(image)
image_encodings_train = face_recognition.face_encodings(image_train)[0]
detect_image = st.sidebar.file_uploader("Upload the second image")
if detect_image is not None:
test_image = np.array(Image.open(detect_image))
st.sidebar.image(test_image)
image_test = face_recognition.load_image_file(detect_image)
image_encodings_test = face_recognition.face_encodings(image_test)[0]
image_location_test = face_recognition.face_locations(image_test)
results = face_recognition.compare_faces([image_encodings_test], image_encodings_train)[0]
dst = face_recognition.face_distance([image_encodings_test], image_encodings_train)
if results:
for (top, right, bottom, left) in image_location_test:
output_image1 = cv2.rectangle(test_image, (left, top), (right, bottom), (0, 0, 255), 2)
st.image(output_image1)
st.write("Both the faces are same")
else:
st.write("Both faces doesn't match")
else:
st.write("Upload a pic")
else:
st.write("Choose any of the given options")