Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import onnxruntime as rt
|
3 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
from fastapi.responses import StreamingResponse
|
6 |
+
from PIL import Image, ImageOps
|
7 |
+
import numpy as np
|
8 |
+
import io
|
9 |
+
import face_detection # Ensure this is the adjusted face_detection.py
|
10 |
+
|
11 |
+
# Initialize FastAPI app
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Allow CORS for your frontend application
|
15 |
+
app.add_middleware(
|
16 |
+
CORSMiddleware,
|
17 |
+
allow_origins=["*"], # Change this to your frontend's URL in production
|
18 |
+
allow_credentials=True,
|
19 |
+
allow_methods=["*"],
|
20 |
+
allow_headers=["*"],
|
21 |
+
)
|
22 |
+
|
23 |
+
# Load the ONNX model
|
24 |
+
MODEL_FILE = "ffhqu2vintage512_pix2pixHD_v1E11-inp2inst-simp.onnx"
|
25 |
+
so = rt.SessionOptions()
|
26 |
+
so.inter_op_num_threads = 4
|
27 |
+
so.intra_op_num_threads = 4
|
28 |
+
session = rt.InferenceSession(MODEL_FILE, sess_options=so)
|
29 |
+
input_name = session.get_inputs()[0].name
|
30 |
+
output_name = session.get_outputs()[0].name
|
31 |
+
|
32 |
+
def array_to_image(array_in):
|
33 |
+
array_in = np.squeeze(255 * (array_in + 1) / 2)
|
34 |
+
array_in = np.transpose(array_in, (1, 2, 0))
|
35 |
+
im = Image.fromarray(array_in.astype(np.uint8))
|
36 |
+
return im
|
37 |
+
|
38 |
+
def image_as_array(image_in):
|
39 |
+
im_array = np.array(image_in, np.float32)
|
40 |
+
im_array = (im_array / 255) * 2 - 1
|
41 |
+
im_array = np.transpose(im_array, (2, 0, 1))
|
42 |
+
im_array = np.expand_dims(im_array, 0)
|
43 |
+
return im_array
|
44 |
+
|
45 |
+
def find_aligned_face(image_in, size=512):
|
46 |
+
aligned_image, n_faces, quad = face_detection.align(image_in, face_index=0, output_size=size)
|
47 |
+
return aligned_image, n_faces, quad
|
48 |
+
|
49 |
+
def align_first_face(image_in, size=512):
|
50 |
+
aligned_image, n_faces, quad = find_aligned_face(image_in, size=size)
|
51 |
+
if n_faces == 0:
|
52 |
+
try:
|
53 |
+
image_in = ImageOps.exif_transpose(image_in)
|
54 |
+
except:
|
55 |
+
print("exif problem, not rotating")
|
56 |
+
image_in = image_in.resize((size, size))
|
57 |
+
im_array = image_as_array(image_in)
|
58 |
+
else:
|
59 |
+
im_array = image_as_array(aligned_image)
|
60 |
+
|
61 |
+
return im_array
|
62 |
+
|
63 |
+
def img_concat_h(im1, im2):
|
64 |
+
dst = Image.new('RGB', (im1.width + im2.width, im1.height))
|
65 |
+
dst.paste(im1, (0, 0))
|
66 |
+
dst.paste(im2, (im1.width, 0))
|
67 |
+
return dst
|
68 |
+
|
69 |
+
def face2vintage(img: Image.Image, size: int) -> Image.Image:
|
70 |
+
aligned_img = align_first_face(img)
|
71 |
+
if aligned_img is None:
|
72 |
+
return None
|
73 |
+
|
74 |
+
output = session.run([output_name], {input_name: aligned_img})[0]
|
75 |
+
output = array_to_image(output)
|
76 |
+
aligned_img = array_to_image(aligned_img).resize((output.width, output.height))
|
77 |
+
output = img_concat_h(aligned_img, output)
|
78 |
+
|
79 |
+
return output
|
80 |
+
|
81 |
+
@app.post("/process_image/")
|
82 |
+
async def process_image(file: UploadFile = File(...)):
|
83 |
+
try:
|
84 |
+
# Read the image file
|
85 |
+
image_bytes = await file.read()
|
86 |
+
image = Image.open(io.BytesIO(image_bytes))
|
87 |
+
|
88 |
+
# Process the image
|
89 |
+
processed_image = face2vintage(image, 512)
|
90 |
+
|
91 |
+
# Convert the processed image to bytes
|
92 |
+
if processed_image is None:
|
93 |
+
raise HTTPException(status_code=400, detail="Could not process image.")
|
94 |
+
|
95 |
+
img_byte_arr = io.BytesIO()
|
96 |
+
processed_image.save(img_byte_arr, format='PNG')
|
97 |
+
img_byte_arr.seek(0)
|
98 |
+
|
99 |
+
return StreamingResponse(img_byte_arr, media_type="image/png")
|
100 |
+
|
101 |
+
except Exception as e:
|
102 |
+
raise HTTPException(status_code=500, detail=str(e))
|