Spaces:
Build error
Build error
Commit ·
2231a45
1
Parent(s): db02942
Modify README.md, app.py
Browse files
README.md
CHANGED
|
@@ -10,4 +10,48 @@ pinned: false
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Simple ResNet-50 Classifier 🏆
|
| 14 |
+
|
| 15 |
+
A Streamlit web application that uses the pre-trained ResNet-50 model for image classification.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- Image upload through drag-and-drop or file browser
|
| 20 |
+
- Real-time image classification
|
| 21 |
+
- Top 5 predictions with confidence scores
|
| 22 |
+
- Support for JPG, JPEG, and PNG formats
|
| 23 |
+
- User-friendly interface
|
| 24 |
+
- Built on PyTorch and Streamlit
|
| 25 |
+
|
| 26 |
+
## Technical Details
|
| 27 |
+
|
| 28 |
+
- **Framework**: Streamlit v1.40.0
|
| 29 |
+
- **Model**: Pre-trained ResNet-50
|
| 30 |
+
- **Image Processing**: PIL and torchvision
|
| 31 |
+
- **Classification**: 1000 ImageNet classes
|
| 32 |
+
|
| 33 |
+
## Requirements
|
| 34 |
+
|
| 35 |
+
The project requires the following dependencies:
|
| 36 |
+
```sh
|
| 37 |
+
streamlit==1.24.0
|
| 38 |
+
torch==2.0.1
|
| 39 |
+
torchvision==0.15.2
|
| 40 |
+
Pillow==9.5.0
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Usage
|
| 44 |
+
|
| 45 |
+
1. Clone the repository
|
| 46 |
+
2. Install dependencies:
|
| 47 |
+
```sh
|
| 48 |
+
pip install -r requirements.txt
|
| 49 |
+
```
|
| 50 |
+
3. Run the app:
|
| 51 |
+
```sh
|
| 52 |
+
streamlit run app.py
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
## License
|
| 56 |
+
|
| 57 |
+
This project is licensed under the MIT License.
|
app.py
CHANGED
|
@@ -22,7 +22,7 @@ def process_image(image):
|
|
| 22 |
])
|
| 23 |
return transform(image).unsqueeze(0)
|
| 24 |
|
| 25 |
-
def
|
| 26 |
input_tensor = process_image(image)
|
| 27 |
with torch.no_grad():
|
| 28 |
output = model(input_tensor)
|
|
@@ -31,7 +31,7 @@ def get_prediction(image):
|
|
| 31 |
|
| 32 |
def handle_image(image):
|
| 33 |
st.image(image, caption='Processed Image', use_container_width=True)
|
| 34 |
-
probabilities =
|
| 35 |
top5_prob, top5_idx = torch.topk(probabilities, 5)
|
| 36 |
|
| 37 |
st.write("Top 5 Predictions:")
|
|
@@ -44,27 +44,10 @@ with open('imagenet_classes.json', 'r') as f:
|
|
| 44 |
|
| 45 |
# Streamlit UI
|
| 46 |
st.title("Image Classification with ResNet50")
|
| 47 |
-
st.write("Upload an image or
|
| 48 |
-
|
| 49 |
-
# Add clipboard paste functionality
|
| 50 |
-
clipboard_container = st.container()
|
| 51 |
-
with clipboard_container:
|
| 52 |
-
st.write("Click here and press Ctrl+V/Cmd+V to paste an image from clipboard")
|
| 53 |
-
|
| 54 |
-
# Handle clipboard paste
|
| 55 |
-
clipboard_data = st.text_area("", "", key="clipboard", label_visibility="collapsed")
|
| 56 |
-
if clipboard_data and clipboard_data.startswith(('data:image', 'iVBORw0KGgo')):
|
| 57 |
-
try:
|
| 58 |
-
# Handle base64 encoded image data
|
| 59 |
-
import base64
|
| 60 |
-
image_data = base64.b64decode(clipboard_data.split(',')[1] if ',' in clipboard_data else clipboard_data)
|
| 61 |
-
image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
| 62 |
-
handle_image(image)
|
| 63 |
-
except Exception as e:
|
| 64 |
-
st.error(f"Error processing pasted image: {str(e)}")
|
| 65 |
|
| 66 |
# File uploader
|
| 67 |
-
uploaded_file = st.file_uploader("
|
| 68 |
if uploaded_file is not None:
|
| 69 |
image = Image.open(uploaded_file).convert('RGB')
|
| 70 |
handle_image(image)
|
|
|
|
| 22 |
])
|
| 23 |
return transform(image).unsqueeze(0)
|
| 24 |
|
| 25 |
+
def predict_image(image):
|
| 26 |
input_tensor = process_image(image)
|
| 27 |
with torch.no_grad():
|
| 28 |
output = model(input_tensor)
|
|
|
|
| 31 |
|
| 32 |
def handle_image(image):
|
| 33 |
st.image(image, caption='Processed Image', use_container_width=True)
|
| 34 |
+
probabilities = predict_image(image)
|
| 35 |
top5_prob, top5_idx = torch.topk(probabilities, 5)
|
| 36 |
|
| 37 |
st.write("Top 5 Predictions:")
|
|
|
|
| 44 |
|
| 45 |
# Streamlit UI
|
| 46 |
st.title("Image Classification with ResNet50")
|
| 47 |
+
st.write("Upload an image by dragging and dropping, browsing, or pasting from clipboard (Ctrl+V/Cmd+V).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# File uploader
|
| 50 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
| 51 |
if uploaded_file is not None:
|
| 52 |
image = Image.open(uploaded_file).convert('RGB')
|
| 53 |
handle_image(image)
|