AttributeError: 'module' object has no attribute 'Bag'

asked 2019-03-11 01:12:55 -0500

SUBHASH gravatar image

updated 2019-03-11 03:38:07 -0500

gvdhoorn gravatar image

Program:

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

from cv_bridge import CvBridge
import cv2

TOPIC = 'camera/image_raw'
videopath='/Home/'
bagname='output1.bag'
def CreateVideoBag(videopath, bagname):
    '''Creates a bag file with a video file'''
    bag = rosbag.Bag(bagname, 'w')
    cap = cv2.VideoCapture(videopath)
    cb = CvBridge()
    prop_fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
    if prop_fps != prop_fps or prop_fps <= 1e-2:
        print ("Warning: can't get FPS. Assuming 24.")
        prop_fps = 24
    ret = True
    frame_id = 0
    while(ret):
        ret, frame = cap.read()
        if not ret:
            break
        stamp = rospy.rostime.Time.from_sec(float(frame_id) / prop_fps)
        frame_id += 1
        image = cb.cv2_to_imgmsg(frame, encoding='bgr8')
        image.header.stamp = stamp
        image.header.frame_id = "camera"
        bag.write(TOPIC, image, stamp)
    cap.release()
    bag.close()

if __name__ == "__main__":
    if len( sys.argv ) == 1:
        CreateVideoBag(videopath,bagname)
    else:
        print( "Usage: video2bag videofilename bagfilename")
bag.close()

Error output

$ python video2bag.py 
Error:
Traceback (most recent call last):
  File "video2bag.py", line 39, in <module>
    CreateVideoBag(videopath,bagname)
  File "video2bag.py", line 15, in CreateVideoBag
    bag = rosbag.Bag(bagname, 'w')
AttributeError: 'module' object has no attribute 'Bag'

Please help me with above error


Edit:

$ dpkg -l | grep rosbag
ii  ros-kinetic-rosbag                           1.12.14-0xenial-20180824-103606-0800                  amd64        This is a set of tools for recording from and playing back to ROS topics.
ii  ros-kinetic-rosbag-migration-rule            1.0.0-0xenial-20180809-134558-0800                    amd64        This empty package allows to export rosbag migration rule files without depending on rosbag.
ii  ros-kinetic-rosbag-storage                   1.12.14-0xenial-20180824-085858-0800                  amd64        This is a set of tools for recording from and playing back ROS message without relying on the ROS client library.
edit retag flag offensive close merge delete

Comments

@SUBHASH I tried to recreate your problem, but using the code you provided, I don't get the error you paste. (I get another one, but in line 18, so this should be working).

You are using ROS kinetic, considering your tags, right?

mgruhler gravatar image mgruhler  ( 2019-03-11 02:05:25 -0500 )edit

@mgruhler: did this really work for you? I've never seen this:

from ros import rosbag
gvdhoorn gravatar image gvdhoorn  ( 2019-03-11 02:59:44 -0500 )edit

@SUBHASH: can you show us the output of dpkg -l | grep rosbag?

gvdhoorn gravatar image gvdhoorn  ( 2019-03-11 03:00:15 -0500 )edit

$ dpkg -l | grep rosbag

ii ros-kinetic-rosbag 1.12.14-0xenial-20180824-103606-0800 amd64 This is a set of tools for recording from and playing back to ROS topics. ii ros-kinetic-rosbag-migration-rule 1.0.0-0xenial-20180809-134558-0800 amd64 This empty package allows to export rosbag migration rule files without depending on rosbag. ii ros-kinetic-rosbag-storage 1.12.14-0xenial-20180824-085858-0800 amd64 This is a set of tools for recording from and playing back ROS message without relying on the ROS client library. test@DH1CT5Z1:~$

SUBHASH gravatar image SUBHASH  ( 2019-03-11 03:41:56 -0500 )edit

I also tried using import rosbag but still same error. Also I don't know how to run python programs on ROS

SUBHASH gravatar image SUBHASH  ( 2019-03-11 03:43:02 -0500 )edit

please help me with python coding in ros

SUBHASH gravatar image SUBHASH  ( 2019-03-11 03:43:56 -0500 )edit

@gvdhoorn yes, I was as suprised as you are that this works. With the quick tests I did, I wasn't able to spot a difference between this and import rosbag...

I'm actually on the same versions @SUBHASH is. So I'm even more surprised that this isn't working for him.

@SUBHASH you have source'd your workspace properly, right? Are you using rosbag from debians or a from-source version?

mgruhler gravatar image mgruhler  ( 2019-03-11 06:40:59 -0500 )edit

yes properly as per mentioned on the official site

SUBHASH gravatar image SUBHASH  ( 2019-03-11 08:04:59 -0500 )edit