Skip to content

Commit d90bd69

Browse files
committed
still have decoding issue with image normarlize to bytes
1 parent 6a063d3 commit d90bd69

File tree

3 files changed

+235
-113
lines changed

3 files changed

+235
-113
lines changed

bigframes/blob/_functions.py

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
import bigframes.session
2222
import bigframes.session._io.bigquery as bf_io_bigquery
2323

24-
_PYTHON_TO_BQ_TYPES = {int: "INT64", float: "FLOAT64", str: "STRING", bytes: "BYTES"}
24+
_PYTHON_TO_BQ_TYPES = {
25+
int: "INT64",
26+
float: "FLOAT64",
27+
str: "STRING",
28+
bytes: "BYTES",
29+
bool: "BOOL",
30+
}
2531

2632

2733
@dataclass(frozen=True)
@@ -334,58 +340,70 @@ def image_normalize_func(
334340
beta: float,
335341
norm_type: str,
336342
ext: str,
343+
verbose: bool,
337344
) -> str:
338-
import json
345+
try:
346+
import json
339347

340-
import cv2 as cv # type: ignore
341-
import numpy as np
342-
import requests
343-
from requests import adapters
348+
import cv2 as cv # type: ignore
349+
import numpy as np
350+
import requests
351+
from requests import adapters
344352

345-
session = requests.Session()
346-
session.mount("https://", adapters.HTTPAdapter(max_retries=3))
353+
session = requests.Session()
354+
session.mount("https://", adapters.HTTPAdapter(max_retries=3))
347355

348-
ext = ext or ".jpeg"
356+
ext = ext or ".jpeg"
349357

350-
norm_type_mapping = {
351-
"inf": cv.NORM_INF,
352-
"l1": cv.NORM_L1,
353-
"l2": cv.NORM_L2,
354-
"minmax": cv.NORM_MINMAX,
355-
}
358+
norm_type_mapping = {
359+
"inf": cv.NORM_INF,
360+
"l1": cv.NORM_L1,
361+
"l2": cv.NORM_L2,
362+
"minmax": cv.NORM_MINMAX,
363+
}
356364

357-
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
358-
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
365+
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
366+
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
359367

360-
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
361-
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
368+
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
369+
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
362370

363-
response = session.get(src_url, timeout=30)
364-
bts = response.content
371+
response = session.get(src_url, timeout=30)
372+
bts = response.content
365373

366-
nparr = np.frombuffer(bts, np.uint8)
367-
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
368-
img_normalized = cv.normalize(
369-
img, None, alpha=alpha, beta=beta, norm_type=norm_type_mapping[norm_type]
370-
)
374+
nparr = np.frombuffer(bts, np.uint8)
375+
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
376+
img_normalized = cv.normalize(
377+
img, None, alpha=alpha, beta=beta, norm_type=norm_type_mapping[norm_type]
378+
)
371379

372-
bts = cv.imencode(ext, img_normalized)[1].tobytes()
380+
bts = cv.imencode(ext, img_normalized)[1].tobytes()
373381

374-
ext = ext.replace(".", "")
375-
ext_mappings = {"jpg": "jpeg", "tif": "tiff"}
376-
ext = ext_mappings.get(ext, ext)
377-
content_type = "image/" + ext
382+
ext = ext.replace(".", "")
383+
ext_mappings = {"jpg": "jpeg", "tif": "tiff"}
384+
ext = ext_mappings.get(ext, ext)
385+
content_type = "image/" + ext
378386

379-
session.put(
380-
url=dst_url,
381-
data=bts,
382-
headers={
383-
"Content-Type": content_type,
384-
},
385-
timeout=30,
386-
)
387+
session.put(
388+
url=dst_url,
389+
data=bts,
390+
headers={
391+
"Content-Type": content_type,
392+
},
393+
timeout=30,
394+
)
395+
if verbose:
396+
result_dict = {"status": "", "content": dst_obj_ref_rt}
397+
return json.dumps(result_dict)
398+
else:
399+
return dst_obj_ref_rt
387400

388-
return dst_obj_ref_rt
401+
except Exception as e:
402+
if verbose:
403+
result_dict = {"status": str(e), "content": None}
404+
return json.dumps(result_dict)
405+
else:
406+
return None
389407

390408

391409
image_normalize_def = FunctionDef(
@@ -394,7 +412,12 @@ def image_normalize_func(
394412

395413

396414
def image_normalize_to_bytes_func(
397-
src_obj_ref_rt: str, alpha: float, beta: float, norm_type: str, ext: str
415+
src_obj_ref_rt: str,
416+
alpha: float,
417+
beta: float,
418+
norm_type: str,
419+
ext: str,
420+
verbose: bool,
398421
) -> str:
399422
try:
400423
import base64
@@ -429,13 +452,22 @@ def image_normalize_to_bytes_func(
429452
img, None, alpha=alpha, beta=beta, norm_type=norm_type_mapping[norm_type]
430453
)
431454
bts = cv.imencode(".jpeg", img_normalized)[1].tobytes()
432-
result_dict = {"status": "", "content": base64.b64encode(bts).decode("utf-8")}
433455

434-
except Exception as e:
435-
result_dict = {"status": str(e), "content": ""}
456+
if verbose:
457+
content_b64 = base64.b64encode(bts).decode("utf-8")
458+
result_dict = {"status": "", "content": content_b64}
459+
result_json = json.dumps(result_dict)
460+
return result_json
461+
else:
462+
return bts
436463

437-
result_json = json.dumps(result_dict)
438-
return result_json
464+
except Exception as e:
465+
if verbose:
466+
result_dict = {"status": str(e), "content": b""}
467+
result_json = json.dumps(result_dict)
468+
return result_json
469+
else:
470+
return b""
439471

440472

441473
image_normalize_to_bytes_def = FunctionDef(

0 commit comments

Comments
 (0)