Ashrafb commited on
Commit
7c33baa
·
verified ·
1 Parent(s): e89342c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -33
main.py CHANGED
@@ -1,47 +1,39 @@
1
- from fastapi import FastAPI, File, UploadFile,Form
2
- from fastapi.responses import FileResponse, StreamingResponse
3
- from fastapi.staticfiles import StaticFiles
4
  from fastapi import FastAPI, File, UploadFile
5
- from pydantic import BaseModel
6
- import numpy as np
7
- import cv2
8
  import torch
 
 
9
  from vtoonify_model import Model
10
- import shutil
11
- import os
12
 
13
  app = FastAPI()
14
  model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
15
 
16
  @app.post("/upload/")
17
- async def upload_image(file: UploadFile = File(...), top: int = Form(...), bottom: int = Form(...), left: int = Form(...), right: int = Form(...)):
18
- try:
19
- with open("temp.jpg", "wb") as buffer:
20
- shutil.copyfileobj(file.file, buffer)
21
-
22
- # Perform image processing using the model
23
- result_image, _, _ = model.detect_and_align_image("temp.jpg", top, bottom, left, right)
24
-
25
- # Save the result image temporarily
26
- result_path = "result.jpg"
27
- cv2.imwrite(result_path, result_image)
28
-
29
- # Return the result image
30
- with open(result_path, "rb") as result_buffer:
31
- result_image_bytes = result_buffer.read()
32
-
33
- # Remove temporary files
34
- os.remove("temp.jpg")
35
- os.remove(result_path)
36
-
37
- return {"result_image": result_image_bytes}
38
-
39
- except Exception as e:
40
- return {"error": str(e)}
41
 
42
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
43
 
44
  @app.get("/")
45
  def index() -> FileResponse:
46
  return FileResponse(path="/app/AB/index.html", media_type="text/html")
47
-
 
1
+ from __future__ import annotations
 
 
2
  from fastapi import FastAPI, File, UploadFile
3
+ from fastapi.responses import FileResponse
4
+ from fastapi.staticfiles import StaticFiles
5
+ import shutil
6
  import torch
7
+ import numpy as np
8
+
9
  from vtoonify_model import Model
 
 
10
 
11
  app = FastAPI()
12
  model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
13
 
14
  @app.post("/upload/")
15
+ async def process_image(file: UploadFile = File(...)):
16
+ # Save the uploaded image locally
17
+ with open("uploaded_image.jpg", "wb") as buffer:
18
+ shutil.copyfileobj(file.file, buffer)
19
+
20
+ # Load the model (assuming 'cartoon1' is always used)
21
+ exstyle, load_info = model.load_model('cartoon1')
22
+
23
+ # Process the uploaded image
24
+ top, bottom, left, right = 200, 200, 200, 200
25
+ aligned_face, _, input_info = model.detect_and_align_image("uploaded_image.jpg", top, bottom, left, right)
26
+ processed_image, message = model.image_toonify(aligned_face, None, exstyle, style_degree=0.5, style_type='cartoon1')
27
+
28
+ # Save the processed image
29
+ with open("result_image.jpg", "wb") as result_buffer:
30
+ result_buffer.write(processed_image)
31
+
32
+ # Return the processed image
33
+ return FileResponse("result_image.jpg", media_type="image/jpeg", headers={"Content-Disposition": "attachment; filename=result_image.jpg"})
 
 
 
 
 
34
 
35
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
36
 
37
  @app.get("/")
38
  def index() -> FileResponse:
39
  return FileResponse(path="/app/AB/index.html", media_type="text/html")