Detect

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <torch/script.

h>
#include <opencv2/opencv.hpp> // Include the OpenCV header

int main() {
std::string model_path = "/home/ubuntu/exported_model.pt";
std::string video_path = "/home/ubuntu/Downloads/remmie.mp4";

try {
// Load the TorchScript model
torch::jit::script::Module model;
model = torch::jit::load(model_path);
std::cout << "Model is Loaded Successfully" << std::endl;
model.eval();

// Open the video file


cv::VideoCapture video(video_path);
if (!video.isOpened()) {
std::cerr << "Error opening the video file" << std::endl;
return -1;
}

// Process each frame in the video


cv::Mat frame;
while (video.read(frame)) {
// Convert the frame to a Torch tensor and preprocess if needed
cv::Mat input;
cv::cvtColor(frame, input, cv::COLOR_BGR2RGB); // Convert BGR to RGB
cv::resize(input, input, cv::Size(224, 224)); // Resize to model's
input size
input.convertTo(input, CV_32FC3, 1.0 / 255); // Normalize pixel
values

// Create a Torch tensor from the preprocessed input


torch::Tensor inputTensor = torch::from_blob(
input.data, {1, input.rows, input.cols,
input.channels()}).permute({0, 3, 1, 2});

// Perform object detection on the frame


std::vector<torch::jit::IValue> inputs;
inputs.emplace_back(inputTensor);
at::Tensor outputTensor = model.forward(inputs).toTensor();

// Extract bounding box coordinates and class labels from the detection
results
auto output = outputTensor.accessor<float, 2>();
std::vector<cv::Rect> boxes;
std::vector<int> labels;
for (int i = 0; i < output.size(0); i++) {
float label = output[i][0];
float xmin = output[i][1] * frame.cols;
float ymin = output[i][2] * frame.rows;
float xmax = output[i][3] * frame.cols;
float ymax = output[i][4] * frame.rows;

boxes.emplace_back(cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax,


ymax)));
labels.emplace_back(static_cast<int>(label));
}
// Draw rectangles and labels on the frame
for (size_t i = 0; i < boxes.size(); i++) {
cv::rectangle(frame, boxes[i], cv::Scalar(0, 255, 0), 2);
cv::putText(frame, std::to_string(labels[i]), cv::Point(boxes[i].x,
boxes[i].y - 5),
cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 255, 0),
2);
}

// Display or save the modified frame


cv::imshow("Video", frame);
cv::waitKey(1); // Adjust the delay between frames as needed

// Break the loop if the 'q' key is pressed


if (cv::waitKey(1) == 'q') {
break;
}
}

// Release the video file and clean up


video.release();
cv::destroyAllWindows();

} catch (const c10::Error& e) {


std::cerr << "Error loading the model: " << e.msg() << std::endl;
return -1;
}

return 0;
}

You might also like