|
from flask import Flask, request |
|
from flask_cors import CORS |
|
|
|
|
|
|
|
import recycleModel |
|
import numpy as np |
|
|
|
import cv2 |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
|
|
@app.route("/", methods=["GET", "POST"]) |
|
def index(): |
|
message = "Welcome to my API! Here are the available endpoints:" |
|
recycle_url = request.host_url + "recycle-prediction" |
|
tree_count_url = request.host_url + "tree-count" |
|
response = { |
|
"message": message, |
|
"endpoints": {"recycle_prediction": recycle_url, "tree_count": tree_count_url}, |
|
} |
|
return response, 200 |
|
|
|
|
|
@app.route("/recycle-prediction", methods=["GET", "POST"]) |
|
def recycle(): |
|
|
|
print(f"Received {request.method} request to /recycle-prediction") |
|
|
|
|
|
if request.method == "POST": |
|
print("Handling POST request") |
|
|
|
|
|
if "img" not in request.files: |
|
print("Image 'img' not found in request files") |
|
return "Image 'img' not found in request files", 400 |
|
|
|
|
|
img = request.files["img"] |
|
img_array = cv2.imdecode(np.frombuffer(img.read(), np.uint8), cv2.IMREAD_COLOR) |
|
recycle_model_instance = recycleModel.RecycleModel() |
|
model = recycle_model_instance.recyclePrediction(img_array) |
|
response = {"Recycling Material": model} |
|
print(f"Processed image and determined material: {model}") |
|
return response |
|
|
|
|
|
print("Received non-POST request to /recycle-prediction") |
|
return "Method Not Allowed", 405 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True) |
|
|
|
|