zivid / zivid-python-samples

Python code samples for Zivid
https://zivid.com
BSD 3-Clause "New" or "Revised" License
37 stars 14 forks source link

trouble creating ply file #45

Closed pbermell closed 4 years ago

pbermell commented 4 years ago

Hello, I am having trouble in creating a ply file from a zdf file.

this is my code:

import numpy as np
import zivid  # using version 0.9.2.1.7.0
import struct

app = zivid.Application()

filename_zdf = 'shot_20190828-135107.zdf'

print(f"Reading {filename_zdf} point cloud")

frame = zivid.Frame(filename_zdf)
print(frame)
point_cloud = frame.get_point_cloud().to_array()
print(point_cloud)

# frame.save('new_ply_shot.ply') # this creates an empty file with ply headers:
# ply
# format binary_little_endian 1.0
# comment Zivid Point Cloud
# element vertex

with open('new_ply_file.ply', "wb") as file_pointer:
    line = "ply\n"
    file_pointer.write(line.encode("utf-8"))
    line = "format binary_little_endian 1.0\n"
    file_pointer.write(line.encode("utf-8"))
    line = "element vertex %d\n"
    file_pointer.write(line.encode("utf-8") % point_cloud.shape[0])
    line = "property float x\n"
    file_pointer.write(line.encode("utf-8"))
    line = "property float y\n"
    file_pointer.write(line.encode("utf-8"))
    line = "property float z\n"
    file_pointer.write(line.encode("utf-8"))
    line = "property uchar red\n"
    file_pointer.write(line.encode("utf-8"))
    line = "property uchar green\n"
    file_pointer.write(line.encode("utf-8"))
    line = "property uchar blue\n"
    file_pointer.write(line.encode("utf-8"))
    line = "end_header\n"
    file_pointer.write(line.encode("utf-8"))

    for i in range(len(point_cloud)):
        data = struct.pack(
            "<fffBBB",
            point_cloud[i, 0],
            point_cloud[i, 1],
            point_cloud[i, 2],
            np.uint8(point_cloud[i, 3]),
            np.uint8(point_cloud[i, 4]),
            np.uint8(point_cloud[i, 5]),
        )

        file_pointer.write(data)

and this is the output:

Reading shot_20190828-135107.zdf point cloud
{ Settings: Settings:
  Bidirectional: no
  BlueBalance: 1.080994
  Brightness: 1.000000
  ExposureTime: 8333
  Filters:
    Contrast:
      Enabled: yes
      Threshold: 5.000000
    Gaussian:
      Enabled: no
      Sigma: 1.500000
    Outlier:
      Enabled: yes
      Threshold: 5.000000
    Reflection:
      Enabled: no
    Saturated:
      Enabled: yes
  Gain: 2.000000
  Iris: 22
  RedBalance: 1.709000, CameraState: CameraState:
  Available: no
  Connected: yes
  Live: no
  Temperature:
    DMD: 31.625000
    General: 32.375000
    LED: 30.483894
    Lens: 32.375000
    PCB: 39.125000, FrameInfo: FrameInfo:
  SoftwareVersion:
    ZividAPI:
      Comments: NA
      FileDescription: ZividAPI
      FileVersion: NA
      ProductVersion: NA
    ZividCameraFile:
      Comments: NA
      FileDescription: ZividCameraFile
      FileVersion: NA
      ProductVersion: NA
    ZividCameraToshibaTeli:
      Comments: NA
      FileDescription: ZividCameraToshibaTeli
      FileVersion: NA
      ProductVersion: NA
    ZividStudio:
      Comments: NA
      FileDescription: ZividStudio
      FileVersion: NA
      ProductVersion: NA
  TimeStamp: 2019-08-28 13:51:08 }
