Allinone / app.py
RANA
using wsgi which is gunicorn 8
5cc16fb
raw
history blame contribute delete
No virus
2.32 kB
from flask import Flask, request
from flask_cors import CORS
# from io import BytesIO
# from PIL import Image
# import treeCountModel
import recycleModel
import numpy as np
# import requests
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 the method of the request received
print(f"Received {request.method} request to /recycle-prediction")
# Handle POST requests
if request.method == "POST":
print("Handling POST request")
# Check if the image is present in the request
if "img" not in request.files:
print("Image 'img' not found in request files")
return "Image 'img' not found in request files", 400
# Process the image
img = request.files["img"]
img_array = cv2.imdecode(np.frombuffer(img.read(), np.uint8), cv2.IMREAD_COLOR)
recycle_model_instance = recycleModel.RecycleModel() # Create an instance of the class
model = recycle_model_instance.recyclePrediction(img_array) # Use the method on that instance
response = {"Recycling Material": model}
print(f"Processed image and determined material: {model}")
return response
# Handle GET requests (or other methods)
print("Received non-POST request to /recycle-prediction")
return "Method Not Allowed", 405
# @app.route("/tree-count/<path:url>", methods=["GET", "POST"])
# def treeCount(url):
# result = None
# if request.method == "GET":
# url = request.url
# url = url[50:]
# response = requests.get(url)
# if url is None:
# return "No image URL provided", 400
# img = Image.open(BytesIO(response.content))
# model = treeCountModel.countTree(img)
# result = {"Tree Count": model}
# return result
if __name__ == "__main__":
app.run(debug=True)