我使用二維碼掃描器來檢測和解碼,它可以正確解碼輸出。這里我使用“文本”作為二維碼的解碼輸出。如果它獲得與 APPLE 相同型別的框,則必須將 apple_count 增加到 1。通過使用此代碼,我只能為 apple_count 獲得“1”。我認為問題可能是因為回圈,但我無法解決它。請幫助我。謝謝
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)
uj5u.com熱心網友回復:
您的問題是因為您將區域變數更改為回呼函式,而不是類屬性。相反,您應該使用self關鍵字,以便計數器會在多個回呼中增加。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313190.html
