AttributeError: 'module' object has no attribute 'Bag'
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.
@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: did this really work for you? I've never seen this:
@SUBHASH: can you show us the output of
dpkg -l | grep rosbag
?$ 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:~$
I also tried using import rosbag but still same error. Also I don't know how to run python programs on ROS
please help me with python coding in ros
@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?yes properly as per mentioned on the official site