Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,8 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
-
from fastapi.middleware.cors import CORSMiddleware # Add this import
|
| 8 |
|
| 9 |
-
# Load your trained model
|
| 10 |
model = tf.keras.models.load_model('recyclebot.keras')
|
| 11 |
|
| 12 |
# Class names for predictions (modify if necessary)
|
|
@@ -15,36 +14,29 @@ class_names = ['Glass', 'Metal', 'Paperboard', 'Plastic-Polystyrene', 'Plastic-R
|
|
| 15 |
# Create FastAPI app
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
CORSMiddleware,
|
| 20 |
-
allow_origins=["https://recyclesmart.vercel.app/"], # or use ["*"] for all origins (not recommended for production)
|
| 21 |
-
allow_credentials=True,
|
| 22 |
-
allow_methods=["*"],
|
| 23 |
-
allow_headers=["*"],
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# Preprocessing the image
|
| 27 |
def preprocess_image(image_file):
|
|
|
|
| 28 |
image = Image.open(image_file)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
@app.post("/predict")
|
| 41 |
async def predict(file: UploadFile = File(...)):
|
| 42 |
try:
|
| 43 |
-
img_array = preprocess_image(file.file)
|
| 44 |
-
predictions = model.predict(img_array)
|
| 45 |
-
predicted_class_idx = np.argmax(predictions, axis=1)[0]
|
| 46 |
-
predicted_class = class_names[predicted_class_idx]
|
| 47 |
-
#predicted_class = ((class_names[np.argmax(predictions)]))
|
| 48 |
return JSONResponse(content={"prediction": predicted_class})
|
| 49 |
except Exception as e:
|
| 50 |
return JSONResponse(content={"error": str(e)}, status_code=400)
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
from fastapi.responses import JSONResponse
|
|
|
|
| 7 |
|
| 8 |
+
# Load your trained model
|
| 9 |
model = tf.keras.models.load_model('recyclebot.keras')
|
| 10 |
|
| 11 |
# Class names for predictions (modify if necessary)
|
|
|
|
| 14 |
# Create FastAPI app
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
# Preprocessing the image (resize, reshape without normalization)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def preprocess_image(image_file):
|
| 19 |
+
# Load image using PIL (or could use OpenCV, depending on preference)
|
| 20 |
image = Image.open(image_file)
|
| 21 |
+
|
| 22 |
+
# Convert image to numpy array
|
| 23 |
+
image = np.array(image)
|
| 24 |
+
|
| 25 |
+
# Resize to the input shape expected by the model
|
| 26 |
+
image = cv2.resize(image, (240, 240)) # Resize image to match model input
|
| 27 |
+
|
| 28 |
+
# Reshape the image (similar to your local code)
|
| 29 |
+
image = image.reshape(-1, 240, 240, 3) # Add the batch dimension for inference
|
| 30 |
+
|
| 31 |
+
return image
|
| 32 |
|
| 33 |
@app.post("/predict")
|
| 34 |
async def predict(file: UploadFile = File(...)):
|
| 35 |
try:
|
| 36 |
+
img_array = preprocess_image(file.file) # Preprocess the image
|
| 37 |
+
predictions = model.predict(img_array) # Get predictions
|
| 38 |
+
predicted_class_idx = np.argmax(predictions, axis=1)[0] # Get predicted class index
|
| 39 |
+
predicted_class = class_names[predicted_class_idx] # Convert to class name
|
|
|
|
| 40 |
return JSONResponse(content={"prediction": predicted_class})
|
| 41 |
except Exception as e:
|
| 42 |
return JSONResponse(content={"error": str(e)}, status_code=400)
|