File size: 2,931 Bytes
a379098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Hướng dẫn sử dụng DeepPrint ONNX Model

File ONNX model này chỉ chứa **DeepPrint model** (không có LoFTR). LoFTR là bước tiền xử lý riêng biệt.

## File ONNX Model

- **Đường dẫn**: `onnx_models/deepprint_fvc_db1_loftr_final.onnx`
- **Input**: `input_image` - shape `[batch_size, 1, 299, 299]` (grayscale image)
- **Outputs**: 
  - `texture_embeddings`: shape `[batch_size, 256]`
  - `minutia_embeddings`: shape `[batch_size, 256]`

## Cài đặt

```bash
pip install onnxruntime
```

## Cách sử dụng

### 1. Sử dụng script có sẵn

```bash
# Extract embedding từ 1 ảnh
python use_onnx_model.py --image1 path/to/fingerprint.tif

# So sánh 2 ảnh fingerprint
python use_onnx_model.py --image1 path/to/image1.tif --image2 path/to/image2.tif --threshold 0.5
```

### 2. Sử dụng trong code Python

```python
import numpy as np
import onnxruntime as ort
from PIL import Image
import torchvision.transforms.functional as VTF
import sys

# Add DeepPrint path
sys.path.append("deepprint/fixed-length-fingerprint-extractors")
from flx.image_processing.binarization import LazilyAllocatedBinarizer
from flx.data.image_helpers import pad_and_resize_to_deepprint_input_size

# Load ONNX model
onnx_model_path = "onnx_models/deepprint_fvc_db1_loftr_final.onnx"
session = ort.InferenceSession(onnx_model_path)

# Preprocess image
def preprocess_image(image_path):
    img = Image.open(image_path).convert('L')
    img_tensor = VTF.to_tensor(img)
    
    # Binarize fingerprint
    binarizer = LazilyAllocatedBinarizer(5.0)
    img_tensor = binarizer(img_tensor)
    
    # Pad to square and resize to 299x299
    img_tensor = pad_and_resize_to_deepprint_input_size(img_tensor)
    img_tensor = img_tensor.unsqueeze(0)  # Add batch dimension
    
    return img_tensor.numpy().astype(np.float32)

# Extract embeddings
input_tensor = preprocess_image("path/to/fingerprint.tif")
outputs = session.run(['texture_embeddings', 'minutia_embeddings'], 
                      {'input_image': input_tensor})

texture_emb = outputs[0][0]  # [256]
minutia_emb = outputs[1][0]  # [256]
combined_emb = np.concatenate([texture_emb, minutia_emb])  # [512]

# Tính similarity giữa 2 embeddings
def cosine_similarity(emb1, emb2):
    emb1_norm = emb1 / (np.linalg.norm(emb1) + 1e-8)
    emb2_norm = emb2 / (np.linalg.norm(emb2) + 1e-8)
    return np.dot(emb1_norm, emb2_norm)

similarity = cosine_similarity(combined_emb1, combined_emb2)
print(f"Similarity: {similarity:.6f}")
```

## Preprocessing Pipeline

Model yêu cầu preprocessing theo thứ tự sau:

1. **Load image**: PIL Image (grayscale)
2. **Convert to tensor**: `VTF.to_tensor()` → shape `[1, H, W]`
3. **Binarize**: `LazilyAllocatedBinarizer(5.0)` → enhance fingerprint ridges
4. **Pad & Resize**: `pad_and_resize_to_deepprint_input_size()``[1, 299, 299]`
5. **Add batch dimension**: `unsqueeze(0)``[1, 1, 299, 299]`