File size: 774 Bytes
52a9452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from PIL import Image
import cv2
import base64
import io
import numpy as np


def convert(data):
    if isinstance(data, dict):
        ndata = {}
        for key, value in data.items():
            nkey = key.decode()
            if nkey == 'img':
                img = Image.open(io.BytesIO(value))
                img = img.convert('RGB')
                img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
                nvalue = img
            else:
                nvalue = convert(value)
            ndata[nkey] = nvalue
        return ndata
    elif isinstance(data, list):
        return [convert(item) for item in data]
    elif isinstance(data, bytes):
        return data.decode()
    else:
        return data


def to_np(x):
    return x.cpu().data.numpy()