Spaces:
Sleeping
Sleeping
Commit
·
b47f30d
1
Parent(s):
81220ff
Update app06.py
Browse files
app06.py
CHANGED
|
@@ -1,38 +1,38 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
-
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
import torch
|
| 7 |
-
|
| 8 |
-
# Load the model and processor
|
| 9 |
-
model_dir = "defectdetection/model
|
| 10 |
-
model = SegformerForSemanticSegmentation.from_pretrained(model_dir)
|
| 11 |
-
processor = SegformerImageProcessor.from_pretrained(model_dir)
|
| 12 |
-
model.eval()
|
| 13 |
-
|
| 14 |
-
st.title("PCB Defect Detection")
|
| 15 |
-
|
| 16 |
-
# Upload image in Streamlit
|
| 17 |
-
uploaded_file = st.file_uploader("Upload a PCB image", type=["jpg", "png"])
|
| 18 |
-
|
| 19 |
-
if uploaded_file:
|
| 20 |
-
# Preprocess the image
|
| 21 |
-
test_image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
-
inputs = processor(images=test_image, return_tensors="pt")
|
| 23 |
-
|
| 24 |
-
# Model inference
|
| 25 |
-
with torch.no_grad():
|
| 26 |
-
outputs = model(**inputs)
|
| 27 |
-
|
| 28 |
-
# Post-process
|
| 29 |
-
semantic_map = processor.post_process_semantic_segmentation(outputs, target_sizes=[test_image.size[::-1]])[0]
|
| 30 |
-
semantic_map = np.uint8(semantic_map)
|
| 31 |
-
semantic_map[semantic_map==1] = 255
|
| 32 |
-
semantic_map[semantic_map==2] = 195
|
| 33 |
-
semantic_map[semantic_map==3] = 135
|
| 34 |
-
semantic_map[semantic_map==4] = 75
|
| 35 |
-
|
| 36 |
-
# Display the results
|
| 37 |
-
st.image(test_image, caption="Uploaded Image", use_column_width=True)
|
| 38 |
-
st.image(semantic_map, caption="Predicted Defects", use_column_width=True, channels="GRAY")
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Load the model and processor
|
| 9 |
+
model_dir = "defectdetection/model"
|
| 10 |
+
model = SegformerForSemanticSegmentation.from_pretrained(model_dir)
|
| 11 |
+
processor = SegformerImageProcessor.from_pretrained(model_dir)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
st.title("PCB Defect Detection")
|
| 15 |
+
|
| 16 |
+
# Upload image in Streamlit
|
| 17 |
+
uploaded_file = st.file_uploader("Upload a PCB image", type=["jpg", "png"])
|
| 18 |
+
|
| 19 |
+
if uploaded_file:
|
| 20 |
+
# Preprocess the image
|
| 21 |
+
test_image = Image.open(uploaded_file).convert("RGB")
|
| 22 |
+
inputs = processor(images=test_image, return_tensors="pt")
|
| 23 |
+
|
| 24 |
+
# Model inference
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model(**inputs)
|
| 27 |
+
|
| 28 |
+
# Post-process
|
| 29 |
+
semantic_map = processor.post_process_semantic_segmentation(outputs, target_sizes=[test_image.size[::-1]])[0]
|
| 30 |
+
semantic_map = np.uint8(semantic_map)
|
| 31 |
+
semantic_map[semantic_map==1] = 255
|
| 32 |
+
semantic_map[semantic_map==2] = 195
|
| 33 |
+
semantic_map[semantic_map==3] = 135
|
| 34 |
+
semantic_map[semantic_map==4] = 75
|
| 35 |
+
|
| 36 |
+
# Display the results
|
| 37 |
+
st.image(test_image, caption="Uploaded Image", use_column_width=True)
|
| 38 |
+
st.image(semantic_map, caption="Predicted Defects", use_column_width=True, channels="GRAY")
|