Skip to content

Commit aecd695

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

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)
@@ -333,58 +339,70 @@ def image_normalize_func(
333339
beta: float,
334340
norm_type: str,
335341
ext: str,
342+
verbose: bool,
336343
) -> str:
337-
import json
344+
try:
345+
import json
338346

339-
import cv2 as cv # type: ignore
340-
import numpy as np
341-
import requests
342-
from requests import adapters
347+
import cv2 as cv # type: ignore
348+
import numpy as np
349+
import requests
350+
from requests import adapters
343351

344-
session = requests.Session()
345-
session.mount("https://", adapters.HTTPAdapter(max_retries=3))
352+
session = requests.Session()
353+
session.mount("https://", adapters.HTTPAdapter(max_retries=3))
346354

347-
ext = ext or ".jpeg"
355+
ext = ext or ".jpeg"
348356

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

356-
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
357-
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
364+
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
365+
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
358366

359-
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
360-
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
367+
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
368+
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
361369

362-
response = session.get(src_url, timeout=30)
363-
bts = response.content
370+
response = session.get(src_url, timeout=30)
371+
bts = response.content
364372

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

371-
bts = cv.imencode(ext, img_normalized)[1].tobytes()
379+
bts = cv.imencode(ext, img_normalized)[1].tobytes()
372380

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

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

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

389407

390408
image_normalize_def = FunctionDef(
@@ -393,7 +411,12 @@ def image_normalize_func(
393411

394412

395413
def image_normalize_to_bytes_func(
396-
src_obj_ref_rt: str, alpha: float, beta: float, norm_type: str, ext: str
414+
src_obj_ref_rt: str,
415+
alpha: float,
416+
beta: float,
417+
norm_type: str,
418+
ext: str,
419+
verbose: bool,
397420
) -> str:
398421
try:
399422
import base64
@@ -428,13 +451,22 @@ def image_normalize_to_bytes_func(
428451
img, None, alpha=alpha, beta=beta, norm_type=norm_type_mapping[norm_type]
429452
)
430453
bts = cv.imencode(".jpeg", img_normalized)[1].tobytes()
431-
result_dict = {"status": "", "content": base64.b64encode(bts).decode("utf-8")}
432454

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

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

439471

440472
image_normalize_to_bytes_def = FunctionDef(

0 commit comments

Comments
 (0)