From 73b5d3ebbb7fc78c4b1e42e75b1a67db2b80977e Mon Sep 17 00:00:00 2001 From: Wangyida Date: Sat, 3 Oct 2015 01:15:11 +0800 Subject: [PATCH] training data generation from PASCAL3D add light --- modules/cnn_3dobj/samples/CMakeLists.txt | 2 +- modules/cnn_3dobj/samples/datagen.sh | 101 ++++++++++++++++-- .../samples/demo_sphereview_data.cpp | 59 ++++++---- 3 files changed, 128 insertions(+), 34 deletions(-) diff --git a/modules/cnn_3dobj/samples/CMakeLists.txt b/modules/cnn_3dobj/samples/CMakeLists.txt index e283f8ef3..6151f89e8 100755 --- a/modules/cnn_3dobj/samples/CMakeLists.txt +++ b/modules/cnn_3dobj/samples/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 2.8) SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb ") SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") -project(sphereview_test) +project(cnn_3dobj) find_package(OpenCV REQUIRED) set(SOURCES_generator demo_sphereview_data.cpp) include_directories(${OpenCV_INCLUDE_DIRS}) diff --git a/modules/cnn_3dobj/samples/datagen.sh b/modules/cnn_3dobj/samples/datagen.sh index fd571e022..3bacc4b69 100755 --- a/modules/cnn_3dobj/samples/datagen.sh +++ b/modules/cnn_3dobj/samples/datagen.sh @@ -1,11 +1,90 @@ -rm -rf ../data/binary_image -rm -rf ../data/binary_label -rm -rf ../data/header_for_image -rm -rf ../data/header_for_label - -./sphereview_test -plymodel=../data/3Dmodel/ape.ply -label_class=0 -cam_head_x=0 -cam_head_y=0 -cam_head_z=1 -./sphereview_test -plymodel=../data/3Dmodel/ant.ply -label_class=1 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0 -./sphereview_test -plymodel=../data/3Dmodel/cow.ply -label_class=2 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0 -./sphereview_test -plymodel=../data/3Dmodel/plane.ply -label_class=3 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0 -./sphereview_test -plymodel=../data/3Dmodel/bunny.ply -label_class=4 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0 -./sphereview_test -plymodel=../data/3Dmodel/horse.ply -label_class=5 -cam_head_x=0 -cam_head_y=0 -cam_head_z=-1 \ No newline at end of file +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/01.ply -label_class=1 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/02.ply -label_class=1 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/03.ply -label_class=1 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/04.ply -label_class=1 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/05.ply -label_class=1 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/06.ply -label_class=1 -label_item=6 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/07.ply -label_class=1 -label_item=7 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/aeroplane/08.ply -label_class=1 -label_item=8 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/01.ply -label_class=2 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/02.ply -label_class=2 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/03.ply -label_class=2 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/04.ply -label_class=2 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/05.ply -label_class=2 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bicycle/06.ply -label_class=2 -label_item=6 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/01.ply -label_class=3 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/02.ply -label_class=3 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/03.ply -label_class=3 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/04.ply -label_class=3 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/05.ply -label_class=3 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/boat/06.ply -label_class=3 -label_item=6 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/01.ply -label_class=4 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/02.ply -label_class=4 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/03.ply -label_class=4 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/04.ply -label_class=4 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/05.ply -label_class=4 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/06.ply -label_class=4 -label_item=6 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/07.ply -label_class=4 -label_item=7 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bottle/08.ply -label_class=4 -label_item=8 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/01.ply -label_class=5 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/02.ply -label_class=5 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/03.ply -label_class=5 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/04.ply -label_class=5 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/05.ply -label_class=5 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/bus/06.ply -label_class=5 -label_item=6 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/01.ply -label_class=6 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/02.ply -label_class=6 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/03.ply -label_class=6 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/04.ply -label_class=6 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/05.ply -label_class=6 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/06.ply -label_class=6 -label_item=6 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/07.ply -label_class=6 -label_item=7 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/08.ply -label_class=6 -label_item=8 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/09.ply -label_class=6 -label_item=9 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/car/10.ply -label_class=6 -label_item=10 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/01.ply -label_class=7 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/02.ply -label_class=7 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/03.ply -label_class=7 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/04.ply -label_class=7 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/05.ply -label_class=7 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/06.ply -label_class=7 -label_item=6 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/07.ply -label_class=7 -label_item=7 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/08.ply -label_class=7 -label_item=8 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/09.ply -label_class=7 -label_item=9 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/chair/10.ply -label_class=7 -label_item=10 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/diningtable/01.ply -label_class=8 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/diningtable/02.ply -label_class=8 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/diningtable/03.ply -label_class=8 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/diningtable/04.ply -label_class=8 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/diningtable/05.ply -label_class=8 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/diningtable/06.ply -label_class=8 -label_item=6 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/01.ply -label_class=9 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/02.ply -label_class=9 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/03.ply -label_class=9 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/04.ply -label_class=9 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/motorbike/05.ply -label_class=9 -label_item=5 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/sofa/01.ply -label_class=10 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/sofa/02.ply -label_class=10 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/sofa/03.ply -label_class=10 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/sofa/04.ply -label_class=10 -label_item=4 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/sofa/05.ply -label_class=10 -label_item=5 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/sofa/06.ply -label_class=10 -label_item=6 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/01.ply -label_class=11 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/02.ply -label_class=11 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/03.ply -label_class=11 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/train/04.ply -label_class=11 -label_item=4 + +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/tvmonitor/01.ply -label_class=12 -label_item=1 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/tvmonitor/02.ply -label_class=12 -label_item=2 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/tvmonitor/03.ply -label_class=12 -label_item=3 +./sphereview_test -plymodel=/Users/yidawang/Downloads/PASCAL3D+_release1.1/CAD/tvmonitor/04.ply -label_class=12 -label_item=4 \ No newline at end of file diff --git a/modules/cnn_3dobj/samples/demo_sphereview_data.cpp b/modules/cnn_3dobj/samples/demo_sphereview_data.cpp index c1f8d4297..2185b4dc8 100755 --- a/modules/cnn_3dobj/samples/demo_sphereview_data.cpp +++ b/modules/cnn_3dobj/samples/demo_sphereview_data.cpp @@ -47,16 +47,19 @@ using namespace cv::cnn_3dobj; int main(int argc, char *argv[]) { const String keys = "{help | | demo :$ ./sphereview_test -ite_depth=2 -plymodel=../data/3Dmodel/ape.ply -imagedir=../data/images_all/ -labeldir=../data/label_all.txt -num_class=6 -label_class=0, then press 'q' to run the demo for images generation when you see the gray background and a coordinate.}" -"{ite_depth | 3 | Iteration of sphere generation.}" -"{plymodel | ../data/3Dmodel/ape.ply | Path of the '.ply' file for image rendering. }" -"{imagedir | ../data/images_all/ | Path of the generated images for one particular .ply model. }" -"{labeldir | ../data/label_all.txt | Path of the generated images for one particular .ply model. }" -"{cam_head_x | 0 | Head of the camera. }" -"{cam_head_y | -1 | Head of the camera. }" -"{cam_head_z | 0 | Head of the camera. }" -"{num_class | 6 | Total number of classes of models}" -"{label_class | 0 | Class label of current .ply model}" -"{rgb_use | 0 | Use RGB image or grayscale}"; + "{ite_depth | 2 | Iteration of sphere generation.}" + "{plymodel | ../data/3Dmodel/ape.ply | Path of the '.ply' file for image rendering. }" + "{imagedir | ../data/images_all/ | Path of the generated images for one particular .ply model. }" + "{labeldir | ../data/label_all.txt | Path of the generated images for one particular .ply model. }" + "{cam_head_x | 0 | Head of the camera. }" + "{cam_head_y | 0 | Head of the camera. }" + "{cam_head_z | -1 | Head of the camera. }" + "{image_size | 128 | Size of captured images. }" + "{label_class | 1 | Class label of current .ply model. }" + "{label_item | 1 | Item label of current .ply model. }" + "{rgb_use | 0 | Use RGB image or grayscale. }" + "{num_class | 6 | Total number of classes of models. }" + "{binary_out | 0 | Produce binaryfiles for images and label. }"; /* Get parameters from comand line. */ cv::CommandLineParser parser(argc, argv, keys); parser.about("Generating training data for CNN with triplet loss"); @@ -69,12 +72,15 @@ int main(int argc, char *argv[]) string plymodel = parser.get("plymodel"); string imagedir = parser.get("imagedir"); string labeldir = parser.get("labeldir"); - int num_class = parser.get("num_class"); int label_class = parser.get("label_class"); + int label_item = parser.get("label_item"); float cam_head_x = parser.get("cam_head_x"); float cam_head_y = parser.get("cam_head_y"); float cam_head_z = parser.get("cam_head_z"); + int image_size = parser.get("image_size"); int rgb_use = parser.get("rgb_use"); + int num_class = parser.get("num_class"); + int binary_out = parser.get("binary_out"); cv::cnn_3dobj::icoSphere ViewSphere(10,ite_depth); std::vector campos = ViewSphere.CameraPos; std::fstream imglabel; @@ -84,10 +90,12 @@ int main(int argc, char *argv[]) /* Create a window using viz. */ viz::Viz3d myWindow("Coordinate Frame"); /* Set window size as 64*64, we use this scale as default. */ - myWindow.setWindowSize(Size(64,64)); + myWindow.setWindowSize(Size(image_size,image_size)); /* Set background color. */ myWindow.setBackgroundColor(viz::Color::gray()); myWindow.spin(); + /* Add light. */ + myWindow.addLight(Vec3d(0,0,100000), Vec3d(0,0,0), viz::Color::white(), viz::Color::gray(), viz::Color::black(), viz::Color::white()); /* Create a Mesh widget, loading .ply models. */ viz::Mesh objmesh = viz::Mesh::load(plymodel); /* Get the center of the generated mesh widget, cause some .ply files. */ @@ -101,17 +109,21 @@ int main(int argc, char *argv[]) cam_y_dir.z = cam_head_z; const char* headerPath = "../data/header_for_"; const char* binaryPath = "../data/binary_"; - ViewSphere.createHeader((int)campos.size(), 64, 64, headerPath); + if (binary_out) + { + ViewSphere.createHeader((int)campos.size(), image_size, image_size, headerPath); + } + char* temp = new char; /* Images will be saved as .png files. */ for(int pose = 0; pose < (int)campos.size(); pose++){ - char* temp = new char; - sprintf (temp,"%d",label_class); + int label_x, label_y, label_z; + label_x = (int)(campos.at(pose).x*100); + label_y = (int)(campos.at(pose).y*100); + label_z = (int)(campos.at(pose).z*100); + sprintf (temp,"%02d_%02d_%04i_%04i_%04i", label_class, label_item, label_x, label_y, label_z); string filename = temp; - filename += "_"; - sprintf (temp,"%d",pose); - filename += temp; filename += ".png"; - imglabel << filename << ' ' << (int)(campos.at(pose).x*100) << ' ' << (int)(campos.at(pose).y*100) << ' ' << (int)(campos.at(pose).z*100) << endl; + imglabel << filename << ' ' << label_class << endl; filename = imagedir + filename; /* Get the pose of the camera using makeCameraPoses. */ Affine3f cam_pose = viz::makeCameraPose(campos.at(pose)*380+cam_focal_point, cam_focal_point, cam_y_dir*380+cam_focal_point); @@ -127,8 +139,8 @@ int main(int argc, char *argv[]) { viz::WCameraPosition cpw(1); // Coordinate axes viz::WCameraPosition cpw_frustum(Vec2f(0.5, 0.5)); // Camera frustum - myWindow.showWidget("CPW", cpw, cam_pose); - myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose); + myWindow.showWidget("CPW", cpw, cam_pose); + myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose); } /* Visualize widget. */ @@ -140,8 +152,11 @@ int main(int argc, char *argv[]) myWindow.setViewerPose(cam_pose); /* Save screen shot as images. */ myWindow.saveScreenshot(filename); + if (binary_out) + { /* Write images into binary files for further using in CNN training. */ - ViewSphere.writeBinaryfile(filename, binaryPath, headerPath,(int)campos.size()*num_class, label_class, (int)(campos.at(pose).x*100), (int)(campos.at(pose).y*100), (int)(campos.at(pose).z*100), rgb_use); + ViewSphere.writeBinaryfile(filename, binaryPath, headerPath,(int)campos.size()*num_class, label_class, (int)(campos.at(pose).x*100), (int)(campos.at(pose).y*100), (int)(campos.at(pose).z*100), rgb_use); + } } imglabel.close(); return 1;