Python Example

This example demonstrates how to use the epc_tofcam_native Python module to capture and visualize DCS, amplitude and distance frames from the EPC670 ToF camera using matplotlib.

python example result

Prerequisites

  • A connected EPC670 ToF camera module

  • The epc_tofcam_native Python package installed

  • matplotlib installed:

pip install matplotlib epc-tofcam-native

Run

python3 python_example.py

A matplotlib window will open showing the amplitude and distance images of a single captured frame.

How It Works

  1. Open the camera with cam.open().

  2. Configure the exposure time and modulation frequency via cam.setControl().

  3. Start the stream with cam.startStream() to enable continuous frame acquisition.

  4. Capture a single frame with cam.captureFrame() and extract the DCS, amplitude and distance data using frame.get().

  5. Stop the stream with cam.stopStream() and close the connection with cam.close().

  6. The captured data is displayed in a matplotlib figure with all four DCS images on the top row and amplitude and distance images on the bottom.

Full Source Code

 1"""
 2This sample demonstrates how to use the epc_tofcam_native module to capture and
 3visualize data from an EPC TOF camera in Python. It connects to the camera,
 4configures settings, captures a frame, and then uses matplotlib to display the
 5DCS, amplitude, and distance images."""
 6
 7import matplotlib.pyplot as plt
 8from epc_tofcam_native import TOFCam, TOFControl, FrameType, AcquisitionMode
 9
10MINIMAL_AMPLITUDE = 80  # Minimum amplitude threshold for valid distance measurements
11MODULATION_FREQUENCY_HZ = 10000000  # Modulation frequency in Hz
12HDR_INTEGRATION_TIMES_US = [40, 400, 4000]  # HDR integration times in microseconds
13
14
15# connect to the TOF hat
16cam = TOFCam()
17cam.open()
18
19# Setup HDR acquisition
20cam.setControl(TOFControl.ACQUISITION_MODE, AcquisitionMode.DIST_AMP_HDR)
21cam.setControl(TOFControl.HDR_INTEGRATION_TIME1_US, HDR_INTEGRATION_TIMES_US[0])
22cam.setControl(TOFControl.HDR_INTEGRATION_TIME2_US, HDR_INTEGRATION_TIMES_US[1])
23cam.setControl(TOFControl.HDR_INTEGRATION_TIME3_US, HDR_INTEGRATION_TIMES_US[2])
24
25# set modulation frequency
26cam.setControl(TOFControl.MODULATION_FREQUENCY_HZ, MODULATION_FREQUENCY_HZ)
27cam.setControl(TOFControl.MIN_AMPLITUDE, MINIMAL_AMPLITUDE)
28
29
30# Start capturing new frames continuously.
31# While single frames can be captured even outside this mode,
32# it is required to achieve the maximum framerate of the camera.
33cam.startStream()
34
35# capture a single frame and extract data
36frame = cam.captureFrame()
37
38amplitude = frame.get(FrameType.AMPLITUDE)
39distance = frame.get(FrameType.DISTANCE)
40
41metadata = frame.getMetadata()
42timestamp = metadata.timestamp
43frame_number = metadata.frameNumber
44
45# stop acquiring new frames
46cam.stopStream()
47
48# close connection to TOF hat
49cam.close()
50
51# plot all images in matplotlib
52fig = plt.figure(figsize=(12, 6))
53ax = fig.add_subplot(1, 2, 1)
54img = ax.imshow(amplitude)
55plt.colorbar(img)
56ax.set_title("Amplitude")
57ax.set_xticks([])
58ax.set_yticks([])
59
60ax = fig.add_subplot(1, 2, 2)
61img = ax.imshow(distance, vmin=0, vmax=2000, cmap="jet")
62plt.colorbar(img)
63ax.set_title("Distance")
64ax.set_xticks([])
65ax.set_yticks([])
66
67plt.show()