main.py

parent b61effc8
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from uvicorn import run
import cv2
import numpy as np
from PIL import Image
import shutil
import shutil
import os
app = FastAPI()
origins = ["*"]
methods = ["*"]
headers = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins = origins,
allow_credentials = True,
allow_methods = methods,
allow_headers = headers
)
origin = r"C:\xampp\htdocs\wcolor\php\files\fishwatercolor.jpg"
target = r"D:\Lectures\4thYear2ndSem\RP\Water\watercolor\fishwatercolor.jpg"
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
blue_range = np.array([[100, 50, 50], [130, 255, 255]])
green_range = np.array([[40, 50, 50], [80, 255, 255]])
red_range1 = np.array([[0, 50, 50], [10, 255, 255]])
red_range2 = np.array([[170, 50, 50], [180, 255, 255]])
def show_water_color():
shutil.copy(origin,target)
# Get the image from the frontend
# image_file = request.files['image']
# img = Image.open(image_file)
img = Image.open("fishwatercolor.jpg")
# Convert the image to the HSV color space
hsv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2HSV)
# Calculate the percentage of blue pixels in the image
blue_mask = cv2.inRange(hsv_img, blue_range[0], blue_range[1])
blue_pixels = np.sum(blue_mask == 255)
total_pixels = blue_mask.shape[0] * blue_mask.shape[1]
blue_percent = blue_pixels / total_pixels * 100
# Calculate the percentage of green pixels in the image
green_mask = cv2.inRange(hsv_img, green_range[0], green_range[1])
green_pixels = np.sum(green_mask == 255)
green_percent = green_pixels / total_pixels * 100
# Calculate the percentage of red pixels in the image
red_mask1 = cv2.inRange(hsv_img, red_range1[0], red_range1[1])
red_mask2 = cv2.inRange(hsv_img, red_range2[0], red_range2[1])
red_mask = cv2.addWeighted(red_mask1, 1.0, red_mask2, 1.0, 0.0)
red_pixels = np.sum(red_mask == 255)
red_percent = red_pixels / total_pixels * 100
# Determine the dominant color of the image
colors, counts = np.unique(hsv_img.reshape(-1, hsv_img.shape[-1]), axis=0, return_counts=True)
max_count_index = np.argmax(counts)
dominant_color = colors[max_count_index]
# Process the water color
if green_percent > 10:
water_color = 'green'
elif green_percent < 1:
water_color = 'brown'
else:
water_color = 'normal'
# Return the results as JSON
# results = {
# 'image_name': image_file.filename,
# 'blue_pixels_percent': blue_percent,
# 'green_pixels_percent': green_percent,
# 'red_pixels_percent': red_percent,
# 'dominant_color': dominant_color.tolist(),
# 'water_color': water_color
# }
results = {
'water_color': water_color
}
return results
@app.get("/getResult")
async def send_image_get_prediction(request: Request):
rt = show_water_color()
return templates.TemplateResponse("Saveimage.html", {"request": request, "variable": rt})
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
run(app, host="0.0.0.0", port=port)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment