Home > Back-end >  how can i increment the count of same type of box if it gets the same type of box after scanning the
how can i increment the count of same type of box if it gets the same type of box after scanning the

Time:10-14

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)
    

CodePudding user response:

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)
    
  • Related