Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
# Load the fine-tuned model and image processor
|
7 |
model_checkpoint = "rararara9999/Model"
|
8 |
model = AutoModelForImageClassification.from_pretrained(model_checkpoint, num_labels=2)
|
9 |
image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
|
10 |
|
11 |
-
#
|
12 |
-
image_path
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
# Get model predictions
|
19 |
-
outputs = model(**inputs)
|
20 |
-
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
21 |
-
predictions = predictions.cpu().detach().numpy()
|
22 |
|
23 |
-
# Get the index of the largest output value
|
24 |
-
max_index = np.argmax(predictions)
|
25 |
-
labels = ["Wearing Mask", "Not Wearing Mask"]
|
26 |
-
predicted_label = labels[max_index]
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
# Install the required packages
|
4 |
+
subprocess.check_call(["pip", "install", "--upgrade", "pip"])
|
5 |
+
subprocess.check_call(["pip", "install", "-U", "transformers"])
|
6 |
+
subprocess.check_call(["pip", "install", "-U", "accelerate"])
|
7 |
+
subprocess.check_call(["pip", "install", "datasets"])
|
8 |
+
subprocess.check_call(["pip", "install", "evaluate"])
|
9 |
+
subprocess.check_call(["pip", "install", "scikit-learn"])
|
10 |
+
subprocess.check_call(["pip", "install", "torchvision"])
|
11 |
+
|
12 |
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
13 |
import torch
|
14 |
import numpy as np
|
15 |
from PIL import Image
|
16 |
+
import streamlit as st
|
17 |
|
18 |
# Load the fine-tuned model and image processor
|
19 |
model_checkpoint = "rararara9999/Model"
|
20 |
model = AutoModelForImageClassification.from_pretrained(model_checkpoint, num_labels=2)
|
21 |
image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
|
22 |
|
23 |
+
# Standalone Test Script
|
24 |
+
def test_model(image_path):
|
25 |
+
# Load and preprocess the image
|
26 |
+
image = Image.open(image_path)
|
27 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
28 |
+
|
29 |
+
# Get model predictions
|
30 |
+
outputs = model(**inputs)
|
31 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
32 |
+
predictions = predictions.cpu().detach().numpy()
|
33 |
+
|
34 |
+
# Get the index of the largest output value
|
35 |
+
max_index = np.argmax(predictions)
|
36 |
+
labels = ["Wearing Mask", "Not Wearing Mask"]
|
37 |
+
predicted_label = labels[max_index]
|
38 |
+
|
39 |
+
print(f"The predicted label is {predicted_label}")
|
40 |
+
|
41 |
+
# Streamlit App for Interactive Testing
|
42 |
+
def main():
|
43 |
+
st.title("Face Mask Detection with HuggingFace Spaces")
|
44 |
+
st.write("Upload an image to analyze whether the person is wearing a mask:")
|
45 |
+
|
46 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
47 |
+
if uploaded_file is not None:
|
48 |
+
image = Image.open(uploaded_file)
|
49 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
50 |
+
st.write("")
|
51 |
+
st.write("Classifying...")
|
52 |
+
|
53 |
+
# Preprocess the image
|
54 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
55 |
|
56 |
+
# Get model predictions
|
57 |
+
outputs = model(**inputs)
|
58 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
59 |
+
predictions = predictions.cpu().detach().numpy()
|
60 |
|
61 |
+
# Get the index of the largest output value
|
62 |
+
max_index = np.argmax(predictions)
|
63 |
+
labels = ["Wearing Mask", "Not Wearing Mask"]
|
64 |
+
predicted_label = labels[max_index]
|
65 |
+
confidence = predictions[max_index]
|
66 |
|
67 |
+
st.write(f"Predicted Label: {predicted_label}")
|
68 |
+
st.write(f"Confidence: {confidence:.2f}")
|
69 |
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|