Edvards commited on
Commit
d194cba
1 Parent(s): 9743956
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -1,9 +1,12 @@
1
  from fastapi import FastAPI, File, UploadFile
 
2
  from retinaface import RetinaFace
3
  import cv2
4
  import numpy as np
5
- from typing import Any, Dict
6
  from rembg import remove
 
 
7
 
8
  app = FastAPI()
9
 
@@ -35,12 +38,19 @@ async def detect_faces(file: UploadFile = File(...)):
35
  @app.post("/remove_background")
36
  async def remove_background(file: UploadFile = File(...)):
37
  contents = await file.read()
38
- nparr = np.frombuffer(contents, np.uint8)
39
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
40
- # Remove background
41
- image = remove(image)
 
42
 
43
- return image
 
 
 
 
 
 
44
 
45
 
46
  @app.get("/")
 
1
  from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import StreamingResponse
3
  from retinaface import RetinaFace
4
  import cv2
5
  import numpy as np
6
+ from typing import Any
7
  from rembg import remove
8
+ from PIL import Image
9
+ import io
10
 
11
  app = FastAPI()
12
 
 
38
  @app.post("/remove_background")
39
  async def remove_background(file: UploadFile = File(...)):
40
  contents = await file.read()
41
+ image = Image.open(io.BytesIO(contents))
42
+
43
+ image_rembg = remove(image)
44
+ image_np = np.array(image_rembg)
45
+ image = Image.fromarray(image_np)
46
 
47
+ # Convert the image to bytes
48
+ img_byte_arr = io.BytesIO()
49
+ image.save(img_byte_arr, format='PNG')
50
+ img_byte_arr.seek(0)
51
+
52
+ return StreamingResponse(io.BytesIO(img_byte_arr.read()), media_type="image/png")
53
+
54
 
55
 
56
  @app.get("/")