Skip to content

Commit 0a3c5b8

Browse files
committed
Working on directory structure.
1 parent 45457a0 commit 0a3c5b8

6 files changed

Lines changed: 81 additions & 217 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ libtorch_static/
4747
examples/vgg/**/*.pt
4848
segmentation_models.pytorch/
4949
.idea/
50-
cmake-build-debug/
50+
cmake-build-debug/
51+
libtorch_old/

CMakeLists.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ set(REQUIRED_LIBS
185185
"libtorch"
186186
"libtorch_cpu"
187187
"libc10"
188-
"libtorch_global_deps"
188+
# "libtorch_global_deps"
189189
)
190190

191191
set(DISALLOWED_LIBS
@@ -206,11 +206,18 @@ foreach(lib_name IN LISTS LIBTORCH_ALL_LIBS)
206206
list(APPEND LIBTORCH_LIBS_LINKER_ARGS "-l${lib_name_short}")
207207
endforeach()
208208

209-
# cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
209+
cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
210210
# cmake_print_variables(${BRIDGE_OBJECT_FILES})
211211
# cmake_print_variables(BRIDGE_OBJECT_FILES)
212212

213213

214+
set(LIBTORCH_LIBS_LINKER_ARGS
215+
"-ltorch"
216+
"-ltorch_cpu"
217+
"-lc10"
218+
"-ltorch_global_deps"
219+
)
220+
214221
add_executable(TorchBridge ${BRIDGE_DIR}/lib/Bridge.chpl)
215222
add_dependencies(TorchBridge bridge)
216223
add_dependencies(TorchBridge ChAI)
@@ -219,10 +226,10 @@ target_link_options(TorchBridge
219226
${BRIDGE_DIR}/include/bridge.h
220227
${BRIDGE_OBJECT_FILES}
221228
-L ${LIBTORCH_DIR}/lib
222-
# "-ltorch"
223-
# "-ltorch_cpu"
224-
# "-lc10"
225-
# "-ltorch_global_deps"
229+
"-ltorch"
230+
"-ltorch_cpu"
231+
"-lc10"
232+
"-ltorch_global_deps"
226233
${LIBTORCH_LIBS_LINKER_ARGS}
227234
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
228235
)

bridge/util/cvtool.hpp

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,42 @@ std::shared_ptr<cv::Mat> create_frame_buffer(int height, int width) {
4646

4747

4848

49-
// std::shared_ptr<torch::Tensor> get_frame_buffer_tensor(int height,int width) {
49+
// std::shared_ptr<at::Tensor> get_frame_buffer_tensor(int height,int width) {
5050
// auto options_cpu = torch::TensorOptions().dtype(torch::kFloat32).device(torch::kCPU);
5151
// torch::Tensor frame_tensor_cpu = torch::empty({1, height, width, 3}, options_cpu);
5252
// }
5353

54-
std::shared_ptr<torch::Tensor> create_buffer_tensor(
54+
std::shared_ptr<at::Tensor> create_buffer_tensor(
5555
torch::IntArrayRef sizes,
5656
torch::ScalarType = torch::kFloat32,
5757
torch::Device device = get_default_device()) {
5858
auto options_device = torch::TensorOptions()
5959
.dtype(torch::kFloat32)
6060
.device(default_device);
6161
auto tensor = torch::empty(sizes, options_device);
62-
auto frame_tensor_device = std::make_shared<torch::Tensor>(tensor);
62+
auto frame_tensor_device = std::make_shared<at::Tensor>(tensor);
6363
return frame_tensor_device;
6464
}
6565

6666

67-
std::shared_ptr<torch::Tensor> create_frame_buffer_tensor(int height,int width,torch::Device device = get_default_device()) {
67+
std::shared_ptr<at::Tensor> create_frame_buffer_tensor(int height,int width,torch::Device device = get_default_device()) {
6868
torch::IntArrayRef sizes = {1, height, width, 3};
6969
return create_buffer_tensor(sizes, torch::kFloat32);
7070
}
7171

72-
torch::Tensor to_tensor(cv::Mat &img) {
72+
at::Tensor to_tensor(cv::Mat &img) {
7373
auto t = torch::from_blob(img.data, {1, img.rows, img.cols, 3}, torch::kUInt8).clone();
7474
t = t.to(default_device);
7575
t = t.to(torch::kFloat32).permute({0, 3, 1, 2}) / 255.0;
7676
return t;//.to(default_device,true);
7777
}
7878

79-
cv::Mat to_mat(torch::Tensor &tensor) {
79+
cv::Mat to_mat(at::Tensor &tensor) {
8080
// Ensure the tensor is on the CPU and not on the GPU
81-
// torch::Tensor cpu_tensor = tensor.to(torch::kCPU);
81+
// at::Tensor cpu_tensor = tensor.to(torch::kCPU);
8282

8383
// Clone the tensor to avoid modifying the original data
84-
// torch::Tensor cloned_tensor = cpu_tensor.clone();
84+
// at::Tensor cloned_tensor = cpu_tensor.clone();
8585

8686

8787
int height = tensor.size(2);
@@ -146,7 +146,7 @@ torch::Device get_default_device() {
146146
// return module;
147147
// }
148148

149-
torch::Tensor imagenet_resize(torch::Tensor& image, int height, int width) {
149+
at::Tensor imagenet_resize(at::Tensor& image, int height, int width) {
150150
// Resize the image to the specified height and width
151151
auto resized_image = torch::nn::functional::interpolate(
152152
image,
@@ -158,15 +158,15 @@ torch::Tensor imagenet_resize(torch::Tensor& image, int height, int width) {
158158
return resized_image;
159159
}
160160

161-
torch::Tensor imagenet_normalize_tensor(torch::Tensor& input) {
161+
at::Tensor imagenet_normalize_tensor(at::Tensor& input) {
162162
// Normalize the image using ImageNet mean and std
163163
// auto mean = torch::tensor({0.485, 0.456, 0.406}).view({1, 3, 1, 1});
164164
// auto std = torch::tensor({0.229, 0.224, 0.225}).view({1, 3, 1, 1});
165165
// return (image - mean) / std;
166166

167167
// std::cout << "Input sizes: " << input.sizes() << std::endl;
168168

169-
torch::Tensor image = input.to(torch::kFloat32).clone();// / 255.0;
169+
at::Tensor image = input.to(torch::kFloat32).clone();// / 255.0;
170170
// std::cout << "Image sizes: " << image.sizes() << std::endl;
171171

172172
static const std::vector<float> mean_data{0.485, 0.456, 0.406};
@@ -189,4 +189,47 @@ torch::Tensor imagenet_normalize_tensor(torch::Tensor& input) {
189189
output = output;
190190
// std::cout << "Output sizes: " << output.sizes() << std::endl;
191191
return output;
192-
}
192+
}
193+
194+
195+
int show_webcam(int cam_index) {
196+
cv::VideoCapture cap = open_camera(cam_index);
197+
if (!cap.isOpened()) {
198+
std::cerr << "Could not open camera index " << cam_index << std::endl;
199+
return -1;
200+
}
201+
202+
cv::Mat frame;
203+
while (true) {
204+
cap >> frame;
205+
if (frame.empty()) {
206+
std::cerr << "Failed to capture image from camera" << std::endl;
207+
break;
208+
}
209+
210+
cv::imshow("Webcam", frame);
211+
if (cv::waitKey(30) >= 0) break; // Exit on any key press
212+
}
213+
return 0;
214+
}
215+
216+
217+
218+
at::Tensor capture_webcam(int cam_index) {
219+
cv::VideoCapture cap = open_camera(cam_index);
220+
if (!cap.isOpened()) {
221+
std::cerr << "Could not open camera index " << cam_index << std::endl;
222+
return at::Tensor();
223+
}
224+
225+
cv::Mat frame;
226+
cap >> frame;
227+
228+
if (frame.empty()) {
229+
std::cerr << "Failed to capture image from camera" << std::endl;
230+
return at::Tensor();
231+
}
232+
233+
auto tensor = to_tensor(frame);
234+
return tensor;
235+
}

demos/video/cvutil.hpp

Lines changed: 0 additions & 192 deletions
This file was deleted.

demos/video/webcam_infer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ struct Model : torch::nn::Module {
5454
}
5555
// auto output = x + r;
5656
auto input = x;
57-
auto output = imageops::sobel_rgb(input);
58-
// auto output = imagenet_normalize_tensor(input);
57+
// auto output = imageops::sobel_rgb(input);
58+
auto output = imagenet_normalize_tensor(input);
5959
return output;
6060
}
6161

@@ -69,6 +69,8 @@ int max_fps = 60;
6969

7070
int main(int argc, char** argv) {
7171

72+
show_webcam(0);
73+
7274
if (argc < 2) {
7375
std::cerr << "Usage: " << argv[0] << " <path/to/torchscript_model.pt> [cam_index]" << std::endl;
7476
return -1;

0 commit comments

Comments
 (0)