File size: 2,885 Bytes
559e08d
 
 
 
 
 
 
 
 
 
 
2dd3f29
 
 
 
 
d111573
2dd3f29
fbb53c3
 
861c882
559e08d
fbb53c3
 
 
 
 
 
559e08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
861c882
559e08d
 
 
 
861c882
559e08d
 
 
 
 
861c882
559e08d
 
fbb53c3
559e08d
 
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
import subprocess

# Install the required packages
subprocess.check_call(["pip", "install", "--upgrade", "pip"])
subprocess.check_call(["pip", "install", "-U", "transformers"])
subprocess.check_call(["pip", "install", "-U", "accelerate"])
subprocess.check_call(["pip", "install", "datasets"])
subprocess.check_call(["pip", "install", "evaluate"])
subprocess.check_call(["pip", "install", "scikit-learn"])
subprocess.check_call(["pip", "install", "torchvision"])

# Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("rararara9999/Model")
model = AutoModelForSequenceClassification.from_pretrained("rararara9999/Model")
from transformers import AutoModelForImageClassification, AutoImageProcessor

import torch
import numpy as np
from PIL import Image
import streamlit as st

# Load the fine-tuned model and image processor
model_checkpoint = "rararara9999/Model"
model = AutoModelForImageClassification.from_pretrained(model_checkpoint, num_labels=2)
image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)

# Standalone Test Script
def test_model(image_path):
    # Load and preprocess the image
    image = Image.open(image_path)
    inputs = image_processor(images=image, return_tensors="pt")

    # Get model predictions
    outputs = model(**inputs)
    predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
    predictions = predictions.cpu().detach().numpy()

    # Get the index of the largest output value
    max_index = np.argmax(predictions)
    labels = ["Wearing Mask", "Not Wearing Mask"]
    predicted_label = labels[max_index]

    print(f"The predicted label is {predicted_label}")

# Streamlit App for Interactive Testing
def main():
    st.title("Face Mask Detection with HuggingFace Spaces")
    st.write("Upload an image to analyze whether the person is wearing a mask:")

    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image.', use_column_width=True)
        st.write("")
        st.write("Classifying...")

        # Preprocess the image
        inputs = image_processor(images=image, return_tensors="pt")

        # Get model predictions
        outputs = model(**inputs)
        predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
        predictions = predictions.cpu().detach().numpy()

        # Get the index of the largest output value
        max_index = np.argmax(predictions)
        labels = ["Wearing Mask", "Not Wearing Mask"]
        predicted_label = labels[max_index]
        confidence = predictions[max_index]

        st.write(f"Predicted Label: {predicted_label}")
        st.write(f"Confidence: {confidence:.2f}")

if __name__ == "__main__":
    main()