Skip to content

Commit

Permalink
Samples: Add CaptureWithSettingsFromJSON sample
Browse files Browse the repository at this point in the history
Add a new sample which reads settings from a .json file and captures an
image in Hdev.
  • Loading branch information
csu-bot-zivid authored and chrisasc committed Nov 14, 2023
1 parent a67b479 commit 3c56216
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ from the camera can be used.
configured settings for each frame.
- [CaptureSavePLY](https://github.com/zivid/zivid-halcon-samples/tree/master//source/Camera/Basic/CaptureSavePLY.hdev) - Capture a 3D color point cloud from the camera and save it
to a PLY file format.
- [CaptureWithSettingsFromJSON](https://github.com/zivid/zivid-halcon-samples/tree/master//source/Camera/Basic/CaptureWithSettingsFromJSON.hdev) - Capture a 3D color point cloud from the camera and use it
to generate a HALCON ObjectModel3D which is then visualized.
- [ConnectToSerialNumberCamera](https://github.com/zivid/zivid-halcon-samples/tree/master//source/Camera/Basic/ConnectToSerialNumberCamera.hdev) - Connect to a specific Zivid 3D camera based on its serial
number.
- **Advanced**
Expand Down
73 changes: 73 additions & 0 deletions source/Camera/Basic/CaptureWithSettingsFromJSON.hdev
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<hdevelop file_version="1.2" halcon_version="21.11.0.0">
<procedure name="main">
<interface/>
<body>
<c>*</c>
<c>* Capture a 3D color point cloud from the camera and use it to generate a HALCON ObjectModel3D which is then visualized.</c>
<c>*</c>
<c>* Please note that you first need to install Zivid Software and correctly set the environment variables. After this, you can access the camera with the HALCON</c>
<c>* GenICamTL interface.</c>
<c>*</c>
<c/>
<c>* Preparing graphics windows</c>
<l>dev_close_window ()</l>
<l>WinWidth := 960</l>
<l>WinHeight := 800</l>
<l>dev_open_window (0, 0, WinWidth, WinHeight, 'blue', Window3D)</l>
<l>dev_open_window (0, WinWidth, WinWidth, WinHeight, 'black', Window2D)</l>
<c/>
<c>*Getting first available Zivid device</c>
<l>info_framegrabber ('GenICamTL','device', Information, Devices)</l>
<l>import './../../Procedures'</l>
<l>get_first_available_zivid_device (Devices, Device)</l>
<c/>
<c>* Connecting to Zivid camera</c>
<l>open_framegrabber ('GenICamTL',1, 1, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', Device, 0, 0, AcqHandle)</l>
<c/>
<c>* Loading settings from file</c>
<l>get_zivid_sample_data_dir(ZividDataDir)</l>
<l>get_framegrabber_param (AcqHandle, 'DeviceModelName', DeviceModelName)</l>
<l>get_zivid_model_folder_name (DeviceModelName, DeviceModelFolder)</l>
<l>read_dict (ZividDataDir + '/SettingsJson/' + DeviceModelFolder + '/Settings01.json', [], [], JsonSettings)</l>
<l>read_zivid_json_params (AcqHandle, JsonSettings)</l>
<c/>
<c>* Configuring acquisition</c>
<l>set_framegrabber_param (AcqHandle, 'create_objectmodel3d', 'enable')</l>
<l>set_framegrabber_param (AcqHandle, 'add_objectmodel3d_overlay_attrib', 'enable')</l>
<l>set_framegrabber_param (AcqHandle, 'grab_timeout', -1)</l>
<c/>
<c>* Capturing frame</c>
<l>grab_data (Image, Region, Contours, AcqHandle, ObjectModel3D)</l>
<c/>
<c>* Extracting the following images: X, Y, Z, SNR, and RGB</c>
<l>select_obj (Image, X, 1)</l>
<l>select_obj (Image, Y, 2)</l>
<l>select_obj (Image, Z, 3)</l>
<l>select_obj (Image, Confidence, 4)</l>
<l>select_obj (Image, RGBA, 5)</l>
<c/>
<c>* Displaying 2D color image</c>
<l>dev_set_window (Window2D)</l>
<l>dev_display (RGBA)</l>
<c/>
<c>* Displaying 3D color point cloud, press the button to continue</c>
<l>Instructions[0] := 'Rotate: Left button'</l>
<l>Instructions[1] := 'Zoom: Shift + left button'</l>
<l>Instructions[2] := 'Move: Ctrl + left button'</l>
<l>visualize_object_model_3d (Window3D, ObjectModel3D, [], [], ['red_channel_attrib','green_channel_attrib','blue_channel_attrib'], ['&amp;overlay_red','&amp;overlay_green','&amp;overlay_blue'], [], [], [Instructions], PoseOut)</l>
<c/>
<c>* Disconnecting from Zivid Camera</c>
<l>close_framegrabber (AcqHandle)</l>
<c/>
<c>* Closing graphics windows</c>
<l>dev_set_window (Window2D)</l>
<l>dev_close_window ()</l>
<l>dev_set_window (Window3D)</l>
<l>dev_close_window ()</l>
</body>
<docu id="main">
<parameters/>
</docu>
</procedure>
</hdevelop>
18 changes: 18 additions & 0 deletions source/Procedures/read_zivid_json_params.hdvp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
<l> set_framegrabber_param (AcqHandle, 'AddAcquisition', 1)</l>
<l>endfor</l>
<c/>
<c>* Engine</c>
<l>get_dict_tuple (Settings, 'Experimental', Experimental)</l>
<l>get_dict_tuple (Experimental, 'Engine', Engine)</l>
<l>set_framegrabber_param (AcqHandle, 'ExperimentalEngine', Engine)</l>
<c/>
<c>* Sampling</c>
<l>get_dict_tuple (Settings, 'Sampling', Sampling)</l>
<l>get_dict_tuple (Sampling, 'Color', SamplingColor)</l>
<l>get_dict_tuple (Sampling, 'Pixel', SamplingPixel)</l>
<l>set_framegrabber_param (AcqHandle, 'SamplingColor', SamplingColor)</l>
<l>set_framegrabber_param (AcqHandle, 'SamplingPixel', SamplingPixel)</l>
<c/>
<c>* RegionOfInterest</c>
<l>get_dict_tuple (Settings, 'RegionOfInterest', RegionOfInterest)</l>
<c/>
Expand Down Expand Up @@ -99,6 +111,12 @@
<l>set_framegrabber_param (AcqHandle, 'ProcessingFiltersNoiseRemovalEnabled', Enabled)</l>
<l>get_dict_tuple (Removal, 'Threshold', Threshold)</l>
<l>set_framegrabber_param (AcqHandle, 'ProcessingFiltersNoiseRemovalThreshold', Threshold)</l>
<l>get_dict_tuple (Noise, 'Suppression', Suppression)</l>
<l>get_dict_tuple (Suppression, 'Enabled', Enabled)</l>
<l>set_framegrabber_param (AcqHandle, 'ProcessingFiltersNoiseSuppressionEnabled', Enabled)</l>
<l>get_dict_tuple (Noise, 'Repair', Repair)</l>
<l>get_dict_tuple (Repair, 'Enabled', Enabled)</l>
<l>set_framegrabber_param (AcqHandle, 'ProcessingFiltersNoiseRepairEnabled', Enabled)</l>
<c/>
<c>* Outlier Filter</c>
<l>get_dict_tuple (Filters, 'Outlier', Outlier)</l>
Expand Down
21 changes: 19 additions & 2 deletions source/SampleUtils/yaml_settings_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def _args() -> argparse.Namespace:
return parser.parse_args()


def _convert_to_halcon_format(yaml_object: dict) -> None:
"""Recursively search through a dictionary and convert necessary values to Halcon-supported format.
Args:
yaml_object: YAML loaded as a dictionary
"""
for key, value in yaml_object.items():
if value == "yes":
yaml_object[key] = 1
elif value == "no":
yaml_object[key] = 0
elif isinstance(value, dict):
_convert_to_halcon_format(value)


def _main() -> None:
input_directory = _args().directory
if not input_directory.is_dir():
Expand All @@ -31,8 +47,9 @@ def _main() -> None:
for x in list(_args().directory.glob("*.yml")):
output_file = output_directory / f"{x.stem}.json"
with open(x, "r", encoding="utf-8") as yaml_in, open(output_file, "w", encoding="utf-8") as json_out:
yaml_object = yaml.safe_load(yaml_in)
json.dump(yaml_object, json_out, indent=2)
yaml_dict = yaml.safe_load(yaml_in)
_convert_to_halcon_format(yaml_dict)
json.dump(yaml_dict, json_out, indent=4)


if __name__ == "__main__":
Expand Down

0 comments on commit 3c56216

Please sign in to comment.