Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ from PIL import Image
|
|
| 15 |
from ultralytics import YOLO
|
| 16 |
from PIL import Image
|
| 17 |
from facenet_pytorch import MTCNN
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
app = Flask(__name__)
|
|
@@ -25,6 +26,8 @@ binary_labels = ['Real','Spoof']
|
|
| 25 |
multi_voice_labels = ['Real','Text to Speech','Voice Conversion','Text to Speech + Voice Conversion']
|
| 26 |
multi_face_labels = ['Genuine Face','Printed Photo','Paper Cut','Replayed Face','3D Mask']
|
| 27 |
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def process_audio(encoded_audio):
|
| 30 |
decoded_audio = base64.b64decode(encoded_audio)
|
|
@@ -83,6 +86,7 @@ def process_image(base64_img,extend=0):
|
|
| 83 |
def process_fingerprint_image(base64_img):
|
| 84 |
image_data = base64.b64decode(base64_img)
|
| 85 |
img = Image.open(io.BytesIO(image_data)).convert('RGB')
|
|
|
|
| 86 |
if isinstance(img, torch.Tensor):
|
| 87 |
img = transforms.ToPILImage()(img)
|
| 88 |
elif isinstance(img, np.ndarray):
|
|
@@ -94,7 +98,9 @@ def process_fingerprint_image(base64_img):
|
|
| 94 |
])
|
| 95 |
|
| 96 |
img = transformer(img)
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
|
|
@@ -462,22 +468,25 @@ def post_api_fingerprint():
|
|
| 462 |
encoded_image = data['base64']
|
| 463 |
|
| 464 |
# Process the image
|
| 465 |
-
processsed_image = process_fingerprint_image(encoded_image)
|
| 466 |
-
|
| 467 |
-
with torch.no_grad():
|
| 468 |
-
output, _ = vit_binary_model(processsed_image)
|
| 469 |
-
prob = torch.nn.functional.softmax(output[0], dim=0)
|
| 470 |
-
pred = torch.argmax(prob).item()
|
| 471 |
-
category = binary_labels[pred]
|
| 472 |
-
probs_dict = {binary_labels[i]: prob[i].item()*100 for i in range(len(binary_labels))}
|
| 473 |
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
except KeyError as e:
|
| 483 |
return jsonify({'error': f'Missing key: {str(e)}','status':400}), 400
|
|
|
|
| 15 |
from ultralytics import YOLO
|
| 16 |
from PIL import Image
|
| 17 |
from facenet_pytorch import MTCNN
|
| 18 |
+
from ultralytics import YOLO
|
| 19 |
|
| 20 |
|
| 21 |
app = Flask(__name__)
|
|
|
|
| 26 |
multi_voice_labels = ['Real','Text to Speech','Voice Conversion','Text to Speech + Voice Conversion']
|
| 27 |
multi_face_labels = ['Genuine Face','Printed Photo','Paper Cut','Replayed Face','3D Mask']
|
| 28 |
|
| 29 |
+
finger_print_detector = YOLO('fingerprint_best.pt')
|
| 30 |
+
|
| 31 |
|
| 32 |
def process_audio(encoded_audio):
|
| 33 |
decoded_audio = base64.b64decode(encoded_audio)
|
|
|
|
| 86 |
def process_fingerprint_image(base64_img):
|
| 87 |
image_data = base64.b64decode(base64_img)
|
| 88 |
img = Image.open(io.BytesIO(image_data)).convert('RGB')
|
| 89 |
+
|
| 90 |
if isinstance(img, torch.Tensor):
|
| 91 |
img = transforms.ToPILImage()(img)
|
| 92 |
elif isinstance(img, np.ndarray):
|
|
|
|
| 98 |
])
|
| 99 |
|
| 100 |
img = transformer(img)
|
| 101 |
+
results = finger_print_detector(img)
|
| 102 |
+
is_detected = any(np.array(results[0].boxes.cls.cpu())==0)
|
| 103 |
+
return img.unsqueeze(0),is_detected
|
| 104 |
|
| 105 |
|
| 106 |
|
|
|
|
| 468 |
encoded_image = data['base64']
|
| 469 |
|
| 470 |
# Process the image
|
| 471 |
+
processsed_image, is_detected = process_fingerprint_image(encoded_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
+
if is_detected:
|
| 474 |
+
with torch.no_grad():
|
| 475 |
+
output, _ = vit_binary_model(processsed_image)
|
| 476 |
+
prob = torch.nn.functional.softmax(output[0], dim=0)
|
| 477 |
+
pred = torch.argmax(prob).item()
|
| 478 |
+
category = binary_labels[pred]
|
| 479 |
+
probs_dict = {binary_labels[i]: prob[i].item()*100 for i in range(len(binary_labels))}
|
| 480 |
+
|
| 481 |
+
response = {
|
| 482 |
+
'message': 'Data received!',
|
| 483 |
+
'class': category,
|
| 484 |
+
'probs': probs_dict,
|
| 485 |
+
'status':200
|
| 486 |
+
}
|
| 487 |
+
return jsonify(response), 201
|
| 488 |
+
else:
|
| 489 |
+
return jsonify({'error': f'No Fingerprint Detected','status':400}), 400
|
| 490 |
|
| 491 |
except KeyError as e:
|
| 492 |
return jsonify({'error': f'Missing key: {str(e)}','status':400}), 400
|