Update main.py
Browse files
main.py
CHANGED
@@ -30,25 +30,22 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
30 |
if model is None:
|
31 |
load_model()
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
shutil.copyfileobj(file.file, buffer)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
# Convert the image from BGR to RGB
|
41 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
42 |
|
43 |
# Process the uploaded image
|
44 |
aligned_face, instyle, message = model.detect_and_align_image(frame_rgb, top, bottom, left, right)
|
45 |
processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon1')
|
46 |
|
47 |
# Convert processed image to bytes
|
48 |
-
|
49 |
|
50 |
# Return the processed image as a streaming response
|
51 |
-
return StreamingResponse(BytesIO(
|
52 |
|
53 |
# Mount static files directory
|
54 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|
|
|
30 |
if model is None:
|
31 |
load_model()
|
32 |
|
33 |
+
# Read the uploaded image file
|
34 |
+
contents = await file.read()
|
|
|
35 |
|
36 |
+
# Convert the uploaded image to numpy array
|
37 |
+
nparr = np.frombuffer(contents, np.uint8)
|
38 |
+
frame_rgb = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
|
|
|
|
39 |
|
40 |
# Process the uploaded image
|
41 |
aligned_face, instyle, message = model.detect_and_align_image(frame_rgb, top, bottom, left, right)
|
42 |
processed_image, message = model.image_toonify(aligned_face, instyle, model.exstyle, style_degree=0.5, style_type='cartoon1')
|
43 |
|
44 |
# Convert processed image to bytes
|
45 |
+
_, encoded_image = cv2.imencode('.jpg', processed_image)
|
46 |
|
47 |
# Return the processed image as a streaming response
|
48 |
+
return StreamingResponse(io.BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
49 |
|
50 |
# Mount static files directory
|
51 |
app.mount("/", StaticFiles(directory="AB", html=True), name="static")
|