From cf857bc8af5ac3725f3bdb40dcdc80752595652f Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 15 May 2024 23:23:57 +0300 Subject: Final version of backend and frontend --- backend/Dockerfile | 18 ----- backend/app.py | 77 --------------------- backend/requirements.txt | 5 -- backend/server/Dockerfile | 24 +++++++ backend/server/app.py | 145 ++++++++++++++++++++++++++++++++++++++++ backend/server/requirements.txt | 7 ++ backend/worker/Dockerfile | 17 +++++ backend/worker/requirements.txt | 5 ++ backend/worker/worker.py | 129 +++++++++++++++++++++++++++++++++++ 9 files changed, 327 insertions(+), 100 deletions(-) delete mode 100644 backend/Dockerfile delete mode 100644 backend/app.py delete mode 100644 backend/requirements.txt create mode 100644 backend/server/Dockerfile create mode 100644 backend/server/app.py create mode 100644 backend/server/requirements.txt create mode 100644 backend/worker/Dockerfile create mode 100644 backend/worker/requirements.txt create mode 100644 backend/worker/worker.py (limited to 'backend') diff --git a/backend/Dockerfile b/backend/Dockerfile deleted file mode 100644 index ff49235..0000000 --- a/backend/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -# Use an official Python runtime as a parent image -FROM python:3.9-slim - -# Set the working directory to /app -WORKDIR /app - -# Copy the current directory contents into the container at /app -COPY . /app - -# Install any needed packages specified in requirements.txt -RUN pip install --no-cache-dir -r requirements.txt - -# Make port 5000 available to the world outside this container -EXPOSE 5000 - -# Run app.py when the container launches -CMD ["python", "app.py"] - diff --git a/backend/app.py b/backend/app.py deleted file mode 100644 index 4ed4954..0000000 --- a/backend/app.py +++ /dev/null @@ -1,77 +0,0 @@ -import os -from flask import Flask, request, jsonify -from flask_cors import CORS -import boto3 -import cv2 -import tempfile -import numpy as np - -app = Flask(__name__) -CORS(app) - -# Configure AWS S3 -s3 = boto3.client('s3') -BUCKET_NAME_ORIGINAL = "original-images-rapid-macaw" -BUCKET_NAME_PROCESSED = "processed-images-rapid-macaw" - -@app.route('/upload', methods=['POST']) -def upload_file(): - if 'image' not in request.files: - return jsonify({'error': 'No file provided'}), 400 - - file = request.files['image'] - operation = request.form.get('operation', 'edge_detection') # Default to edge detection - if file and allowed_file(file.filename): - # Save the file temporarily - temp_file = tempfile.NamedTemporaryFile(delete=False) - file.save(temp_file.name) - - # Upload to S3 original bucket - with open(temp_file.name, "rb") as img_data: - s3.put_object(Bucket=BUCKET_NAME_ORIGINAL, Key=file.filename, Body=img_data, ContentType="image/png") - - # Fetch the image from the original bucket - original_img_obj = s3.get_object(Bucket=BUCKET_NAME_ORIGINAL, Key=file.filename) - original_img_data = original_img_obj['Body'].read() - - # Process the image - processed_image_path = process_image(original_img_data, operation) - - # Upload processed image to S3 processed bucket - processed_filename = f"processed_{file.filename}" - with open(processed_image_path, "rb") as processed_img_data: - s3.put_object(Bucket=BUCKET_NAME_PROCESSED, Key=processed_filename, Body=processed_img_data, ContentType="image/png") - - # Clean up temporary files - os.remove(temp_file.name) - os.remove(processed_image_path) - - processed_file_url = f'https://{BUCKET_NAME_PROCESSED}.s3.amazonaws.com/{processed_filename}' - - return jsonify({'message': 'File processed and uploaded successfully', 'processed_file': processed_file_url}), 200 - else: - return jsonify({'error': 'Invalid file type'}), 400 - -def allowed_file(filename): - return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'png', 'jpg', 'jpeg'} - -def process_image(image_data, operation): - # Convert image data to numpy array - nparr = np.frombuffer(image_data, np.uint8) - img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) - - processed_img = img - - if operation == 'edge_detection': - processed_img = cv2.Canny(img, 100, 200) - elif operation == 'color_inversion': - processed_img = cv2.bitwise_not(img) - - # Save processed image to a temporary path - output_path = os.path.join(tempfile.gettempdir(), f'processed_image.png') - cv2.imwrite(output_path, processed_img) - - return output_path - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/backend/requirements.txt b/backend/requirements.txt deleted file mode 100644 index bf3d422..0000000 --- a/backend/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -flask -flask-cors -boto3 -opencv-python-headless -numpy diff --git a/backend/server/Dockerfile b/backend/server/Dockerfile new file mode 100644 index 0000000..8601d37 --- /dev/null +++ b/backend/server/Dockerfile @@ -0,0 +1,24 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Install dependencies for OpenCV using apt-get +RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 curl + +# Set the working directory +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Make port 5000 available to the world outside this container +EXPOSE 5000 + +# Define environment variable +ENV FLASK_APP=app.py + +# Run app.py when the container launches +CMD ["python", "app.py"] + diff --git a/backend/server/app.py b/backend/server/app.py new file mode 100644 index 0000000..1b4e80e --- /dev/null +++ b/backend/server/app.py @@ -0,0 +1,145 @@ +import pika +import json +from flask import Flask, request, jsonify +from flask_cors import CORS +import boto3 +import tempfile +import os +import base64 +import cv2 +import numpy as np + +app = Flask(__name__) +CORS(app) + +s3 = boto3.client('s3') +BUCKET_NAME_ORIGINAL = "original-images-allowing-griffon" +BUCKET_NAME_PROCESSED = "processed-images-allowing-griffon" + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'png', 'jpg', 'jpeg'} + +def split_image(image_data, num_parts): + img = cv2.imdecode(np.frombuffer(image_data, np.uint8), cv2.IMREAD_COLOR) + height, width, _ = img.shape + part_height = height // num_parts + parts = [] + + for i in range(num_parts): + part_img = img[i * part_height: (i + 1) * part_height if i != num_parts - 1 else height, :, :] + _, buffer = cv2.imencode('.png', part_img) + part_data = buffer.tobytes() + parts.append(part_data) + + return parts, width, height, part_height + +def publish_task(part_data, filename, part_num, operation, callback_queue): + connection = pika.BlockingConnection(pika.ConnectionParameters('