cvdetectors commited on
Commit
f8aa3d1
·
verified ·
1 Parent(s): 6e089b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py CHANGED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
4
+ from PIL import Image
5
+ import torch
6
+
7
+ # Load processor and model
8
+ processor = AutoImageProcessor.from_pretrained("nguyenkhoa/dinov2_Liveness_detection_v2.2.3")
9
+ model = AutoModelForImageClassification.from_pretrained("nguyenkhoa/dinov2_Liveness_detection_v2.2.3")
10
+
11
+ # Define labels
12
+ id2label = model.config.id2label
13
+
14
+ # Inference function
15
+ def detect_liveness(image: Image.Image):
16
+ # Preprocess image
17
+ inputs = processor(images=image, return_tensors="pt")
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+
21
+ logits = outputs.logits
22
+ probs = torch.nn.functional.softmax(logits, dim=-1)[0]
23
+
24
+ # Get prediction
25
+ predicted_class_idx = torch.argmax(probs).item()
26
+ predicted_label = id2label[predicted_class_idx]
27
+ confidence = round(probs[predicted_class_idx].item(), 4)
28
+
29
+ return f"Liveness: {predicted_label} (Confidence: {confidence})"
30
+
31
+ # Launch Gradio app
32
+ app = gr.Interface(
33
+ fn=detect_liveness,
34
+ inputs=gr.Image(type="pil", label="Upload Face Image"),
35
+ outputs=gr.Text(label="Liveness Detection Result"),
36
+ title="Liveness Detection App",
37
+ description="Upload a face image to check if it's live or spoofed using DinoV2 model."
38
+ )
39
+
40
+ if __name__ == "__main__":
41
+ app.launch()