Skip to content

Commit

Permalink
Commented the code
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGuamanDavila committed Jan 14, 2024
1 parent 48a1714 commit 0b1f865
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/object_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
#include <jsoncpp/json/json.h>
#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<object_detection_pkg::DetectedObjsArray>("/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();
Expand All @@ -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);
Expand All @@ -63,29 +65,28 @@ 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());
return;
}
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) {
ROS_ERROR("Failed to execute the Python script.");
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());
Expand All @@ -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.");
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down

0 comments on commit 0b1f865

Please sign in to comment.