Luisgust commited on
Commit
df581a6
·
verified ·
1 Parent(s): f0c6553

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -9
main.py CHANGED
@@ -20,6 +20,15 @@ net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype='float32')]
20
  # Initialize FastAPI app
21
  app = FastAPI()
22
 
 
 
 
 
 
 
 
 
 
23
  def colorize_image(image):
24
  # Convert the PIL image to OpenCV format
25
  image = np.array(image)
@@ -38,23 +47,25 @@ def colorize_image(image):
38
 
39
  L = cv2.split(lab)[0]
40
  colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
41
- colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
42
  colorized = np.clip(colorized, 0, 1)
43
  colorized = (255 * colorized).astype("uint8")
44
 
45
- # Convert the colorized image from BGR to RGB
46
- colorized = cv2.cvtColor(colorized, cv2.COLOR_BGR2RGB)
47
-
48
  return colorized
49
 
50
  @app.post("/upload/")
51
  async def upload(file: UploadFile = File(...)):
52
- input_image = Image.open(file.file).convert("RGB")
53
- colorized_image = colorize_image(input_image)
 
 
 
54
 
55
  # Convert the colorized image to bytes
56
- _, buffer = cv2.imencode('.png', colorized_image)
57
- io_buf = io.BytesIO(buffer)
 
 
58
 
59
- return StreamingResponse(io_buf, media_type="image/png")
60
 
 
20
  # Initialize FastAPI app
21
  app = FastAPI()
22
 
23
+ def read_image(file):
24
+ # Open the image with Pillow
25
+ pil_image = Image.open(file)
26
+
27
+ # Convert the Pillow image to a NumPy array
28
+ image_array = np.array(pil_image)
29
+
30
+ return image_array
31
+
32
  def colorize_image(image):
33
  # Convert the PIL image to OpenCV format
34
  image = np.array(image)
 
47
 
48
  L = cv2.split(lab)[0]
49
  colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
50
+ colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2RGB)
51
  colorized = np.clip(colorized, 0, 1)
52
  colorized = (255 * colorized).astype("uint8")
53
 
 
 
 
54
  return colorized
55
 
56
  @app.post("/upload/")
57
  async def upload(file: UploadFile = File(...)):
58
+ # Read the image using Pillow and convert to NumPy array
59
+ image_array = read_image(file.file)
60
+
61
+ # Process the image
62
+ colorized_image = colorize_image(image_array)
63
 
64
  # Convert the colorized image to bytes
65
+ pil_image = Image.fromarray(colorized_image)
66
+ buf = io.BytesIO()
67
+ pil_image.save(buf, format="PNG")
68
+ buf.seek(0)
69
 
70
+ return StreamingResponse(buf, media_type="image/png")
71