Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import rospy
  2. from pyzbar import pyzbar
  3. from cv_bridge import CvBridge
  4. from sensor_msgs.msg import Image
  5.  
  6. bridge = CvBridge()
  7.  
  8. rospy.init_node('barcode_test')
  9.  
  10. # Image subscriber callback function
  11. def image_callback(data):
  12.     cv_image = bridge.imgmsg_to_cv2(data, 'bgr8')  # OpenCV image
  13.     barcodes = pyzbar.decode(cv_image)
  14.     for barcode in barcodes:
  15.         b_data = barcode.data.encode("utf-8")
  16.         b_type = barcode.type
  17.         (x, y, w, h) = barcode.rect
  18.         xc = x + w/2
  19.         yc = y + h/2
  20.         print ("Found {} with data {} with center at x={}, y={}".format(b_type, b_data, xc, yc))
  21.  
  22. image_sub = rospy.Subscriber('main_camera/image_raw', Image, image_callback, queue_size=1)
  23.  
  24. rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement