20 lines
445 B
Python
20 lines
445 B
Python
# coding=utf-8
|
|
import base64
|
|
|
|
import numpy as np
|
|
|
|
|
|
def feature_to_blob(feature):
|
|
arr = np.asarray(feature, dtype=np.float32)
|
|
return base64.b64encode(arr.tobytes()).decode('ascii')
|
|
|
|
|
|
def blob_to_feature(blob):
|
|
if not blob:
|
|
return None
|
|
try:
|
|
raw = base64.b64decode(blob.encode('ascii') if isinstance(blob, str) else blob)
|
|
return np.frombuffer(raw, dtype=np.float32)
|
|
except Exception:
|
|
return None
|