Home > front end >  Import opencv mat in python (compressed with c#)
Import opencv mat in python (compressed with c#)

Time:09-22

I've problems decoding/unzipping a string in python which was orginially compressed using C# with "GZipStream".

The "opencv mat" was converted to json with:

public override void WriteJson(JsonWriter writer, Mat value, JsonSerializer serializer)
{
    byte[] matBytes = null;
    Cv2.ImEncode(".png", value, out matBytes);

    // Convert byte[] to Base64 String
    string matBase64 = Convert.ToBase64String(matBytes);

    var resCompressed = Utils.StringCompressor.CompressString(matBase64);
    writer.WriteValue(resCompressed);
}

An example string "jsontoken" to uncompress is:

uAoAAB LCAAAAAAABADtls3SsrgWhS IASogOOiB/GgS/gkqMlMUgkFEEGK8 o7v1931DU7VqTqzU9UMfDS1srPY2dmx3pthwmbutnqsxRPgHXF2lfj28xNU1vrniceHr34HPrPGifeJur4q5nqHdocErlnx0YH8eA2fZwhrsz4q2epTEWRSyjESqDjO0VogzpFPK/yDWgD69I5jJLDFKYLOL1ABGyFKLYGfIOa/sf6XWJGbAaDW9kzmUryc6FDcpQMGmbYGaKIGka8J4ZmGYFnC9co1ahpOs JKpXRhE6O6zQg/qtdIzLuNp9YGmgWgfr8wTcImDzVaBAF3wkTxU5KP2MSRflTPZ1rsbtInBAttu4HRqLPTNX5jW78bfkudHZEWEphoOKYyOtsKTzKk3GU2N9o5z7Xe8OGYeih8L 4c7JEy7hi2NM8gS3DIUELbQv0gv6f9x1ZjHZq7RtKzdakBSCflpG4U KaNsVTWkrYZ7hMIl24GW7phmpLUmmP0Ys0c9NT7vAogU79OpvDFbmPco4PQ4KdmGo vBnlCQwpPplaNp3DF5oudzMchWFF5J5bvi3DVhMazF9qoQa6heZal4732OTjmguqfeoBgQG27OcZRnpYuZ2zJ72PSkPecBkXr2XSCeQczcDmJBMVT GSknjXXS0DsVfcTEOrWXmMHJ13sJl4N/nPoztQ8Xva5pSCLLXW OeH2vTjQ04xGn2GA8xcq2/Mx6U5YDjG71ayRioaAVYOM15vAy3/gILj6RbQXfEZ22rtYW19Bd4gbHhvhwWi2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R Gbfv6AmSnaa01NUVdUff/wJwhGNxbgKAAA=

The according C# code to load this data is:

private static void CreateDataFromJsonZip(ref ThresholdMask mask, JObject jsonObj, string maskName)
{
    // get JSON result objects into a list
    JToken jsonToken = jsonObj["Selection"]; # jsonToken

    var res = Utils.StringCompressor.DecompressString(jsonToken.ToString());
    byte[] data = Convert.FromBase64String(res);
    mask.Selection = Cv2.ImDecode(data, ImreadModes.Unchanged);

My attempt to read the data in python is:

jsonToken = r'uAoAAB LCAAAAAAABADtls3SsrgWhS IASogOOiB/GgS/gkqMlMUgkFEEGK8 o7v1931DU7VqTqzU9UMfDS1srPY2dmx3pthwmbutnqsxRPgHXF2lfj28xNU1vrniceHr34HPrPGifeJur4q5nqHdocErlnx0YH8eA2fZwhrsz4q2epTEWRSyjESqDjO0VogzpFPK/yDWgD69I5jJLDFKYLOL1ABGyFKLYGfIOa/sf6XWJGbAaDW9kzmUryc6FDcpQMGmbYGaKIGka8J4ZmGYFnC9co1ahpOs JKpXRhE6O6zQg/qtdIzLuNp9YGmgWgfr8wTcImDzVaBAF3wkTxU5KP2MSRflTPZ1rsbtInBAttu4HRqLPTNX5jW78bfkudHZEWEphoOKYyOtsKTzKk3GU2N9o5z7Xe8OGYeih8L 4c7JEy7hi2NM8gS3DIUELbQv0gv6f9x1ZjHZq7RtKzdakBSCflpG4U KaNsVTWkrYZ7hMIl24GW7phmpLUmmP0Ys0c9NT7vAogU79OpvDFbmPco4PQ4KdmGo vBnlCQwpPplaNp3DF5oudzMchWFF5J5bvi3DVhMazF9qoQa6heZal4732OTjmguqfeoBgQG27OcZRnpYuZ2zJ72PSkPecBkXr2XSCeQczcDmJBMVT GSknjXXS0DsVfcTEOrWXmMHJ13sJl4N/nPoztQ8Xva5pSCLLXW OeH2vTjQ04xGn2GA8xcq2/Mx6U5YDjG71ayRioaAVYOM15vAy3/gILj6RbQXfEZ22rtYW19Bd4gbHhvhwWi2dDQu884NUGBourU74/P3XdmBTp92QHRAURsd0yDHkRcc0/1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onToken
original = base64.b64decode(string)
convert_to_np = np.frombuffer(original, dtype=np.uint8)
print(convert_to_np)
len(convert_to_np)
img = cv2.imdecode(convert_to_np, cv2.IMREAD_UNCHANGED)

Unfortunately, the last step with imdecode does not work, the result is None. Any hints?

CodePudding user response:

This will do exactly what you asked for, following the StringCompressor.DecompressString code from this SO answer (the decompressed length is stored in the first 4 bytes):

original = base64.b64decode(gzip.decompress(base64.b64decode(jsonToken)[4:]))

However, I'd recommend simplifying your C# implementation. Starting from the raw OpenCV mat, you:

  1. encode it as a PNG image
  2. convert that binary data to base64 string
  3. compress the text string with gzip
  4. convert to base64 again
  5. store it to the json file

Step 2 is completely unnecessary, and step 3 is redundant, since the PNG encoding process already compresses the image data. Adding another layer of compression won't help much, and may even make it worse.

I would just convert to a PNG, encode with base64, and write that to the json file. Then your original Python code would work as expected.

  • Related