diff --git a/src/object_detection.cpp b/src/object_detection.cpp index 4d6d1c1..7b042cd 100755 --- a/src/object_detection.cpp +++ b/src/object_detection.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -44,6 +45,8 @@ class Image_Finder { ros::Subscriber sub_img_; ros::Publisher publisher; object_detection_pkg::DetectedObjsArray detected_objs_array; + std::string package_path = ros::package::getPath("object_detection_pkg"); + // Callback function for image data from the subscribed topic void imageCallback(const sensor_msgs::Image::ConstPtr& msg) { @@ -53,7 +56,7 @@ class Image_Finder { } // Ensuring the directory for saving images exists - std::string directory = "/home/david/Documents/catkin_ws/src/object_detection_pkg/temp_files"; + std::string directory = package_path + "/temp_files"; mkdir(directory.c_str(), 0777); // Converting ROS image message to OpenCV image @@ -66,7 +69,7 @@ class Image_Finder { } // Saving the OpenCV image to a file - std::string image_filename = directory + "/image.png"; + std::string image_filename = package_path + "/temp_files/image.png"; if (!cv::imwrite(image_filename, cv_ptr->image)) { ROS_ERROR("Failed to save image to %s", image_filename.c_str()); return; @@ -74,7 +77,7 @@ class Image_Finder { ROS_INFO("Saved image to %s", image_filename.c_str()); // 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; + std::string python_command = "python3 " + package_path + "/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."); @@ -85,7 +88,7 @@ class Image_Finder { ros::Duration(1.0).sleep(); // Adjust the duration if needed // 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"; + std::string json_file_path = package_path + "/temp_files/detected_objects.json"; std::ifstream json_file(json_file_path); if (!json_file.is_open()) { diff --git a/src/yolov5_image_analyzer.py b/src/yolov5_image_analyzer.py index 3afebcb..870ff54 100644 --- a/src/yolov5_image_analyzer.py +++ b/src/yolov5_image_analyzer.py @@ -12,6 +12,8 @@ from yolov5.models.experimental import attempt_load from yolov5.utils.general import non_max_suppression, scale_segments, xyxy2xywh from yolov5.utils.augmentations import letterbox +import rospkg + # Define a structure for detected objects def create_detected_object(class_name, confidence, x_min, x_max, y_min, y_max): @@ -57,10 +59,13 @@ def detect_objects(image_path, weights_path): return detected_objects def main(): + rospack = rospkg.RosPack() + package_path = rospack.get_path('object_detection_pkg') + # Specify the input image path and model weights path - input_image_path = '/home/david/Documents/catkin_ws/src/object_detection_pkg/temp_files/image.png' + input_image_path = package_path + '/temp_files/image.png' weights_path = '/home/david/Documents/ObjectDetection/buoy_detector_yolov_5_pytorch.pt' - output_json_path = '/home/david/Documents/catkin_ws/src/object_detection_pkg/temp_files/detected_objects.json' + output_json_path = package_path + '/temp_files/detected_objects.json' # Detect objects in the input image and get the detected objects as an array detected_objects = detect_objects(input_image_path, weights_path)