Ashrafb commited on
Commit
5d5d49d
·
verified ·
1 Parent(s): f41fc03

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -14
main.py CHANGED
@@ -1,6 +1,6 @@
1
  from __future__ import annotations
2
  from fastapi import FastAPI, File, UploadFile
3
- from fastapi.responses import HTMLResponse
4
  from fastapi.staticfiles import StaticFiles
5
  import shutil
6
  from io import BytesIO
@@ -13,26 +13,27 @@ import pathlib
13
  from vtoonify_model import Model
14
 
15
  app = FastAPI()
 
16
 
 
 
 
 
 
17
 
 
 
18
 
19
- model = cartoon1(device='cuda' if torch.cuda.is_available() else 'cpu')
 
 
20
 
21
- def generate_cartoon(image_bytes: bytes) -> bytes:
22
- image = Image.open(BytesIO(image_bytes))
23
- cartoon_image = model.generate_cartoon(image)
24
- with BytesIO() as output:
25
- cartoon_image.save(output, format="PNG")
26
- return output.getvalue()
27
-
28
- @app.post("/upload/")
29
- async def upload_image(file: UploadFile = File(...)):
30
- contents = await file.read()
31
- result_bytes = generate_cartoon(contents)
32
- return {"result": result_bytes}
33
 
34
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
35
 
36
  @app.get("/")
37
  def index() -> FileResponse:
38
  return FileResponse(path="/app/AB/index.html", media_type="text/html")
 
 
1
  from __future__ import annotations
2
  from fastapi import FastAPI, File, UploadFile
3
+ from fastapi.responses import FileResponse, StreamingResponse
4
  from fastapi.staticfiles import StaticFiles
5
  import shutil
6
  from io import BytesIO
 
13
  from vtoonify_model import Model
14
 
15
  app = FastAPI()
16
+ model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
17
 
18
+ @app.post("/process_image")
19
+ async def process_image(file: UploadFile = File(...)):
20
+ # Save the uploaded image locally
21
+ with open("uploaded_image.jpg", "wb") as buffer:
22
+ shutil.copyfileobj(file.file, buffer)
23
 
24
+ # Load the model (assuming 'cartoon1' is always used)
25
+ exstyle, load_info = model.load_model('cartoon1')
26
 
27
+ # Process the uploaded image
28
+ aligned_face, _, input_info = model.detect_and_align_image("uploaded_image.jpg", padding_params=[200, 200, 200, 200])
29
+ result_face, output_info = model.image_toonify(aligned_face, exstyle, 'cartoon1', style_degree=0.5)
30
 
31
+ # Return the processed image
32
+ return FileResponse("result_image.jpg")
 
 
 
 
 
 
 
 
 
 
33
 
34
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
35
 
36
  @app.get("/")
37
  def index() -> FileResponse:
38
  return FileResponse(path="/app/AB/index.html", media_type="text/html")
39
+