From 0b1f865b7dda85f07e869d01a270becc1c2927a1 Mon Sep 17 00:00:00 2001 From: Anton David Guaman Davila Date: Sat, 13 Jan 2024 21:43:49 -0330 Subject: [PATCH] Commented the code --- src/object_detection.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/object_detection.cpp b/src/object_detection.cpp index a36d859..4d6d1c1 100755 --- a/src/object_detection.cpp +++ b/src/object_detection.cpp @@ -19,19 +19,20 @@ #include #include "DetectedObject.h" - +// Image_Finder class definition class Image_Finder { public: - + // Constructor Image_Finder() : nh_("") { - // Subscribe to camera topic publishing data + // Subscribing to a camera topic to receive image data sub_img_ = nh_.subscribe("/camera/color/image_raw", 1, &Image_Finder::imageCallback, this); + // Setting up a publisher for the detected objects publisher = nh_.advertise("/Detected_Objects", 1); } // Function to continue the life of the node void spin() { - ros::Rate rate(100); // 2Hz + ros::Rate rate(100); // Setting a rate of 100Hz while (ros::ok()) { ros::spinOnce(); rate.sleep(); @@ -44,17 +45,18 @@ class Image_Finder { ros::Publisher publisher; object_detection_pkg::DetectedObjsArray detected_objs_array; + // Callback function for image data from the subscribed topic void imageCallback(const sensor_msgs::Image::ConstPtr& msg) { if (msg->data.empty()) { ROS_WARN("Received image message with empty data. Not saving the image."); return; // Exit early if the image data is empty } - // Ensure the temp_files directory exists + // Ensuring the directory for saving images exists std::string directory = "/home/david/Documents/catkin_ws/src/object_detection_pkg/temp_files"; mkdir(directory.c_str(), 0777); - // Convert the sensor_msgs/Image to an OpenCV image + // Converting ROS image message to OpenCV image cv_bridge::CvImagePtr cv_ptr; try { cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); @@ -63,7 +65,7 @@ class Image_Finder { return; } - // Save the OpenCV image to a file + // Saving the OpenCV image to a file std::string image_filename = directory + "/image.png"; if (!cv::imwrite(image_filename, cv_ptr->image)) { ROS_ERROR("Failed to save image to %s", image_filename.c_str()); @@ -71,7 +73,7 @@ class Image_Finder { } ROS_INFO("Saved image to %s", image_filename.c_str()); - // Run the Python script + // Running a Python script for object detection std::string python_command = "python3 /home/david/Documents/catkin_ws/src/object_detection_pkg/src/yolov5_image_analyzer.py " + image_filename; int return_code = system(python_command.c_str()); if (return_code != 0) { @@ -79,13 +81,12 @@ class Image_Finder { return; } - // Wait briefly to ensure the Python script has time to write the JSON file + // Waiting for the script to complete and generate JSON output ros::Duration(1.0).sleep(); // Adjust the duration if needed - // Define the path to the JSON file + // Reading the JSON output from the Python script std::string json_file_path = "/home/david/Documents/catkin_ws/src/object_detection_pkg/temp_files/detected_objects.json"; - // Read and parse the JSON file std::ifstream json_file(json_file_path); if (!json_file.is_open()) { ROS_ERROR("Failed to open JSON file: %s", json_file_path.c_str()); @@ -98,6 +99,7 @@ class Image_Finder { parseAndProcessJSONOutput(json_content); } + // Function to parse JSON output and process detected objects void parseAndProcessJSONOutput(const std::string& json_output) { if (json_output.empty() || std::all_of(json_output.begin(), json_output.end(), isspace)) { ROS_WARN("JSON output is empty or only contains whitespaces."); @@ -117,7 +119,7 @@ class Image_Finder { ROS_INFO("JSON array is empty. No objects detected."); return; } - + // Parsing each object in the JSON array for (const auto& obj : root) { object_detection_pkg::ObjDetected detected_obj; detected_obj.class_name = obj["class_name"].asString(); @@ -129,6 +131,7 @@ class Image_Finder { detected_objs_array.objects.push_back(detected_obj); } + // Publishing the array of detected objects and clearing the array for the next callback publisher.publish(detected_objs_array); detected_objs_array.objects.clear(); }