-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsamples.py
More file actions
1026 lines (829 loc) · 37.2 KB
/
samples.py
File metadata and controls
1026 lines (829 loc) · 37.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Sample file demonstrating all public methods of the Session class.
This script shows how to use the cryptonets_python_sdk with type-safe Session API.
"""
from ast import List
import os
import sys
import pathlib
from typing import List, Optional, Union
from matplotlib import pyplot as plt
from msgspec import UNSET, UnsetType
# Import SDK components
from cryptonets_python_sdk.library import PrivIDFaceLib
from cryptonets_python_sdk.session import Session, SessionError, ImageInputArg
from cryptonets_python_sdk.flags import FlagUtil
from cryptonets_python_sdk.library_loader import LibraryLoadError
from cryptonets_python_sdk.idl.gen.privateid_types import (
SessionSettings,
Collection,
OperationConfig,
FaceResult,
FaceTraitsFlags,
DocumentTraits,
ReturnStatus,
CallResult,
)
import numpy as np
# Global configuration
# Image paths
SCRIPT_DIR = pathlib.Path(__file__).parent
IMAGE_DIR = pathlib.Path(__file__).parent / "images"
OUTPUT_DIR = pathlib.Path(__file__).parent / "output"
FACE_IMAGE = str(IMAGE_DIR / "tom_hanks_1.png")
FACE_IMAGE_2 = str(IMAGE_DIR / "tom_hanks_2.jpg")
COMPARE_FACE = str(IMAGE_DIR / "compare_face1.png")
COMPARE_DOC = str(IMAGE_DIR / "compare_doc1.png")
ISO_IMAGE = str(IMAGE_DIR / "tom_hanks_1.png")
SPOOF_IMAGE = str(IMAGE_DIR / "spoof.png")
CONSIDER_BIG_FACE = str(IMAGE_DIR / "consider_big_face.png")
INVALID_FACE = str(IMAGE_DIR / "invalid_face.png")
NO_FACE = str(IMAGE_DIR / "no_face.png")
ENV_FILE = SCRIPT_DIR / ".env"
# try to load CRYPTONETS_BASE_URL and CRYPTONETS_API_KEY from .env file if available
# useful for local testing
if ENV_FILE.exists():
from dotenv import load_dotenv
load_dotenv(dotenv_path=ENV_FILE)
# Test configuration - can be overridden via environment variables
BASE_URL = os.getenv("CRYPTONETS_BASE_URL", "https://xxxxxxxxxxxxxxxxxxx")
API_KEY = os.getenv("CRYPTONETS_API_KEY", "xxxxxxxxxxxxxxxx")
def ensure_output_dir() -> pathlib.Path:
"""Ensure output directory exists and return its path.
Returns:
Path to the output directory
"""
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
return OUTPUT_DIR
def create_session_settings(api_key: str, base_url: str) -> SessionSettings:
"""Create SessionSettings with multiple collections.
Args:
api_key: API key for authentication
base_url: Base URL for the API
Returns:
SessionSettings configured with default, RES100, and RES200 collections
Routes can be custoimized as needed as per the API deployment.
"""
return SessionSettings(
collections={
"default": Collection(
named_urls= {
"base_url" : base_url,
"predict": f"{base_url}/FACE3_4/predict",
"enroll": f"{base_url}/FACE3_4/enroll",
"deleteUser": f"{base_url}/FACE3_4/deleteUser"
}
),
"RES100": Collection(
named_urls= {
"base_url" : base_url,
"predict": f"{base_url}/RES100/predict",
"enroll": f"{base_url}/RES100/enroll",
"deleteUser": f"{base_url}/RES100/deleteUser"
},
embedding_model_id=14
),
"RES200": Collection(
named_urls= {
"base_url" : base_url,
"predict": f"{base_url}/RES200/predict",
"enroll": f"{base_url}/RES200/enroll",
"deleteUser": f"{base_url}/RES200/deleteUser"
},
embedding_model_id=19
)
},
session_token=api_key
)
def display_face_object(face: FaceResult,index: int):
"""Display information about a detected face object."""
print(f"\nFace #{index + 1}:")
traits = FlagUtil.get_active_flags(FaceTraitsFlags, face.face_traits_flags)
print(f"\n Face Traits Flags:")
for flag in traits:
print(f"\n -{flag.name}")
if face.geometry is not None:
print(f"\n Confidence: {face.geometry.face_confidence_score}")
print(f"\n Bounding Box: {face.geometry.bounding_box}")
if face.age_data is not UNSET:
print(f"\n Estimated Age: {face.age_data.estimated_age} , Confidence Score: {face.age_data.age_confidence_score}")
if face.ids is not UNSET:
print(f"\n Face ID - PUID: {face.ids.puid}, GUID: {face.ids.guid}")
if face.spoof_status is not None:
print(f"\n Spoof Status: {face.spoof_status.name}")
def display_faces_collection(faces: Union[List[FaceResult], UnsetType]):
"""Display information about a collection of detected faces."""
if faces is not UNSET and faces is not None and len(faces) > 0:
print(f"\nDetected {len(faces)} face(s):")
for i, face in enumerate(faces):
display_face_object(face, i)
else:
print("\nNo faces detected.")
def display_returned_status(op_id: int, call_result: CallResult):
"""Display the returned status (CallHeader) from a call result."""
header = call_result.call_status
assert header is not None, "CallResult should contain a CallResultHeader"
# When a call fails, op_id is negative (-1)
# The call_result will still contain the status information
if op_id < 0:
print(f"\nOperation failed with:")
print(f"\n Status code: {header.return_status.name}")
print(f"\n Message: {header.return_message}")
return
# When call is successful, op_id is non-negative
assert op_id >= 0, "Operation ID should be non-negative for successful calls"
assert header.return_status == ReturnStatus.API_NO_ERROR, "Return status should indicate no error for successful calls"
assert header.operation_id == op_id, "Operation ID in header should match the returned operation ID"
print(f"\nCall Result for Operation ID {op_id}:")
print(f"\n Return Status: {call_result.call_status.return_status}")
# Information about what the `CallResult` contains
# depends on the specific API method called
print("\nThe result contains the following data object(s):")
if call_result.faces is not UNSET:
print(f"\n - Faces collection with {len(call_result.faces)} face(s)")
if call_result.enroll is not UNSET:
print("\n - Enroll data")
if call_result.predict is not UNSET:
print("\n - Predict data")
if call_result.user_delete is not UNSET:
print("\n - User delete data")
if call_result.compare is not UNSET:
print("\n - Compare data")
if call_result.iso_image is not UNSET:
print("\n - ISO image data")
if call_result.document is not UNSET:
print("\n - Document data")
def sample_validate(session: Session, image_path: str = FACE_IMAGE):
"""Sample: Validate a face image.
Demonstrates face detection, quality check, pose estimation, and anti-spoofing.
"""
try:
# Pick an image and specify its format
image = ImageInputArg(image_path, "rgb")
# Configure thresholds for face detection
config = OperationConfig (
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
# Call validate method return operation ID and result object
op_id, result = session.validate(image, config)
# Display the call result status header
display_returned_status(op_id, result)
# `validate` result contains only a face collection object
display_faces_collection(result.faces)
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_validate_no_face(session: Session):
"""Sample: Validate an image with no face.
Demonstrates validation failure when no face is detected in the image.
This shows how the SDK handles cases where face detection fails.
"""
print("\n" + "="*80)
print("Testing validation with image containing no face")
print("="*80)
try:
# Load image with no face
image = ImageInputArg(NO_FACE, "rgb")
# Configure thresholds for face detection
config = OperationConfig(
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
# Call validate method - expected to fail since no face is present
op_id, result = session.validate(image, config)
# Display the call result status header
# When no face is detected, op_id will be negative (-1)
display_returned_status(op_id, result)
# Display faces collection (should be empty or None)
display_faces_collection(result.faces)
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_enroll_onefa(session: Session, image_path: str = FACE_IMAGE, collection: str = "default") -> Optional[str]:
"""Sample: Enroll a face for 1FA authentication.
Returns the PUID (Private User ID) on success.
"""
try:
image = ImageInputArg(image_path, "rgb")
config = OperationConfig(
collection_name=collection,
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
op_id, result = session.enroll_onefa(image, config)
# Display the call result status header
display_returned_status(op_id, result)
# `enroll_onefa` result contains enrollment data object + faces data object
display_faces_collection(result.faces)
enroll_data = result.enroll
if enroll_data is not UNSET:
print("\n Enrollment Data:")
print(f"\n - Enroll Performed: {enroll_data.enroll_performed}")
print(f"\n - User puid: {enroll_data.api_response.puid}")
print(f"\n - User guid: {enroll_data.api_response.guid}")
print(f"\n - Backend API reesponse status (0 mean success): {enroll_data.api_response.status}")
print(f"\n - Backend API reesponse message: {enroll_data.api_response.message}")
return enroll_data.api_response.puid
else:
print("Enrollment data not available")
return None
except Exception as e:
print(f"Error: {e}")
return None
def sample_face_predict_onefa(session: Session, image_path: str = FACE_IMAGE, collection: str = "default"):
"""Sample: Predict/authenticate using enrolled face.
Matches face against enrolled faces in the collection.
"""
try:
image = ImageInputArg(image_path, "rgb")
config = OperationConfig(
collection_name=collection,
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
op_id, result = session.face_predict_onefa(image, config)
# Display the call result status header
display_returned_status(op_id, result)
# `face_predict_onefa` result contains predict data object + faces data object
display_faces_collection(result.faces)
predict_data = result.predict
if predict_data is not UNSET:
print("\n Predict Data:")
print(f"\n - Predict Performed: {predict_data.predict_performed}")
print(f"\n - User puid: {predict_data.api_response.puid}")
print(f"\n - User guid: {predict_data.api_response.guid}")
print(f"\n - Backend API reesponse status (0 mean success): {predict_data.api_response.status}")
print(f"\n - Backend API reesponse message: {predict_data.api_response.message}")
return predict_data.api_response.puid
else:
print("Predict data not available")
return None
except Exception as e:
print(f"Error: {e}")
return None
def sample_face_compare_files(session: Session,
image_path_1: str = FACE_IMAGE,
image_path_2: str = FACE_IMAGE_2,
collection: str = "default"):
"""Sample: Compare two face images (1:1 comparison).
Demonstrates 1:1 face comparison between two images.
Returns similarity score and match result.
"""
try:
# Load both images
image_a = ImageInputArg(image_path_1, "rgb")
image_b = ImageInputArg(image_path_2, "rgb")
# Configure comparison thresholds
config = OperationConfig(
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
# Call face_compare_files method
op_id, result = session.face_compare_files(image_a, image_b, config)
# Display the call result status header
display_returned_status(op_id, result)
# Display face detection results if available as compare should return the faces to compare
if result.faces is not UNSET:
display_faces_collection(result.faces)
# Display comparison results
if result.compare is not UNSET:
compare_data = result.compare
print("\n Comparison Results:")
print(f"\n - Face Detected in Image A: {compare_data.face_detected_a}")
print(f"\n - Face Detected in Image B: {compare_data.face_detected_b}")
print(f"\n - Is Match: {compare_data.is_match}")
print(f"\n - Similarity Score: {compare_data.similarity_score:.4f}")
print(f"\n - Confidence: {compare_data.confidence:.4f}")
print(f"\n - Distance Max: {compare_data.distance_max:.4f}")
print(f"\n - Distance Mean: {compare_data.distance_mean:.4f}")
print(f"\n - Distance Min: {compare_data.distance_min:.4f}")
print(f"\n - Face Thresholds: {compare_data.face_thresholds}")
else:
print("\n Comparison data not available")
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_estimate_age(session: Session, image_path: str = FACE_IMAGE):
"""Sample: Estimate age from a face image.
Demonstrates age estimation from a face image.
Returns only faces data object with age information.
"""
try:
# Load the image
image = ImageInputArg(image_path, "rgb")
# Configure thresholds for age estimation
config = OperationConfig(
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
# Call estimate_age method
op_id, result = session.estimate_age(image, config)
# Display the call result status header
display_returned_status(op_id, result)
# `estimate_age` result contains only a face collection object with age data
display_faces_collection(result.faces)
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_face_iso(session: Session, image_path: str = ISO_IMAGE):
"""Sample: Process face image according to ISO standards.
Demonstrates ISO-compliant face image generation.
Returns ISO-compliant face image cropped with ISO standards and background replaced with
compliant background color RGB(200, 100, 127).
"""
try:
# Load the image
image = ImageInputArg(image_path, "rgb")
# Configure thresholds for ISO processing
config = OperationConfig(
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
# Call face_iso method - returns operation ID, result, and ISO image bytes and the Face collection
op_id, result, iso_image_bytes = session.face_iso(image, config)
# Save ISO image to output directory
if result.iso_image is not UNSET and result.iso_image.image:
output_dir = ensure_output_dir()
# Get image dimensions from the result metadata
iso_info = result.iso_image.image.info
iso_width = iso_info.width
iso_height = iso_info.height
iso_channels = iso_info.channels
# Reconstruct ISO image from raw RGB bytes
iso_array = np.frombuffer(iso_image_bytes, dtype=np.uint8)
iso_array = iso_array.reshape((iso_height, iso_width, iso_channels))
# Save ISO image to output directory
iso_output_path = output_dir / "face_iso_image.png"
plt.imsave(str(iso_output_path), iso_array)
print(f"\n ISO image saved: {iso_output_path}")
# Display the call result status header
display_returned_status(op_id, result)
# Display face detection results if available
if result.faces is not UNSET:
display_faces_collection(result.faces)
# Display ISO image processing results
if result.iso_image is not UNSET:
iso_data = result.iso_image
print("\n ISO Image Processing Results:")
print(f"\n - Success: {iso_data.success}")
if iso_data.image:
print(f"\n - Image Width: {iso_data.image.info.width}")
print(f"\n - Image Height: {iso_data.image.info.height}")
print(f"\n - Image Channels: {iso_data.image.info.channels}")
print(f"\n - Image Depth: {iso_data.image.info.depth.name}")
print(f"\n - Image Color Format: {iso_data.image.info.color.name}")
print(f"\n - ISO Image Bytes Size: {len(iso_image_bytes)} bytes")
else:
print("\n ISO image data not available")
return result, iso_image_bytes
except Exception as e:
print(f"Error: {e}")
return None, None
def sample_anti_spoofing(session: Session, image_path: str = FACE_IMAGE):
"""Sample: Perform anti-spoofing detection.
Demonstrates liveness detection to identify spoofed faces.
Returns faces data object with anti-spoofing status code.
"""
try:
# Load the image
image = ImageInputArg(image_path, "rgb")
# Configure anti-spoofing thresholds
config = OperationConfig(
anti_spoofing_mode=1, # 1 = XMS mode (default), 0 = Off, 2 = JPD, 3 = Recognito Android
anti_spoofing_threshold=0.9,
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
eyes_blinking_threshold=0.3
)
# Call anti_spoofing method - returns status code only
op_id, result = session.anti_spoofing(image, config)
# Display the call result status header
display_returned_status(op_id, result)
# Display face detection results if available
if result.faces is not UNSET:
display_faces_collection(result.faces)
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_face_detection_strategies(session: Session, image_path: str = CONSIDER_BIG_FACE):
"""Sample: Demonstrate different face detection strategies.
Shows how to use the face_detection_strategy parameter to control
which faces are detected and returned when multiple faces are present.
"""
try:
print("\n" + "="*80)
print("Testing Face Detection Strategies")
print("="*80)
# Load the image (use image with multiple faces for better demonstration)
image = ImageInputArg(image_path, "rgb")
# Strategy 0: Multiple faces - returns all detected faces
print("\n[Strategy 0] Multiple Faces - Return all detected faces")
config = OperationConfig(
face_detection_strategy=0, # Return all detected faces
angle_rotation_left_threshold=20.0,
angle_rotation_right_threshold=20.0,
anti_spoofing_mode=0, # Disable anti-spoofing for faster processing
)
_, result = session.validate(image, config)
if result.faces is not UNSET and result.faces:
print(f" → Detected {len(result.faces)} face(s)")
for i, face in enumerate(result.faces):
if face.geometry:
print(f" Face {i+1}: Confidence = {face.geometry.face_confidence_score:.4f}")
# Strategy 1: Best confidence score (default)
print("\n[Strategy 1] Best Confidence Score - Single face with highest confidence")
config = OperationConfig(
face_detection_strategy=1, # Best confidence score (default)
angle_rotation_left_threshold=20.0,
angle_rotation_right_threshold=20.0,
anti_spoofing_mode=0,
)
_, result = session.validate(image, config)
if result.faces is not UNSET and result.faces:
print(f" → Returned {len(result.faces)} face(s)")
if result.faces[0].geometry:
print(f" Best confidence: {result.faces[0].geometry.face_confidence_score:.4f}")
# Strategy 2: Biggest face
print("\n[Strategy 2] Biggest Face - Single face with largest area")
config = OperationConfig(
face_detection_strategy=2, # Biggest face by area
angle_rotation_left_threshold=20.0,
angle_rotation_right_threshold=20.0,
anti_spoofing_mode=0,
)
_, result = session.validate(image, config)
if result.faces is not UNSET and result.faces:
print(f" → Returned {len(result.faces)} face(s)")
if result.faces[0].geometry:
print(f" Face confidence: {result.faces[0].geometry.face_confidence_score:.4f}")
# Strategy 3: Hybrid - best score of (area × confidence)
print("\n[Strategy 3] Hybrid - Best score of (area × confidence)")
config = OperationConfig(
face_detection_strategy=3, # Hybrid scoring
angle_rotation_left_threshold=20.0,
angle_rotation_right_threshold=20.0,
anti_spoofing_mode=0,
)
_, result = session.validate(image, config)
if result.faces is not UNSET and result.faces:
print(f" → Returned {len(result.faces)} face(s)")
if result.faces[0].geometry:
print(f" Hybrid score confidence: {result.faces[0].geometry.face_confidence_score:.4f}")
print("\n" + "="*80)
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_doc_scan_face(session: Session, image_path: str = COMPARE_DOC):
"""Sample: Scan document and extract face.
Demonstrates document scanning and face extraction from ID documents.
Returns cropped document image and extracted face.
"""
try:
# Load the document image
image = ImageInputArg(image_path, "rgb")
# Configure document scanning thresholds and options
config = OperationConfig(
detect_and_recognize_mrz_code = True,
calculate_age_from_ocr_text = True,
blur_threshold_enroll_pred = 0.0,
conf_score_thr_doc = 0.001,
blur_threshold_doc = 50.0,
threshold_doc_too_close = 0.99,
threshold_doc_too_far = 0.03,
threshold_doc_x = 0.01,
threshold_doc_y = 0.01
)
# Call doc_scan_face method - returns op_id, document bytes, face bytes, and result
op_id, result ,doc_image_bytes, face_image_bytes = session.doc_scan_face(image, config)
# Save both images to output directory
if result.document is not UNSET and result.faces is not UNSET:
output_dir = ensure_output_dir()
# Get image dimensions from the result metadata
doc_info = result.document.cropped_document_image_info.info
doc_width = doc_info.width
doc_height = doc_info.height
doc_channels = doc_info.channels
# Reconstruct document image from raw RGB bytes
doc_array = np.frombuffer(doc_image_bytes, dtype=np.uint8)
doc_array = doc_array.reshape((doc_height, doc_width, doc_channels))
# Get face image dimensions from first face result
face_info = result.faces[0].cropped_image_info.info
face_width = face_info.width
face_height = face_info.height
face_channels = face_info.channels
# Reconstruct face image from raw RGB bytes
face_array = np.frombuffer(face_image_bytes, dtype=np.uint8)
face_array = face_array.reshape((face_height, face_width, face_channels))
# Save both images to output directory
doc_output_path = output_dir / "doc_scan_document.png"
face_output_path = output_dir / "doc_scan_face.png"
plt.imsave(str(doc_output_path), doc_array)
plt.imsave(str(face_output_path), face_array)
print(f"\n Images saved:")
print(f" - Document: {doc_output_path}")
print(f" - Face: {face_output_path}")
else:
print("\n Note: Image saving requires document and face results")
# Display the call result status header
display_returned_status(op_id, result)
# Display face detection results if available
if result.faces is not UNSET:
display_faces_collection(result.faces)
# Display document scanning results
if result.document is not UNSET:
doc_data = result.document.detected_document
print("\n Document Scanning Results:")
print(f"\n - Document Confidence Score: {doc_data.confidence_score:.4f}")
print(f"\n - Document Box Center: ({doc_data.document_box_center.x:.2f}, {doc_data.document_box_center.y:.2f})")
# Display document traits flags
doc_traits = FlagUtil.get_active_flags(DocumentTraits, doc_data.document_traits)
if doc_traits:
print(f"\n - Document Traits:")
for trait in doc_traits:
print(f"\n * {trait.name}")
else:
print(f"\n - Document Traits: No issues detected")
# Display MRZ data if available
if doc_data.mrz_data:
print(f"\n - MRZ Data Lines: {len(doc_data.mrz_data)}")
for i, line in enumerate(doc_data.mrz_data):
print(f"\n Line {i+1}: {line}")
# Display OCR age data if available
if doc_data.ocr_age_data:
print(f"\n - OCR Age: {doc_data.ocr_age_data.age}")
# Display image information
if result.document.cropped_document_image_info:
img_info = result.document.cropped_document_image_info.info
print(f"\n - Cropped Document Image Info:")
print(f"\n * Width: {img_info.width}")
print(f"\n * Height: {img_info.height}")
print(f"\n * Channels: {img_info.channels}")
print(f"\n - Document Image Bytes Size: {len(doc_image_bytes)} bytes")
print(f"\n - Face Image Bytes Size: {len(face_image_bytes)} bytes")
else:
print("\n Document scanning data not available")
return result, doc_image_bytes, face_image_bytes
except Exception as e:
print(f"Error: {e}")
return None, None, None
def sample_user_delete(session: Session, puid: str, collection: str = "default"):
"""Sample: Delete a user by PUID.
Demonstrates user deletion from a collection by PUID.
Args:
puid: User's unique identifier to delete
collection: Collection name where user is enrolled
"""
try:
# Configure collection for user deletion
config = OperationConfig(
collection_name=collection,
angle_rotation_left_threshold=6.0,
angle_rotation_right_threshold=5.0,
anti_spoofing_threshold=0.9,
eyes_blinking_threshold=0.3
)
# Call user_delete method - returns operation ID and result
op_id, result = session.user_delete(puid, config)
# Display the call result status header
display_returned_status(op_id, result)
# Display user deletion results
if result.user_delete is not UNSET:
delete_data = result.user_delete
print("\n User Deletion Results:")
print(f"\n - PUID: {puid}")
print(f"\n - Collection: {collection}")
print(f"\n - Delete Status Code: {delete_data.status}")
# Interpret status code
if delete_data.status == 0:
print(f"\n - Result: User deleted successfully ✓")
else:
print(f"\n - Result: Delete operation failed")
# Display uuid count if available
if delete_data.uuid_count is not None:
print(f"\n - UUID Count: {delete_data.uuid_count}")
# Display message if available
if delete_data.message:
print(f"\n - Message: {delete_data.message}")
else:
print("\n User deletion data not available")
return result
except Exception as e:
print(f"Error: {e}")
return None
def sample_enroll_predict_delete_workflow(session: Session, collection: str = "default"):
"""Sample: Complete workflow - enroll, predict, and delete.
Demonstrates a typical authentication workflow with face enrollment,
prediction/authentication, and user deletion.
Args:
session: Active Session instance
collection: Collection name for enrollment
Returns:
bool: True if workflow completed successfully, False otherwise
"""
try:
print("\n" + "=" * 70)
print(f"WORKFLOW: Enroll -> Predict -> Delete")
print(f"Collection: {collection}")
print("=" * 70)
# Step 1: Enroll a user
print("\n[Step 1/3] Enrolling user...")
puid = sample_enroll_onefa(session, FACE_IMAGE, collection)
if not puid:
print("\n✗ Workflow failed: Enrollment unsuccessful")
return False
print(f"\n✓ Enrollment successful - PUID: {puid}")
# Step 2: Predict/authenticate the user
print("\n[Step 2/3] Authenticating user...")
predicted_puid = sample_face_predict_onefa(session, FACE_IMAGE, collection)
if not predicted_puid:
print("\n✗ Workflow failed: Prediction unsuccessful")
# Clean up enrolled user even if prediction failed
print("\nCleaning up enrolled user...")
sample_user_delete(session, puid, collection)
return False
# Verify PUID match
if puid != predicted_puid:
print(f"\n✗ Workflow warning: PUID mismatch")
print(f" - Enrolled PUID: {puid}")
print(f" - Predicted PUID: {predicted_puid}")
else:
print(f"\n✓ Authentication successful - PUID matched: {puid}")
# Step 3: Delete the user
print("\n[Step 3/3] Deleting user...")
result = sample_user_delete(session, puid, collection)
if result and result.user_delete is not UNSET and result.user_delete.status == 0:
print("\n✓ User deleted successfully")
print("\n" + "=" * 70)
print("WORKFLOW COMPLETED SUCCESSFULLY")
print("=" * 70)
return True
else:
print("\n✗ Workflow warning: Delete operation may have failed")
return False
except Exception as e:
print(f"\n✗ Workflow failed with error: {e}")
return False
def display_menu():
"""Display the sample selection menu."""
print("\n" + "=" * 70)
print("CRYPTONETS PYTHON SDK - SAMPLE SELECTOR")
print("=" * 70)
print("\nAvailable Samples:")
print("\n 1. Validate - Face detection, quality check, and pose estimation")
print(" 2. Estimate Age - Estimate age from face image")
print(" 3. Face ISO - Generate ISO-compliant face image")
print(" 4. Anti-Spoofing - Detect liveness and spoofing")
print(" 5. Face Detection Strategies - Test different face selection strategies")
print(" 6. Face Compare - Compare two face images (1:1)")
print(" 7. Document Scan - Scan document and extract face")
print(" 8. Enroll (1FA) - Enroll a face for authentication")
print(" 9. Predict (1FA) - Authenticate using enrolled face")
print(" 10. User Delete - Delete enrolled user by PUID")
print(" 11. Complete Workflow - Enroll, Predict, and Delete")
print(" 0. Run All Samples")
print("\n Q. Quit")
print("\n" + "=" * 70)
def run_selected_sample(session: Session, choice: str) -> bool:
"""Run the selected sample.
Args:
session: Active Session instance
choice: User's menu choice
Returns:
bool: True to continue, False to quit
"""
if choice == '1':
sample_validate(session)
elif choice == '2':
sample_estimate_age(session)
elif choice == '3':
sample_face_iso(session)
elif choice == '4':
sample_anti_spoofing(session)
elif choice == '5':
sample_face_detection_strategies(session)
elif choice == '6':
sample_face_compare_files(session)
elif choice == '7':
sample_doc_scan_face(session)
elif choice == '8':
collection = input("\nEnter collection name (default: 'default'): ").strip() or "default"
sample_enroll_onefa(session, FACE_IMAGE, collection)
elif choice == '9':
collection = input("\nEnter collection name (default: 'default'): ").strip() or "default"
sample_face_predict_onefa(session, FACE_IMAGE, collection)
elif choice == '10':
puid = input("\nEnter PUID to delete: ").strip()
if puid:
collection = input("Enter collection name (default: 'default'): ").strip() or "default"
sample_user_delete(session, puid, collection)
else:
print("Error: PUID is required")
elif choice == '11':
collection = input("\nEnter collection name (default: 'default'): ").strip() or "default"
sample_enroll_predict_delete_workflow(session, collection)
elif choice == '0':
print("\n" + "=" * 70)
print("RUNNING ALL SAMPLES")
print("=" * 70)
sample_validate(session)
sample_estimate_age(session)
sample_face_iso(session)
sample_anti_spoofing(session)
sample_face_detection_strategies(session)
sample_face_compare_files(session)
sample_doc_scan_face(session)
print("\n" + "=" * 70)
print("TESTING WORKFLOWS FOR ALL COLLECTIONS")
print("=" * 70)
collections = ["default", "RES100", "RES200"]
for collection in collections:
sample_enroll_predict_delete_workflow(session, collection)
print("\n" + "=" * 70)
print("ALL SAMPLES COMPLETED")
print("=" * 70)
elif choice.upper() == 'Q':
print("\nExiting...")
return False
else:
print("\nInvalid choice. Please try again.")
return True
def main():
"""Main function with interactive sample selection."""
# Check if running in non-interactive mode
non_interactive =True # len(sys.argv) > 1 and sys.argv[1] == '--all'
try:
# Initialize the library
print("\n" + "=" * 70)
print("INITIALIZING CRYPTONETS SDK")
print("=" * 70)
print("\nInitializing PrivIDFaceLib...")
PrivIDFaceLib.initialize()
print(f"✓ Native Library version: {PrivIDFaceLib.get_native_sdk_version()}")
# Create session
print("\nCreating session...")
settings = create_session_settings(API_KEY, BASE_URL)
session = Session(settings)
print("✓ Session created successfully!")
if non_interactive:
# Run all samples automatically
print("\n" + "=" * 70)
print("RUNNING ALL SAMPLES (NON-INTERACTIVE MODE)")
print("=" * 70)
run_selected_sample(session, '0') # Choice '0' runs all samples
PrivIDFaceLib.shutdown()
return 0
else:
# Interactive menu loop
while True:
display_menu()
choice = input("\nEnter your choice: ").strip()
if not run_selected_sample(session, choice):
break
# Prompt to continue
if choice.upper() != 'Q':
input("\nPress Enter to continue...")