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.


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
$ 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:
  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 ...");
    while((ptr = readdir(dir)) != NULL){
        if(ptr->d_type == 8)//it;s file

        else if(ptr->d_type == 10)//link file
        else if(ptr->d_type == 4){
    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;
    sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
    ros::Rate loop_rate(5);
    while (nh.ok()) {

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){
        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.


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


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
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(' ')
    return timestamp,imu_data

def CreateBag(args):#img,imu, bagname, timestamps
    '''read time stamps'''
    imgs = ReadImages(args[0])
    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])
    '''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')

        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.angular_velocity = angular_v
            imu.linear_acceleration = linear_a


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

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

            while 1:
                s =
                if not 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
            bag.write('camera/image_raw', Img, Stamp)

if __name__ == "__main__":

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 /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.


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.


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