Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- Dockerfile +10 -0
- README (1).md +10 -0
- app.py +52 -0
- banana_classification.h5 +3 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12.2-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
|
README (1).md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Banana Classification
|
| 3 |
+
emoji: 🔥
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from flask_cors import CORS
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
# Load the trained model
|
| 11 |
+
model = tf.keras.models.load_model('banana_classification.h5')
|
| 12 |
+
|
| 13 |
+
# Define class labels
|
| 14 |
+
class_labels = ["overripe", "ripe", "rotten", "unripe"]
|
| 15 |
+
|
| 16 |
+
# Define route for image classification
|
| 17 |
+
@app.route('/predict', methods=['POST'])
|
| 18 |
+
def predict():
|
| 19 |
+
if 'image' not in request.files:
|
| 20 |
+
return jsonify({'error': 'No image file provided'}), 400
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
img_file = request.files['image']
|
| 24 |
+
img = Image.open(img_file)
|
| 25 |
+
img = img.resize((224, 224)) # Resize image to match model input size
|
| 26 |
+
img_array = np.array(img) / 255.0 # Normalize image array
|
| 27 |
+
print(img_array)
|
| 28 |
+
|
| 29 |
+
# Ensure image array has the correct shape
|
| 30 |
+
if img_array.ndim == 2: # grayscale image
|
| 31 |
+
img_array = np.expand_dims(img_array, axis=-1)
|
| 32 |
+
img_array = np.repeat(img_array, 3, axis=-1) # Convert grayscale to RGB
|
| 33 |
+
elif img_array.shape[-1] != 3: # If not RGB, convert to RGB
|
| 34 |
+
img_array = img_array[..., :3]
|
| 35 |
+
|
| 36 |
+
predictions = model.predict(np.expand_dims(img_array, axis=0))[0]
|
| 37 |
+
|
| 38 |
+
predicted_class_index = np.argmax(predictions)
|
| 39 |
+
predicted_class = class_labels[predicted_class_index]
|
| 40 |
+
confidence = predictions[predicted_class_index]
|
| 41 |
+
|
| 42 |
+
response = {
|
| 43 |
+
'predicted_class': predicted_class,
|
| 44 |
+
'confidence': float(confidence)
|
| 45 |
+
}
|
| 46 |
+
return jsonify(response)
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return jsonify({'error': str(e)}), 500
|
| 50 |
+
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
app.run(host='0.0.0.0', port=7860)
|
banana_classification.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:18c64389d7979df9aa47c568fe861259bc7870a741dbb74178517f4def00fd26
|
| 3 |
+
size 136021864
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask==3.1.0
|
| 2 |
+
pillow==11.1.0
|
| 3 |
+
numpy==2.0.2
|
| 4 |
+
tensorflow==2.18.1
|
| 5 |
+
flask-cors==5.0.1
|
| 6 |
+
gunicorn==23.0.0
|