Commit b2f475c7 authored by Basnayake N.S.N.'s avatar Basnayake N.S.N.

Upload New File

parent 35cf32a5
from flask import Flask, request, jsonify
import cv2
import numpy as np
import os
import time
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# Function to check if the image is likely a leaf and calculate affliction percentage
def is_likely_leaf(img_rgb, mask):
total_pixels = mask.sum() / 255 # Count of white pixels in the mask
# If the object is too small, it's likely not a leaf
if total_pixels < 5000: # this threshold might need adjustment based on your image resolutions
return False
# Convert image to HSV
hsv = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV)
# Define a mask for green pixels
green_mask = cv2.inRange(hsv, (28, 0, 26), (95, 255, 255))
# Count green pixels within the object (leaf)
green_pixels = cv2.bitwise_and(green_mask, green_mask, mask=mask).sum() / 255
green_ratio = green_pixels / total_pixels
# If more than 30% of the leaf is green, it's likely a leaf
if green_ratio > 0.3: # adjust this threshold as needed
return True
return False
def process_image(img):
timestamp = int(time.time())
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred_gray = cv2.GaussianBlur(gray, (5, 5), 0)
binary = cv2.adaptiveThreshold(blurred_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
kernel = np.ones((5, 5), np.uint8)
binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros_like(gray)
if contours:
leaf_contour = max(contours, key=cv2.contourArea)
cv2.drawContours(mask, [leaf_contour], -1, (255), thickness=cv2.FILLED)
gray_background = np.ones_like(img_rgb) * 127 # A (127,127,127) RGB image
leaf_on_gray = cv2.bitwise_and(gray_background, gray_background, mask=cv2.bitwise_not(mask)) + cv2.bitwise_and(
img_rgb, img_rgb, mask=mask)
if is_likely_leaf(leaf_on_gray, mask):
img_HSV = cv2.cvtColor(leaf_on_gray, cv2.COLOR_RGB2HSV)
healthy_mask = cv2.inRange(img_HSV, (28, 0, 26), (95, 255, 255))
inverse_healthy_mask = cv2.bitwise_not(healthy_mask)
total_leaf_area = np.sum(mask == 255)
afflicted_area = np.sum((inverse_healthy_mask == 255) & (mask == 255))
affliction_percentage = round((afflicted_area / total_leaf_area) * 100, 2)
# leaf_on_gray_path = f'leaf_on_gray_{timestamp}.jpeg'
# cv2.imwrite(leaf_on_gray_path, cv2.cvtColor(leaf_on_gray, cv2.COLOR_RGB2BGR))
leaf_on_gray_path = os.path.join(UPLOAD_FOLDER, f'leaf_on_gray_{timestamp}.jpeg')
cv2.imwrite(leaf_on_gray_path, cv2.cvtColor(leaf_on_gray, cv2.COLOR_RGB2BGR))
# healthy_path = f'healthy_areas_{timestamp}.jpeg'
# cv2.imwrite(healthy_path, cv2.bitwise_and(leaf_on_gray, leaf_on_gray, mask=healthy_mask))
healthy_path = os.path.join(UPLOAD_FOLDER, f'healthy_areas_{timestamp}.jpeg')
cv2.imwrite(healthy_path, cv2.bitwise_and(leaf_on_gray, leaf_on_gray, mask=healthy_mask))
# afflictions_path = f'afflictions_{timestamp}.jpeg'
# cv2.imwrite(afflictions_path, cv2.bitwise_and(leaf_on_gray, leaf_on_gray, mask=inverse_healthy_mask))
afflictions_path = os.path.join(UPLOAD_FOLDER, f'afflictions_{timestamp}.jpeg')
cv2.imwrite(afflictions_path, cv2.bitwise_and(leaf_on_gray, leaf_on_gray, mask=inverse_healthy_mask))
# mask_image_path = f'mask_image_{timestamp}.jpeg'
# cv2.imwrite(mask_image_path, np.where(mask == 255, inverse_healthy_mask, 127))
mask_image_path = os.path.join(UPLOAD_FOLDER, f'mask_image_{timestamp}.jpeg')
cv2.imwrite(mask_image_path, np.where(mask == 255, inverse_healthy_mask, 127))
return affliction_percentage, True, leaf_on_gray_path, healthy_path, afflictions_path, mask_image_path
else:
return None, False, None, None, None, None
@app.route('/upload', methods=['POST'])
def upload_file():
try:
uploaded_file = request.files['file']
if uploaded_file.filename != '':
nparr = np.fromstring(uploaded_file.read(), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
affliction_percentage, is_leaf, leaf_on_gray_path, healthy_path, afflictions_path, mask_image_path = process_image(img)
if is_leaf:
return jsonify({
"success": True,
"affliction_percentage": affliction_percentage,
"leaf_on_gray_image": leaf_on_gray_path,
"healthy_areas_image": healthy_path,
"afflictions_image": afflictions_path,
"mask_image": mask_image_path
})
else:
return jsonify({"success": False, "error": "Image is not a clear picture of a leaf."})
else:
return jsonify({"success": False, "error": "No file uploaded."})
except Exception as e:
return jsonify({"success": False, "error": str(e)})
from flask import send_from_directory
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
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