Update main.py
Browse files
main.py
CHANGED
@@ -32,9 +32,19 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
32 |
# Read the uploaded image file
|
33 |
contents = await file.read()
|
34 |
|
35 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
37 |
-
temp_file.
|
38 |
temp_file_path = temp_file.name
|
39 |
|
40 |
try:
|
@@ -49,8 +59,11 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
49 |
logging.error("Failed to toonify the image.")
|
50 |
return {"error": message}
|
51 |
|
|
|
|
|
|
|
52 |
# Convert processed image to bytes
|
53 |
-
_, encoded_image = cv2.imencode('.jpg',
|
54 |
|
55 |
# Return the processed image as a streaming response
|
56 |
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|
|
|
32 |
# Read the uploaded image file
|
33 |
contents = await file.read()
|
34 |
|
35 |
+
# Convert the uploaded image to numpy array
|
36 |
+
nparr = np.frombuffer(contents, np.uint8)
|
37 |
+
frame_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # Read as BGR format by default
|
38 |
+
|
39 |
+
if frame_bgr is None:
|
40 |
+
logging.error("Failed to decode the image.")
|
41 |
+
return {"error": "Failed to decode the image. Please ensure the file is a valid image format."}
|
42 |
+
|
43 |
+
logging.info(f"Uploaded image shape: {frame_bgr.shape}")
|
44 |
+
|
45 |
+
# Save the uploaded image temporarily to pass the file path to the model
|
46 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
47 |
+
cv2.imwrite(temp_file.name, frame_bgr)
|
48 |
temp_file_path = temp_file.name
|
49 |
|
50 |
try:
|
|
|
59 |
logging.error("Failed to toonify the image.")
|
60 |
return {"error": message}
|
61 |
|
62 |
+
# Convert the processed image to RGB before returning
|
63 |
+
processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
|
64 |
+
|
65 |
# Convert processed image to bytes
|
66 |
+
_, encoded_image = cv2.imencode('.jpg', processed_image_rgb)
|
67 |
|
68 |
# Return the processed image as a streaming response
|
69 |
return StreamingResponse(BytesIO(encoded_image.tobytes()), media_type="image/jpeg")
|