Ashrafb commited on
Commit
814da2f
·
verified ·
1 Parent(s): c0cfc30

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +23 -38
main.py CHANGED
@@ -7,56 +7,41 @@ import numpy as np
7
  import cv2
8
  import torch
9
  from vtoonify_model import Model
 
 
10
 
11
  app = FastAPI()
12
-
13
- # Load the model
14
  model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
15
- exstyle, message = model.load_model("cartoon1")
16
-
17
- class ImageRequest(BaseModel):
18
- image_file: UploadFile = File(...)
19
-
20
- from fastapi import FastAPI, File, UploadFile
21
- from fastapi.responses import JSONResponse
22
- import numpy as np
23
- import cv2
24
- import torch
25
- from vtoonify_model import Model
26
 
27
- app = FastAPI()
28
-
29
- # Load the model
30
- model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
31
- exstyle, message = model.load_model("cartoon1")
32
-
33
- @app.post("/upload/")
34
- async def toonify_image(image_file: UploadFile = File(...)):
35
  try:
36
- # Read the contents of the uploaded file
37
- contents = await image_file.read()
38
- # Determine the file extension from the content type
39
- ext = image_file.content_type.split("/")[-1]
40
- # Construct a unique filename with the extension
41
- filename = f"uploaded_image.{ext}"
42
 
43
- # Convert the file contents to a numpy array
44
- nparr = np.frombuffer(contents, np.uint8)
45
- # Decode the image using OpenCV
46
- img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
47
 
48
- # Perform image processing with the model
49
- aligned_face, instyle, message = model.detect_and_align_image(img, 200, 200, 200, 200) # Hardcoded values
50
- toonified_img, message = model.image_toonify(aligned_face, instyle, exstyle, style_degree=0.5, style_type="cartoon1")
51
 
52
- # Return the processed image and filename
53
- return {"toonified_image": toonified_img, "message": message, "filename": filename}
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
- # Return an error response if an exception occurs
56
- return JSONResponse(status_code=500, content={"error": str(e)})
57
 
58
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
59
 
60
  @app.get("/")
61
  def index() -> FileResponse:
62
  return FileResponse(path="/app/AB/index.html", media_type="text/html")
 
 
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
+