Ashrafb commited on
Commit
4ce8314
·
verified ·
1 Parent(s): ea1444b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -5
main.py CHANGED
@@ -21,6 +21,9 @@ async def load_model():
21
  global model
22
  model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
23
 
 
 
 
24
  @app.post("/upload/")
25
  async def process_image(file: UploadFile = File(...), top: int = Form(...), bottom: int = Form(...), left: int = Form(...), right: int = Form(...)):
26
  if model is None:
@@ -34,12 +37,11 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
34
  aligned_face, instyle, message = model.detect_and_align_image("uploaded_image.jpg", top, bottom, left, right)
35
  processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon1')
36
 
37
- # Save the processed image
38
- with open("result_image.jpg", "wb") as result_buffer:
39
- result_buffer.write(processed_image)
40
 
41
- # Return the processed image
42
- return FileResponse("result_image.jpg", media_type="image/jpeg", headers={"Content-Disposition": "attachment; filename=result_image.jpg"})
43
 
44
 
45
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")
 
21
  global model
22
  model = Model(device='cuda' if torch.cuda.is_available() else 'cpu')
23
 
24
+ from fastapi.responses import StreamingResponse
25
+ from io import BytesIO
26
+
27
  @app.post("/upload/")
28
  async def process_image(file: UploadFile = File(...), top: int = Form(...), bottom: int = Form(...), left: int = Form(...), right: int = Form(...)):
29
  if model is None:
 
37
  aligned_face, instyle, message = model.detect_and_align_image("uploaded_image.jpg", top, bottom, left, right)
38
  processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon1')
39
 
40
+ # Convert processed image to bytes
41
+ image_bytes = cv2.imencode('.jpg', processed_image)[1].tobytes()
 
42
 
43
+ # Return the processed image as a streaming response
44
+ return StreamingResponse(BytesIO(image_bytes), media_type="image/jpeg")
45
 
46
 
47
  app.mount("/", StaticFiles(directory="AB", html=True), name="static")