Alessio Grancini commited on
Commit
557ac16
·
verified ·
1 Parent(s): dcdf01a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -147,6 +147,28 @@ def get_camera_matrix(depth_estimator):
147
  }
148
 
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  @spaces.GPU
151
  def get_detection_data(image_data):
152
  """
 
147
  }
148
 
149
 
150
+ def encode_base64_image(image_array):
151
+ """
152
+ Encodes a NumPy (OpenCV) image array to a base64-encoded PNG DataURL
153
+ like "data:image/png;base64,<...>".
154
+ """
155
+ import base64
156
+ import cv2
157
+
158
+ # If your image is BGR, that’s fine. We just need to encode it as PNG bytes.
159
+ # (Optionally convert to RGB first if you need consistent color channels.)
160
+
161
+ success, encoded_buffer = cv2.imencode(".png", image_array)
162
+ if not success:
163
+ raise ValueError("Could not encode image to PNG buffer")
164
+
165
+ # Encode the buffer to base64
166
+ b64_str = base64.b64encode(encoded_buffer).decode("utf-8")
167
+
168
+ # Return a data URL
169
+ return "data:image/png;base64," + b64_str
170
+
171
+
172
  @spaces.GPU
173
  def get_detection_data(image_data):
174
  """