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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -59
app.py CHANGED
@@ -1,73 +1,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
- 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:")
46
-
47
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
48
- if uploaded_file is not None:
49
- image = Image.open(uploaded_file)
50
- st.image(image, caption='Uploaded Image.', use_column_width=True)
51
- st.write("")
52
- st.write("Classifying...")
53
-
54
- # Preprocess the image
55
- inputs = image_processor(images=image, return_tensors="pt")
56
-
57
- # Get model predictions
58
- outputs = model(**inputs)
59
- predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
60
- predictions = predictions.cpu().detach().numpy()
61
 
62
- # Get the index of the largest output value
63
- max_index = np.argmax(predictions)
64
- labels = ["Wearing Mask", "Not Wearing Mask"]
65
- predicted_label = labels[max_index]
66
- confidence = predictions[max_index]
67
 
68
- st.write(f"Predicted Label: {predicted_label}")
69
- st.write(f"Confidence: {confidence:.2f}")
 
 
70
 
71
- if __name__ == "__main__":
72
- main()
73
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Load and preprocess the image
12
+ image_path = r"C:\Users\crc96\Desktop\HKUST\testing_picture" # Using raw string
13
+ # or
14
+ # image_path = "C:/Users/crc96/Desktop/HKUST/testing_picture" # Using forward slashes
15
+ image = Image.open(image_path)
16
+ inputs = image_processor(images=image, return_tensors="pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ print(f"The predicted label is {predicted_label}")
 
29