python pyautogui库应用

2023-01-23   ES  

Because many SLAM codes have the ROS version, the input of the general data of the ROS version is in the form of ROSBAG, so in order to test the new data set, you need to convert a data set without Rosbag to Rosbag. In fact What involves here is to convert ordinary photos to ROS pictures, use a node to post this picture stream, then use ROSBAG to record, and then generate a rosbag bag. The overall is the idea. Ros’s official website is also related to the relevant website of the official website.Tutorials.torials were commanded in some small errors, but it would not have a lot of influence. Next, two parts will be divided into two parts to record the transition process. The first part is to learn the content of tutorials on ROS Wiki. Experiment of your own dataset.

1.

in the Image_transport of ROS

Ontutorial, the following command operation is performed according to its tutorial. The premise of these operations is that you have installed the ROS system on your system, wowofficial tutorialInstalled, because of the problem of setting the software source, it has been tossed for a long time. Finally, the software source of the problem is deleted first, and finally the official tutorial can be successfully installed.

$ mkdir -p ~/image_transport_ws/src
$ cd ~/image_transport_ws/src
$ source /opt/ros/kinetic/setup.bash  #Notice to see what version of ROS you installed, mine is Kinetic
$ catkin_create_pkg learning_image_transport image_transport cv_bridge # generate a bag
$ cd ~/image_transport_ws  # or CD ..
$ rosdep install --from-paths src -i -y --rosdistro kinetic # Here you need to change to your own installation
$ catkin_make
$ source devel/setup.bash
$ git clone https://github.com/ros-perception/image_common.git
$ ln -s `pwd`/image_common/image_transport/tutorial/ ./src/image_transport_tutorial
$catkin_make # compile source file

After the operation is completed, you can use the following steps. You can post a picture to the node of ROS.

  1. Open a new window in the command line window you compiled to ensure that it is under the path of /image_transport_ws, and then run:
$roscore 
  1. Open a new window in the command line you compiled to ensure that it is under the path of /image_transport_ws, and then run:
$ source /opt/ros/kinetic/setup.bash
$ source devel/setup.bash
$ rosrun image_transport_tutorial my_publisher path/to/some/image.jpg

Source is to ensure certain paths. If you are not written in Bash, you need to run a new command line every time you need to run. The above commands are released. Then do the 3rd step.

  1. View node
$rostopic list -v # View node

Output is as follows: (Different from the different plug -in may output different output, but as long as you have not checked too much)
Published topics:
* /rosout [roslib/Log] 1 publisher
* /camera/image [sensor_msgs/Image] 1 publisher
* /rosout_agg [roslib/Log] 1 publisher

Subscribed topics:
* /rosout [roslib/Log] 1 subscriber

  1. Subscribe to node
$rosrun image_transport_tutorial my_subscriber

If there is a View, you will appear you posted the image picture after running an appeal command.

2. Use your own dataset as ROSBAG package

Method 1: Use ROS for data release

At present, I found two methods to achieve ROS-BAG generation. The first is to use the Publisher node of the ROS to release the data in detail. Then use ROSBAG to record.
This part is mainly improved according to the tutorial of the first part, mainly to improve the publisher.cpp file for its data set. I posted it directly below the code:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <iostream>
#include <vector>
#include <glob.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;

vector<string> getFiles(string dirc){
    vector<string> files;
    struct dirent *ptr;
    char base[1000];
    DIR *dir;
    dir = opendir(dirc.c_str());
    /*
    if(dir == NULL){
        perror("open dir error ...");
        exit(1);
    }*/
    while((ptr = readdir(dir)) != NULL){
        if(ptr->d_type == 8)//it;s file
        {
            files.push_back(dirc+'/'+ptr->d_name);
        }

        else if(ptr->d_type == 10)//link file
            continue;
        else if(ptr->d_type == 4){
            files.push_back(ptr->d_name);
        }
    }
    closedir(dir);
    sort(files.begin(),files.end());
    for(size_t i = 0; i < files.size();++i){
        cout << files[i] << endl;
    }
    return files;
}

void origin(int argc,char** argv){
  /* 
   ** This function is very similar to the Tutorial of a photo provided by the official website. It is only used to test a photo. 
   ** The previous part is the operation of the basic ROS publishing data, not much to say here. 
   */
    ros::init(argc, argv, "image_publisher");
    ros::NodeHandle nh;
    image_transport::ImageTransport it(nh);
    image_transport::Publisher pub = it.advertise("camera/image", 1);
    cv::Mat image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
    cout << argv[1] << endl;
    cv::waitKey(30);
    sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
    ros::Rate loop_rate(5);
    while (nh.ok()) {
        pub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();
        }
}


void directory(int argc,char** argv){
/* 
 ** This function is aimed at a large amount of data 
 */
    ros::init(argc, argv, "image_publisher");
    ros::NodeHandle nh;
    image_transport::ImageTransport it(nh);
    image_transport::Publisher pub = it.advertise("camera/image", 1);
    vector<string> result = getFiles(argv[1]);// /home/***/***/dataset/Mars/*.pgm

    //the argv is the url of the image,may we can use that for all images
    ros::Rate loop_rate(16);
    for(size_t i = 0; i < result.size();++i){
        if(!nh.ok())
            break;
        ostringstream stringStream;
        stringStream << result[i];
        cv::Mat image = cv::imread(stringStream.str(),CV_LOAD_IMAGE_COLOR);
        sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
        msg->header.stamp = ros::Time(111);// The timestamp here is replaced by your own. You need to write a function of reading the timestamp by yourself. Note that you correspond to the image. Generally, the timestamp file is also included in the folder of the picture.

        pub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();
    }
}

