Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- sample.zip +3 -0
- weed_streamlit.py +72 -0
sample.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5d1e0b40866bcddf08ae2adc974d050e970f1c89856cdd5ccef84b93823b643c
|
3 |
+
size 3094968
|
weed_streamlit.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PIL
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import AutoImageProcessor
|
5 |
+
from transformers import AutoModelForImageClassification
|
6 |
+
import torch
|
7 |
+
|
8 |
+
# Replace the relative path to your weight file
|
9 |
+
model_path = 'weights/yolov8n.pt'
|
10 |
+
|
11 |
+
model_dict={"vit":"uisikdag/weed_vit_balanced",
|
12 |
+
"deit":"uisikdag/weed_deit_balanced",
|
13 |
+
"convnext":"uisikdag/weeds_convnext_balanced",
|
14 |
+
"resnet":"uisikdag/weed_resnet_balanced",
|
15 |
+
"beit":"uisikdag/weed_beit_balanced"}
|
16 |
+
|
17 |
+
|
18 |
+
# Setting page layout
|
19 |
+
st.set_page_config(
|
20 |
+
page_title="Weed Classification", # Setting page title
|
21 |
+
page_icon="🤖", # Setting page icon
|
22 |
+
layout="wide", # Setting layout to wide
|
23 |
+
initial_sidebar_state="expanded" # Expanding sidebar by default
|
24 |
+
)
|
25 |
+
|
26 |
+
# Creating sidebar
|
27 |
+
with st.sidebar:
|
28 |
+
st.header("Settings") # Adding header to sidebar
|
29 |
+
|
30 |
+
model_idx=st.selectbox("Select Base Classifier",{'vit','deit','convnext','resnet','beit'})
|
31 |
+
model=model_dict[model_idx]
|
32 |
+
|
33 |
+
# Adding file uploader to sidebar for selecting images
|
34 |
+
source_img = st.file_uploader(
|
35 |
+
"Choose an image...", type=("jpg", "jpeg", "png", 'bmp', 'webp'))
|
36 |
+
|
37 |
+
with open('sample.zip', 'rb') as f:
|
38 |
+
st.download_button('Sample Images', f, file_name='images.zip')
|
39 |
+
|
40 |
+
# Creating main page heading
|
41 |
+
st.title("Weed Classification with \N{hugging face} Transformers")
|
42 |
+
|
43 |
+
# Creating two columns on the main page
|
44 |
+
col1, col2 = st.columns(2)
|
45 |
+
|
46 |
+
# Adding image to the first column if image is uploaded
|
47 |
+
with col1:
|
48 |
+
if source_img:
|
49 |
+
# Opening the uploaded image
|
50 |
+
uploaded_image = PIL.Image.open(source_img)
|
51 |
+
# Adding the uploaded image to the page with a caption
|
52 |
+
st.image(source_img,
|
53 |
+
caption="Uploaded Image",
|
54 |
+
use_column_width=True
|
55 |
+
)
|
56 |
+
else:
|
57 |
+
uploaded_image=None
|
58 |
+
st.write('Please upload an image')
|
59 |
+
|
60 |
+
with col2:
|
61 |
+
if st.sidebar.button('Classify'):
|
62 |
+
if uploaded_image is not None:
|
63 |
+
image_processor = AutoImageProcessor.from_pretrained(model)
|
64 |
+
inputs = image_processor(uploaded_image, return_tensors="pt")
|
65 |
+
model = AutoModelForImageClassification.from_pretrained(model)
|
66 |
+
with torch.no_grad():
|
67 |
+
logits = model(**inputs).logits
|
68 |
+
predicted_label = logits.argmax(-1).item()
|
69 |
+
out=model.config.id2label[predicted_label]
|
70 |
+
out='The predicted class for the image is: '+out
|
71 |
+
st.text(out)
|
72 |
+
|