Skip to content

Commit

Permalink
Solved absolute path references by using rospack to find the package …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
DavidGuamanDavila committed Jan 14, 2024
1 parent 0b1f865 commit 641eb17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/object_detection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ros/ros.h>
#include <ros/console.h>
#include <ros/package.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/image_encodings.h>
#include <geometry_msgs/Vector3.h>
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -66,15 +69,15 @@ 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;
}
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.");
Expand All @@ -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()) {
Expand Down
9 changes: 7 additions & 2 deletions src/yolov5_image_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 641eb17

Please sign in to comment.