-1

Im using the qrcode scanner to detect and decode and it decodes the output properly. Here im using the "text" as the decoded output of qrcode. If It gets same type of box like APPLE,it must increment the apple_count to 1. by using this code i only get "1" for apple_count . I think the problem may be because of loop but Im not able to solve it.Please help me.Thankyou

class camera_1:

  def __init__(self):
    self.image_sub = rospy.Subscriber("/iris/usb_cam/image_raw", Image, self.callback)

  def callback(self,data):
    bridge = CvBridge()

    try:
      cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      rospy.logerr(e)

    (rows,cols,channels) = cv_image.shape
    
    image = cv_image

    resized_image = cv2.resize(image, (640, 640)) 

    
    qr_result = decode(resized_image)

    #print (qr_result)
    
    qr_data = qr_result[0].data
    print(qr_data)
    
    (x, y, w, h) = qr_result[0].rect

    cv2.rectangle(resized_image, (x, y), (x + w, y + h), (0, 0, 255), 4)

    Apple_count = 0
    text = "{}".format(qr_data)
    type_of_box = (text.endswith("Apple"))    
    if type_of_box == True:
      Apple_count=Apple_count+1
    print(Apple_count)
    

1 Answers1

0

You're issue is because you're changing a local variable to the callback function, not a class attribute. Instead, you should use the self keyword so the counter will increase across multiple callbacks.

class camera_1:

  def __init__(self):
    self.Apple_count = 0
    self.image_sub = rospy.Subscriber("/iris/usb_cam/image_raw", Image, self.callback)

  def callback(self,data):
    bridge = CvBridge()

    try:
      cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      rospy.logerr(e)

    (rows,cols,channels) = cv_image.shape
    
    image = cv_image

    resized_image = cv2.resize(image, (640, 640)) 

    
    qr_result = decode(resized_image)

    #print (qr_result)
    
    qr_data = qr_result[0].data
    print(qr_data)
    
    (x, y, w, h) = qr_result[0].rect

    cv2.rectangle(resized_image, (x, y), (x + w, y + h), (0, 0, 255), 4)

    text = "{}".format(qr_data)
    type_of_box = (text.endswith("Apple"))    
    if type_of_box == True:
      self.Apple_count += 1
    print(self.Apple_count)
    
BTables
  • 4,413
  • 2
  • 11
  • 30
  • Thankyou sir, its working now . Its increasing the value but the thing its continuously incrementing the value .is that because im using the rospy.spin() in main function? – white_panther Oct 13 '21 at 14:41
  • @white_panther It's not necessarily because of `rospy.spin`. The value will increase every time an image is received on the callback that matches your QR code conditions. Being a video stream, that will happen multiple times per second. I'd recommend a caching method with a low sample rate outside of the callback. Either that or you could keep track of QR codes you've already scanned and ignore those. – BTables Oct 13 '21 at 14:48
  • 1
    Okay ,and thanks once again. – white_panther Oct 13 '21 at 14:52