rararara9999 commited on
Commit
fbb53c3
·
verified ·
1 Parent(s): 861c882

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -1,13 +1,45 @@
1
- # Load model directly
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
 
4
- tokenizer = AutoTokenizer.from_pretrained("rararara9999/Model")
5
- model = AutoModelForSequenceClassification.from_pretrained("rararara9999/Model")
 
 
 
 
 
 
6
 
7
- import streamlit as st
8
- from transformers import pipeline
 
9
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
 
 
 
 
 
 
 
 
11
  def main():
12
  st.title("Face Mask Detection with HuggingFace Spaces")
13
  st.write("Upload an image to analyze whether the person is wearing a mask:")
@@ -19,10 +51,6 @@ def main():
19
  st.write("")
20
  st.write("Classifying...")
21
 
22
- # Load the fine-tuned model and image processor
23
- model_checkpoint = "rararara9999/Model"
24
- image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
25
-
26
  # Preprocess the image
27
  inputs = image_processor(images=image, return_tensors="pt")
28
 
@@ -42,3 +70,4 @@ def main():
42
 
43
  if __name__ == "__main__":
44
  main()
 
 
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
+ image_path = "C:\Users\crc96\Desktop\HKUST\testing_picture"
25
+ def test_model(image_path):
26
+ # Load and preprocess the image
27
+ image = Image.open(image_path)
28
+ inputs = image_processor(images=image, return_tensors="pt")
29
+
30
+ # Get model predictions
31
+ outputs = model(**inputs)
32
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
33
+ predictions = predictions.cpu().detach().numpy()
34
 
35
+ # Get the index of the largest output value
36
+ max_index = np.argmax(predictions)
37
+ labels = ["Wearing Mask", "Not Wearing Mask"]
38
+ predicted_label = labels[max_index]
39
+
40
+ print(f"The predicted label is {predicted_label}")
41
+
42
+ # Streamlit App for Interactive Testing
43
  def main():
44
  st.title("Face Mask Detection with HuggingFace Spaces")
45
  st.write("Upload an image to analyze whether the person is wearing a mask:")
 
51
  st.write("")
52
  st.write("Classifying...")
53
 
 
 
 
 
54
  # Preprocess the image
55
  inputs = image_processor(images=image, return_tensors="pt")
56
 
 
70
 
71
  if __name__ == "__main__":
72
  main()
73
+