48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
# utils.py
|
|
|
|
import cv2
|
|
import base64
|
|
import numpy as np
|
|
|
|
def encode_image_base64(frame):
|
|
"""
|
|
Codifica uma imagem (np.ndarray) como string base64 JPEG.
|
|
"""
|
|
ret, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 80])
|
|
if not ret:
|
|
return None
|
|
base64_str = base64.b64encode(buffer).decode('utf-8')
|
|
return base64_str
|
|
|
|
def gerar_mapa_profundidade(depth_frame, linhas=10, colunas=10):
|
|
altura, largura = depth_frame.shape
|
|
h = altura // linhas
|
|
w = largura // colunas
|
|
|
|
grid = []
|
|
resposta = {
|
|
"resolucao": f"{linhas}x{colunas}",
|
|
"unidade": "cm",
|
|
"grid": grid
|
|
}
|
|
|
|
if depth_frame is None:
|
|
return resposta
|
|
|
|
for i in range(linhas):
|
|
linha = []
|
|
for j in range(colunas):
|
|
y1, y2 = i * h, (i + 1) * h
|
|
x1, x2 = j * w, (j + 1) * w
|
|
celula = depth_frame[y1:y2, x1:x2]
|
|
validos = celula[(celula > 0) & (celula < 10000)]
|
|
if validos.size == 0:
|
|
linha.append(None)
|
|
else:
|
|
media_cm = np.mean(validos) / 100.0
|
|
linha.append(round(float(media_cm), 2))
|
|
grid.append(linha)
|
|
|
|
resposta["grid"] = grid
|
|
return resposta
|