File size: 2,717 Bytes
5c5e547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
from flask import Flask, render_template, request, jsonify
import torch
import torchvision.transforms as transforms
from PIL import Image
import torch.nn.functional as F
import torch.nn as nn

num_classes = 10

# Class definition for the model (same as in your code)
class FingerprintRecognitionModel(nn.Module):
    def __init__(self, num_classes):
        super(FingerprintRecognitionModel, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
        self.fc1 = nn.Linear(128 * 28 * 28, 256)
        self.fc2 = nn.Linear(256, num_classes)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = x.view(-1, 128 * 28 * 28)
        x = F.relu(self.fc1(x))
        x = F.softmax(self.fc2(x), dim=1)
        return x

app = Flask(__name__)

# Load the model
model_path = 'fingerprint_recognition_model_bs32_lr0.001_opt_Adam.pt'
model = FingerprintRecognitionModel(num_classes)
model.load_state_dict(torch.load(model_path))
model.eval()

def preprocess_image(image_bytes):
    # Convert bytes to PIL Image
    image = Image.open(io.BytesIO(image_bytes)).convert('L')  # Convert to grayscale

    # Resize to 224x224
    img_resized = image.resize((224, 224))

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5,), (0.5,))
    ])

    # Apply transforms and add batch dimension
    img_tensor = transform(img_resized).unsqueeze(0)

    return img_tensor

def predict_class(image_bytes):
    img_tensor = preprocess_image(image_bytes)
    with torch.no_grad():
        outputs = model(img_tensor)
        _, predicted = torch.max(outputs.data, 1)
        predicted_class = int(predicted.item())
    return predicted_class

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file = request.files['file']
        if file:
            contents = file.read()
            predicted_class = predict_class(contents)
            class_labels = {0:'left_index_fingers',1:'left_little_fingers',2:'left_middle_fingers',3: 'left_ring_fingers', 4:'left_thumb_fingers',5:'right_index_fingers',6:'right_little_fingers',7:'right_middle_fingers',8:'right_ring_fingers',9: 'right_thumb_fingers'}
            return jsonify({'predicted_class': predicted_class, 'class_label': class_labels[predicted_class]})
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)