int main(int argc, char** argv)
{
  DIR *dir;
  dir = opendir(argv[1]);
  if(dir == NULL)
      origin(argc,argv);
  else
      directory(argc,argv);

}

above is the part of the Publisher, and then go to the Subscriper section or the previous Tutorial code, and then follow the previous operation. Finally, use Rosbag Record -a (-a to indicate all the topics published, other parameters referenceofficial document).
above is the method of generating ROSBAG using ROS to generate your data set.

Method 2. Use ROSBAG bags to write directly

This part is referred to the Great Godcode
I just worked as the following transportation and applied it to my own data set.
The following is my code, because I need to integrate IMU data and image data, and both have time to poke TimestAmp.
The following is my code:

import cv2
import time, sys, os
from ros import rosbag
import roslib
import rospy
roslib.load_manifest('sensor_msgs')
from sensor_msgs.msg import Image,Imu
from geometry_msgs.msg import Vector3

# import ImageFile
from PIL import ImageFile
from PIL import Image as ImagePIL

def CompSortFileNamesNr(f):
    g = os.path.splitext(os.path.split(f)[1])[0] #get the file of the
    numbertext = ''.join(c for c in g if c.isdigit())
    return int(numbertext)

def ReadImages(filename):
    '''Generates a list of files from the directory'''
    print("Searching directory %s" % dir)
    all = []
    left_files = []
    right_files = []
    files = os.listdir(filename)
    for f in sorted(files):
        if os.path.splitext(f)[1] in ['.bmp', '.png', '.jpg', '.pgm']:
            '''
            if 'left' in f or 'left' in path:
                left_files.append(os.path.join(path, f))
            elif 'right' in f or 'right' in path:
                right_files.append(os.path.join(path, f))
            '''
            all.append(os.path.join(filename, f))
    return all

def ReadIMU(filename):
    '''return IMU data and timestamp of IMU'''
    file = open(filename,'r')
    all = file.readlines()
    timestamp = []
    imu_data = []
    for f in all:
        line = f.rstrip('\n').split(' ')
        timestamp.append(line[0])
        imu_data.append(line[1:])
    return timestamp,imu_data


def CreateBag(args):#img,imu, bagname, timestamps
    '''read time stamps'''
    imgs = ReadImages(args[0])
    imagetimestamps=[]
    imutimesteps,imudata = ReadIMU(args[1]) #the url of  IMU data
    file = open(args[3], 'r')
    all = file.readlines()
    for f in all:
        imagetimestamps.append(f.rstrip('\n').split(' ')[1])
    file.close()
    '''Creates a bag file with camera images'''
    if not os.path.exists(args[2]):
    os.system(r'touch %s' % args[2])
    bag = rosbag.Bag(args[2], 'w')

    try:
        for i in range(len(imudata)):
            imu = Imu()
            angular_v = Vector3()
            linear_a = Vector3()
            angular_v.x = float(imudata[i][0])
            angular_v.y = float(imudata[i][1])
            angular_v.z = float(imudata[i][2])
            linear_a.x = float(imudata[i][3])
            linear_a.y = float(imudata[i][4])
            linear_a.z = float(imudata[i][5])
            imuStamp = rospy.rostime.Time.from_sec(float(imutimesteps[i]))
            imu.header.stamp=imuStamp
            imu.angular_velocity = angular_v
            imu.linear_acceleration = linear_a

            bag.write("IMU/imu_raw",imu,imuStamp)

        for i in range(len(imgs)):
            print("Adding %s" % imgs[i])
            fp = open(imgs[i], "r")
            p = ImageFile.Parser()

            '''read image size'''
            imgpil = ImagePIL.open(imgs[0])
            width, height = imgpil.size
            # print "size:",width,height

            while 1:
                s = fp.read(1024)
                if not s:
                    break
                p.feed(s)

            im = p.close()

            Stamp = rospy.rostime.Time.from_sec(float(imagetimestamps[i]))

            '''set image information '''
            Img = Image()

            Img.header.stamp = Stamp
            Img.height = height
            Img.width = width
            Img.header.frame_id = "camera"

            '''for mono8'''
            Img.encoding = "mono8"
            Img_data = [pix for pixdata in [im.getdata()] for pix in pixdata]
            Img.step = Img.width


            Img.data = Img_data
            bag.write('camera/image_raw', Img, Stamp)
    finally:
        bag.close()

if __name__ == "__main__":
      print(sys.argv)
      CreateBag(sys.argv[1:])

This is enough to run directly with python. It does not need ROS help, but the premise is that you have installed ROS, and the ROS environment variable class is written into the Bash file.
Running command:

python img2bag_Mars_imu.py /home/***/***/dataset/Mars/Regular_1/img_data/left /home/***/***/dataset/Mars/Regular_1/imu_data.txt /home/***/***/test.bag /home/***/***/dataset/Mars/Regular_1/img_data/timestamps.txt

The first parameter is the name of the IMAGE folder, the second parameter is the data of the IMU, the third parameter is the name of the ROSBAG you want to generate, and the fourth is the timestamp of IMAGE.
This method is firstHereSee.

Above

or above is the result of almost two days of tossing. Although the effect of the final test is not very good, at least the data is processed. I hope it will be useful to everyone.
Reprinted, please the famous source.

source

Related Posts

POJ3006, Dirichlet’s theorem on Arithmetic Progressions, Di Likrey theorem.

Hive query sequence

Oracle automatically creates a partition table Guns at time

hexo+github build a personal blog (super detailed tutorial)

python pyautogui库应用

Random Posts

jump table (c ++ implementation) shenhang The

CSS space processing

Eclipse CDT Introduce the C/C ++ project built by Makefile

Solving the Mu class linux tutorial xshell cannot connect CentOS, VMware Install CentOS6.3, connect xshell

Maven’s basic use