Notes
Notes - notes.io |
def liveness_detector():
global image_path
global temp_path
fd_model_path = "/home/plnt_admin/app/intel/face-detection-0200/FP32/face-detection-0200.xml"
fd_threshold = 0.6
device = 'CPU'
face_detector = FaceDetector(fd_model_path,fd_threshold, device, None)
spf_model_path = "/home/plnt_admin/app/spf_models/MN3_antispoof.xml"
spoof_model = VectorCNN(spf_model_path)
# vidcap = cv2.VideoCapture('/home/sharjeel/Documents/light-weight-face-anti-spoofing/test.mp4')
brightness_threshold = 100
spoof_threshold = 0.95
real_threshold = 0.2
spoof_flag = False
# real_flag = False
since_last_spoof_counter = 0
spoof_flag_counter = 0
off_center_counter = 0
spoof_counter = 0
real_counter = 0
spoof_counter_threshold = 10
real_counter_threshold = 0.7
last_spoof_threshold = 1
spoof_flag_threshold = 50
off_center_threshold = 10
if request.method == "POST":
response = {'message' : 'Inconclusive, Try Again',
'real_flag' : False,
'spoof_flag': spoof_flag,
'liveness_score': 0,
'off_center_counter': off_center_counter,
'spoof_flag_counter': spoof_flag_counter,
'since_last_spoof_counter': since_last_spoof_counter,
'spoof_counter': spoof_counter}
frame_count = request.form['frame_count']
running_flag = True
for i in range(0,int(frame_count)):
if running_flag:
frame_key="frame"+str(i)
base64_s = request.form[str(frame_key)]
base64_s=base64_s.split(',')
base64_s=base64_s[1]
now = datetime.now()
now.strftime("%d%m%Y%H%M%S%.3f")
cur_date=now.strftime("%d%m%Y%H%M%S%.3f")
filename=image_path+'live_'+cur_date+'_'+str(i)+'.png'
# filename1=image_path+cur_date+'.jpg'
img = Image.open(io.BytesIO(base64.decodebytes(bytes(base64_s, "utf-8"))))
img.save(filename)
img = cv2.imread(filename)
img_rotate_90_clockwise = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
cv2.imwrite(filename, img_rotate_90_clockwise)
img = Image.open(filename)
# print('Rotating into correct orientation...')
if hasattr(ImageOps, 'exif_transpose'):
# Very recent versions of PIL can do exit transpose internally
img = ImageOps.exif_transpose(img)
else:
# Otherwise, do the exif transpose ourselves
img = exif_transpose(img)
img.save(filename)
frame = cv2.imread(filename)
# has_frame, frame = vidcap.read()
# if not has_frame:
# break
frame = increase_brightness(frame, value=-10)
frame = contrast(frame, value=-10)
frame = sat(frame)
frame = white_balance(frame)
detections = face_detector.get_detections(frame)
if not detections:
response['message'] = "Face not Found in image."
response['code'] = 2
return response
left, top, right, bottom = detections[0][0]
b = check_brightness(frame)
if b <= brightness_threshold:
running_flag = False
response['message'] = "Kindly ensure adequate light conditions are followed."
response['code'] = 2
return response
# sat_frame = spoof_frame(frame)
# sat_detections = face_detector.get_detections(sat_frame)
# if sat_detections:
# spoof_flag = True
# since_last_spoof_counter = 0
# else:
# spoof_flag = False
# since_last_spoof_counter += 1
# frame_width = vidcap.get(3)
# width_limits = [frame_width * 0.2, frame_width * 0.8]
h, w, _ = frame.shape
size = [h, w]
pixels=size[0]*size[1]
sat_frame=spoof_frame(frame)
red_pix=np.sum(sat_frame==255)/(pixels)
if red_pix<0.2:
face_pixels = abs((right-left) * (top-bottom))
face_sat_frame = spoof_frame(frame[top:bottom, left:right])
face_red_pix = np.sum(face_sat_frame == 255)/(face_pixels)
if face_red_pix<0.2:
running_flag=False
response['message']='Kindly ensure there is no light source behind you.'
response['code'] = 2
return response
faces =[]
confidence = None, None
for rect, _ in detections:
left, top, right, bottom = rect
faces.append(frame[top:bottom, left:right])
if faces:
confidence = spoof_model.forward(faces)
confidence = list(map(lambda x: x.reshape(-1), confidence))
for i, rect in enumerate(detections):
left, top, right, bottom = rect[0]
area = calc_area(rect[0], size)
if area < 0.15:
off_center_counter += 1
if spoof_flag:
spoof_flag_counter += 1
elif confidence[i][0] > real_threshold:
# real_flag = True
real_counter += 1
elif confidence[i][1] > spoof_threshold:
spoof_counter += 1
if off_center_counter == off_center_threshold:
running_flag = False
response['message'] = 'Face too far, kindly move closer to camera and retry.'
response['code'] = 2
elif spoof_flag_counter == spoof_flag_threshold:
running_flag = False
response['message'] = 'Possible fraud Detected'
response['code'] = 1
elif spoof_counter >= spoof_counter_threshold:
running_flag = False
response['message'] = 'Possible fraud Detected'
response['code'] = 1
print('Spoof Counter:', spoof_counter,' ','Percentage:',confidence[i][1])
print('Real Counter:', real_counter,' ','Percentage:',confidence[i][0])
print('Area: ', area )
continue
break
response['spoof_flag'] = spoof_flag
response['off_center_counter'] = off_center_counter
response['spoof_flag_counter'] = spoof_flag_counter
response['since_last_spoof_counter'] = since_last_spoof_counter
response['spoof_counter'] = spoof_counter
if spoof_counter/(real_counter + spoof_counter + 0.0001)>= spoof_counter_threshold:
response['real_flag'] = False
response['message'] = 'Possible Fraud Detected.'
response['code'] = 1
if real_counter/(real_counter+spoof_counter + 0.0001) >= real_counter_threshold and running_flag == True:
response['real_flag'] = True
response['message'] = 'Live Face Detected'
response['code'] = 0
response['liveness_score'] = real_counter/int(frame_count)
print(response)
return response
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team