Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eyad #31

Merged
merged 7 commits into from
May 8, 2024
Merged

Eyad #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask import Flask


def create_app():
app = Flask(__name__)

from .api import api_blueprint

app.register_blueprint(api_blueprint)
return app
32 changes: 32 additions & 0 deletions api/app/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from flask import Blueprint, request, jsonify
from flask_cors import CORS
from PIL import Image
import os
import io
import base64
from .image_processing import process_image

api_blueprint = Blueprint("api", __name__)


@api_blueprint.route("/coloring", methods=["POST"])
def process_image_route():
# if "image" not in request.files:
# return jsonify({"error": "No image provided"}), 400

img_byte_array = io.BytesIO()

base64_image_data = request.json["image"]
image_data = base64.b64decode(base64_image_data)

processed_image = process_image(io.BytesIO(image_data))

# Convert the processed image to bytes

processed_image.save(img_byte_array, format="JPEG")
img_byte_array.seek(0)

# Convert bytes to base64 encoded string
base64_image = base64.b64encode(img_byte_array.getvalue()).decode("utf-8")

return jsonify({"processed_image": base64_image})
28 changes: 28 additions & 0 deletions api/app/image_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from PIL import Image
from io import BytesIO
from img2img.models.pix2pix.predictor import Pix2PixPredictor
import numpy as np

predictor = Pix2PixPredictor(
model_path="/home/eyad/watashi-ubuntu/academics/seminar/pbl/image2image/out/saved_models/anime_training/gen.pth.tar"
)


def process_image(image_file):
# Open the image
image = Image.open(image_file)

# Example processing: convert to RGB array
processed_image = np.array(image.convert("RGB"))

# Ensure the array shape is correct
assert processed_image.shape[2] == 3

# Process the image using the Pix2Pix model
processed_image = predictor(processed_image)

# Convert the processed image array back to PIL Image
processed_image = Image.fromarray(processed_image)

# Return the processed image as a PIL Image object
return processed_image
18 changes: 18 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Use
`
flask run --debug
`
to active the API server"""

from app import create_app

app = create_app()


def main() -> int:
app.run(debug=True)
return 0


if __name__ == "__main__":
raise SystemExit(main())
Binary file added api/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions api/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import requests

image = open("test.png", "rb")
new_image = requests.post("http://127.0.0.1:5000/process_image", files={"image": image})
print(new_image.headers)
image.close()
Loading