Skip to content

Commit

Permalink
Fixed python example for u3v
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-iizuka committed Jan 12, 2024
1 parent eba8caa commit 8efb0fa
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 291 deletions.
28 changes: 12 additions & 16 deletions example/u3v_camera1_opencv/u3v_camera1_opencv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const int32_t height = 480;
double gain = 400;
double exposure = 400;

int positive_pow(int base, int expo){
if (expo <= 0){
int positive_pow(int base, int expo) {
if (expo <= 0) {
return 1;
}
if (expo == 1){
if (expo == 1) {
return base;
}else{
} else {
return base * positive_pow(base, expo-1);
}
}
Expand All @@ -41,25 +41,20 @@ int main(int argc, char *argv[])
Builder b;

// Set the target hardware. The default is CPU.
b.set_target(Halide::get_host_target());
b.set_target(ion::get_host_target());

// Load standard building block
b.with_bb_module("ion-bb");

Buffer<double> gainb(1);
gainb.fill(gain);

Buffer<double> exposureb(1);
exposureb.fill(gain);

// Connect the input port to the Node instance created by b.add().
Node n = b.add("image_io_u3v_cameraN_u16x2")(gainb, exposureb)
Node n = b.add("image_io_u3v_cameraN_u16x2")(&gain, &exposure)
.set_param(
Param("num_devices", 1),
Param("frame_sync", "false"),
Param("frame_sync", false),
Param("gain_key", FEATURE_GAIN_KEY),
Param("exposure_key", FEATURE_EXPOSURE_KEY),
Param("realtime_diaplay_mode", "true")
Param("realtime_diaplay_mode", true),
Param("enable_control", true)
);

// Map output buffer and ports by using Port::bind.
Expand All @@ -74,6 +69,7 @@ int main(int argc, char *argv[])

// Obtain image data continuously for 100 frames to facilitate operation check.
int loop_num = 100;
int coef = positive_pow(2, NUM_BIT_SHIFT);
for (int i = 0; i < loop_num; ++i)
{
// JIT compilation and execution of pipelines with Builder.
Expand All @@ -83,7 +79,7 @@ int main(int argc, char *argv[])
cv::Mat A(height, width, CV_16UC1, output.data());

// Depends on sensor image pixel format, apply bit shift on images
A = A * positive_pow(2, NUM_BIT_SHIFT);
A = A * coef;

// Display the image
cv::imshow("A", A);
Expand All @@ -93,7 +89,7 @@ int main(int argc, char *argv[])
cv::waitKey(1);
}

} catch (const Halide::Error& e) {
} catch (const ion::Error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
Expand Down
71 changes: 26 additions & 45 deletions example/u3v_camera1_opencv/u3v_camera1_opencv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
feature_exposure_key = 'ExposureTime'
num_bit_shift = 0

if platform == "win32":
module_name = 'ion-bb.dll'
elif platform == "darwin":
module_name = 'libion-bb.dylib'
else:
module_name = 'libion-bb.so'

if __name__ == "__main__":

# Define parameters
Expand All @@ -25,75 +18,63 @@

# Build the pipeline by adding nodes to this builder.
builder = Builder()
# Set the target hardware, The default is CPU.

# Set the target hardware, The default is CPU.
builder.set_target('host')
# Load building block module from the library
builder.with_bb_module(module_name)

# Load building block module from the library
builder.with_bb_module("ion-bb")

# Define Input Port
# Port class would be used to define dynamic O/O for each node.
t = Type(TypeCode.Uint, 1, 1)
dispose_p = Port('dispose', t, 0)
t = Type(TypeCode.Float, 64, 1)
gain0_p = Port('gain0', t, 0)
exposure0_p = Port('exposure0', t, 0)

# Params
pixel_format_ptr = Param('pixel_format_ptr', 'Mono12')
frame_sync = Param('frame_sync', 'true')
num_devices = Param('num_devices', '1')
frame_sync = Param('frame_sync', 'false')
gain_key = Param('gain_key', feature_gain_key)
exposure_key = Param('exposure_key', feature_exposure_key)
realtime_diaplay_mode = Param('realtime_diaplay_mode', 'true')

enable_control = Param('enable_control', 'true')

# Add node and connect the input port to the node instance
node = builder.add('image_io_u3v_camera1_u16x2')\
.set_port([dispose_p, gain0_p, exposure0_p, ])\
.set_param([pixel_format_ptr, frame_sync, gain_key, exposure_key, realtime_diaplay_mode, ])

node = builder.add('image_io_u3v_cameraN_u16x2')\
.set_iport([gain0_p, exposure0_p])\
.set_param([num_devices, frame_sync, gain_key, exposure_key, realtime_diaplay_mode, enable_control])

# Define Output Port
lp = node.get_port('output0')
out_p = node.get_port('output')
frame_count_p = node.get_port('frame_count')

# portmap
port_map = PortMap()

# input values
port_map.set_f64(gain0_p, gain)
port_map.set_f64(exposure0_p, exposure)
gain0_p.bind(gain)
exposure0_p.bind(exposure)

# output values
buf_size = (width, height, )
t = Type(TypeCode.Uint, 16, 1)
output0 = Buffer(t, buf_size)
t = Type(TypeCode.Uint, 32, 1)
frame_count = Buffer(t, (1,))
odata0 = np.full((height, width), fill_value=0, dtype=np.uint16)
output0 = Buffer(array=odata0)
out_p[0].bind(output0)

port_map.set_buffer(lp, output0)
port_map.set_buffer(frame_count_p, frame_count)

buf_size_opencv = (height, width)
fcdata = np.full((1), fill_value=0, dtype=np.uint32)
frame_count = Buffer(array=fcdata)
frame_count_p.bind(frame_count)

loop_num = 100

for x in range(loop_num):
port_map.set_u1(dispose_p, x==loop_num-1)

# running the builder
builder.run(port_map)

output0_bytes = output0.read(width*height*2)
builder.run()

output0_np_HxW = np.frombuffer(output0_bytes, np.uint16).reshape(buf_size_opencv)
odata0 *= pow(2, num_bit_shift)

output0_np_HxW *= pow(2, num_bit_shift)
median_output0_np_HxW = cv2.medianBlur(odata0, 5)

median_output0_np_HxW = cv2.medianBlur(output0_np_HxW, 5)

cv2.imshow("A", output0_np_HxW)
cv2.imshow("A", odata0)
cv2.imshow("C", median_output0_np_HxW)
cv2.waitKey(0)

cv2.destroyAllWindows()
cv2.waitKey(1)

cv2.destroyAllWindows()
195 changes: 80 additions & 115 deletions example/u3v_camera2_opencv/u3v_camera2_opencv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,126 +22,91 @@ const int32_t height = 1080;
double gain = 400;
double exposure = 400;

#ifdef _WIN32
#define MODULE_NAME "ion-bb.dll"
#elif __APPLE__
#define MODULE_NAME "libion-bb.dylib"
#else
#define MODULE_NAME "libion-bb.so"
#endif

int positive_pow(int base, int expo){
if (expo <= 0){
return 1;
}
if (expo == 1){
return base;
}else{
return base * positive_pow(base, expo-1);
}
int positive_pow(int base, int expo) {
if (expo <= 0) {
return 1;
}
if (expo == 1) {
return base;
} else {
return base * positive_pow(base, expo-1);
}
}


int main(int argc, char *argv[])
{
// Define builders to build, compile, and execute pipelines.
// Build the pipeline by adding nodes to the Builder.
Builder b;

// Set the target hardware. The default is CPU.
b.set_target(Halide::get_host_target());

// Load standard building block
b.with_bb_module(MODULE_NAME);

// Define the input port
// Port class is used to define dynamic I/O for each node.
Port dispose_p{ "dispose", Halide::type_of<bool>() };
Port gain0_p{ "gain0", Halide::type_of<double>() };
Port gain1_p{ "gain1", Halide::type_of<double>() };
Port exposure0_p{ "exposure0", Halide::type_of<double>() };
Port exposure1_p{ "exposure1", Halide::type_of<double>() };

// Connect the input port to the Node instance created by b.add().
Node n = b.add("image_io_u3v_camera2_u16x2")(dispose_p, gain0_p, gain1_p, exposure0_p, exposure1_p)
.set_param(
Param{"pixel_format_ptr", "Mono12"},
Param{"frame_sync", "true"},
Param{"gain_key", FEATURE_GAIN_KEY},
Param{"exposure_key", FEATURE_EXPOSURE_KEY},
Param{"realtime_diaplay_mode", "true"}
);

// Define output ports and pass each object from Node instance.
Port rp = n["output0"];
Port lp = n["output1"];
Port frame_count_p = n["frame_count"];

// Using PortMap, define output ports that map data to input ports and pass each object from a Node instance.
PortMap pm;

// In this sample, the gain value and exposure time of both sensors are set statically.
pm.set(gain0_p, gain);
pm.set(gain1_p, gain);
pm.set(exposure0_p, exposure);
pm.set(exposure1_p, exposure);

// Map data from output ports by using PortMap.
// Of the output of "u3v_camera2_u16x2",
// - rp and lp (output of the obtained video data) to output0 and 1 respectively,
// - frame_count_p (output of the frame number of the obtained video) to frame_count,
// each stored in the buffer.
std::vector< int > buf_size = std::vector < int >{ width, height };
Halide::Buffer<uint16_t> output0(buf_size);
Halide::Buffer<uint16_t> output1(buf_size);
Halide::Buffer<uint32_t> frame_count(1);

pm.set(rp, output0);
pm.set(lp, output1);
pm.set(frame_count_p, frame_count);

// Obtain image data continuously for 100 frames to facilitate operation check.
int loop_num = 100;
for (int i = 0; i < loop_num; ++i)
{
pm.set(dispose_p, i == loop_num-1);
// JIT compilation and execution of pipelines with Builder.
try {
b.run(pm);
}catch(std::exception& e){
// e.what() shows the error message if pipeline build/run was failed.
std::cerr << "Failed to build pipeline" << std::endl;
// Define builders to build, compile, and execute pipelines.
// Build the pipeline by adding nodes to the Builder.
Builder b;

// Set the target hardware. The default is CPU.
b.set_target(ion::get_host_target());

// Load standard building block
b.with_bb_module("ion-bb");

// Connect the input port to the Node instance created by b.add().
Node n = b.add("image_io_u3v_cameraN_u16x2")(&gain, &exposure, &gain, &exposure)
.set_param(
Param("frame_sync", false),
Param("gain_key", FEATURE_GAIN_KEY),
Param("exposure_key", FEATURE_EXPOSURE_KEY),
Param("realtime_diaplay_mode", true),
Param("enable_control", true)
);

// Map output buffer and ports by using Port::bind.
// - output0: output 0 of the obtained video data
// - output1: output 1 of the obtained video data
// - frame_count: output of the frame number of the obtained video
std::vector< int > buf_size = std::vector < int >{ width, height };
Buffer<uint16_t> output0(buf_size);
Buffer<uint16_t> output1(buf_size);
Buffer<uint32_t> frame_count(1);

n["output"].bind(std::vector<Buffer<uint16_t>>{output0, output1});
n["frame_count"].bind(frame_count);

// Obtain image data continuously for 100 frames to facilitate operation check.
int loop_num = 100;
int coef = positive_pow(2, NUM_BIT_SHIFT);
for (int i = 0; i < loop_num; ++i)
{
// JIT compilation and execution of pipelines with Builder.
b.run();

// Convert the retrieved buffer object to OpenCV buffer format.
// C and D are objects that store the result after smoothing.
cv::Mat A(height, width, CV_16UC1, output0.data());
cv::Mat B(height, width, CV_16UC1, output1.data());
cv::Mat C(height, width, CV_16UC1);
cv::Mat D(height, width, CV_16UC1);

// Depends on sensor image pixel format, apply bit shift on images
A = A * coef;
B = B * coef;

// Perform smoothing
cv::medianBlur(A, C, 5);
cv::medianBlur(B, D, 5);

// Display the image
cv::imshow("A", A);
cv::imshow("B", B);
cv::imshow("C", C);
cv::imshow("D", D);

// Wait for key input
// When any key is pressed, close the currently displayed image and proceed to the next frame.
cv::waitKey(1);
}

} catch (const ion::Error& e) {
std::cerr << e.what() << std::endl;
exit(1);
return 1;
}

// Convert the retrieved buffer object to OpenCV buffer format.
// C and D are objects that store the result after smoothing.
cv::Mat A(height, width, CV_16UC1);
cv::Mat B(height, width, CV_16UC1);
cv::Mat C(height, width, CV_16UC1);
cv::Mat D(height, width, CV_16UC1);

std::memcpy(A.ptr(), output0.data(), output0.size_in_bytes());
std::memcpy(B.ptr(), output1.data(), output1.size_in_bytes());

// Depends on sensor image pixel format, apply bit shift on images
A = A * positive_pow(2, NUM_BIT_SHIFT);
B = B * positive_pow(2, NUM_BIT_SHIFT);

// Perform smoothing
cv::medianBlur(A, C, 5);
cv::medianBlur(B, D, 5);

// Display the image
cv::imshow("A", A);
cv::imshow("B", B);
cv::imshow("C", C);
cv::imshow("D", D);

// Wait for key input
// When any key is pressed, close the currently displayed image and proceed to the next frame.
cv::waitKey(0);
}

return 0;
}
return 0;
}
Loading

0 comments on commit 8efb0fa

Please sign in to comment.