@@ -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+ }
0 commit comments