[[(       nan,        nan,       nan, 13.6565    , 49, 40, 15, 255)
  (       nan,        nan,       nan,  9.939818  , 49, 41, 16, 255)
  (       nan,        nan,       nan, 13.546217  , 50, 40, 16, 255) ...
  (       nan,        nan,       nan,  0.5477225 ,  9, 15, 10, 255)
  (       nan,        nan,       nan,  0.44721356,  9, 14, 10, 255)
  (       nan,        nan,       nan,  0.89442724,  9, 15, 11, 255)]
 [(       nan,        nan,       nan,  9.471008  , 50, 41, 16, 255)
  (       nan,        nan,       nan,  3.8340578 , 49, 40, 16, 255)
  (-436.47256, -262.30704, 1204.7329,  9.762172  , 50, 41, 16, 255) ...
  (       nan,        nan,       nan,  0.83666   ,  9, 15, 11, 255)
  (       nan,        nan,       nan,  0.8944271 ,  8, 15, 10, 255)
  (       nan,        nan,       nan,  0.44721356,  9, 15, 11, 255)]
 [(       nan,        nan,       nan, 13.953494  , 50, 40, 16, 255)
  (-437.44824, -262.16626, 1206.1522, 10.8074045 , 49, 39, 15, 255)
  (-436.78793, -262.0335 , 1206.5846, 13.516656  , 51, 41, 17, 255) ...
  (       nan,        nan,       nan,  0.5477226 ,  9, 16, 11, 255)
  (       nan,        nan,       nan,  0.5477226 ,  9, 15, 10, 255)
  (       nan,        nan,       nan,  1.1401755 ,  8, 15, 10, 255)]
 ...
 [(       nan,        nan,       nan,  1.        , 13, 17, 11, 255)
  (       nan,        nan,       nan,  0.83666   , 13, 17, 12, 255)
  (       nan,        nan,       nan,  0.4472136 , 13, 18, 12, 255) ...
  (       nan,        nan,       nan,  0.83666   ,  8, 12,  9, 255)
  (       nan,        nan,       nan,  0.5477226 ,  7, 12,  8, 255)
  (       nan,        nan,       nan,  0.70710677,  7, 11,  8, 255)]
 [(       nan,        nan,       nan,  0.44721356, 13, 16, 11, 255)
  (       nan,        nan,       nan,  0.83666   , 13, 17, 11, 255)
  (       nan,        nan,       nan,  0.5477226 , 13, 17, 11, 255) ...
  (       nan,        nan,       nan,  0.4472136 ,  8, 12,  8, 255)
  (       nan,        nan,       nan,  0.5477225 ,  8, 12,  8, 255)
  (       nan,        nan,       nan,  0.8944271 ,  8, 12,  7, 255)]
 [(       nan,        nan,       nan,  0.83666   , 11, 15,  9, 255)
  (       nan,        nan,       nan,  1.1401755 , 12, 16, 11, 255)
  (       nan,        nan,       nan,  0.8944272 , 12, 17, 11, 255) ...
  (       nan,        nan,       nan,  1.095445  ,  8, 12,  9, 255)
  (       nan,        nan,       nan,  0.44721356,  8, 12,  8, 255)
  (       nan,        nan,       nan,  0.70710677,  6, 11,  7, 255)]]
Traceback (most recent call last):
  File "/home/xtcdev/DEV/defectnet/venv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3319, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-fae442df4333>", line 1, in <module>
    runfile('/home/xtcdev/DEV/defectnet/ZDF_processorv02.py', wdir='/home/xtcdev/DEV/defectnet')
  File "/snap/pycharm-professional/173/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/snap/pycharm-professional/173/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/xtcdev/DEV/defectnet/ZDF_processorv02.py", line 52, in <module>
    np.uint8(point_cloud[i, 3]),
ValueError: Can't cast from structure to non-structure, except if the structure only has a single field.
torbsorb commented 4 years ago

Hi @pbermell,

Looks like you tried frame.save('new_ply_shot.ply'), which is what we do in convert_zdf.py. First of all I'm curious why you get an empty output. If you can share your shot_20190828-135107.zdf then I can try it on my PC.

Then it looks like you've reverted to the method of zdf_2_ply_without_zivid.py (which we actually plan to deprecate). You're missing a few steps however. Most importantly, in the .zdf we have a 7th value in the point cloud (Contrast, in addition to XYZ and RGB). The .ply should not have that, so you have to deal with that somehow. Maybe it's sufficient to change indexing to:

            point_cloud[i, 2],
# Contrast is located at point_cloud[i, 3]
            np.uint8(point_cloud[i, 4]),
            np.uint8(point_cloud[i, 5]),
            np.uint8(point_cloud[i, 6]),

However, I'd rather recommend you fully use zdf_2_ply_without_zivid.py.

pbermell commented 4 years ago

hi @torbsorb

shot_20190828-135107.zdf is a 20 Mb file and it can be opened using zivid studio. if you have a place where to upload the file I can do it.

on the other hand, the content of the empty file looks like this:f

ply
format binary_little_endian 1.0
comment Zivid Point Cloud
element vertex 

Indeed I had tried to play with the indexing before as you recommended but I get the same error.

any further ideas?

pablo

torbsorb commented 4 years ago

Did you try zdf_2_ply_without_zivid.py?

Can you share your files with https://wetransfer.com/?

pbermell commented 4 years ago

It all works now using zdf_2_ply_without_zivid.py thanks and sorry to waste your time.

torbsorb commented 4 years ago

Glad we could help! Please send you capture (if you can) so that we can investigate why frame.save didn't work.