From 6cbbb5580e547f39c59fc7ebc38db476b9ec511a Mon Sep 17 00:00:00 2001 From: Arthur Golubtsov Date: Tue, 7 Apr 2020 17:06:05 +0300 Subject: [PATCH] docs (en): Update qr code scan instruction and script --- docs/en/camera.md | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/docs/en/camera.md b/docs/en/camera.md index a478592e..e5b533be 100644 --- a/docs/en/camera.md +++ b/docs/en/camera.md @@ -94,40 +94,39 @@ The obtained images can be viewed using [web_video_server](web_video_server.md). > **Hint** For high-speed recognition and positioning, it is better to use [ArUco markers](aruco.md). -To program actions of the copter upon detection of [QR codes](https://en.wikipedia.org/wiki/QR_code) you can use the [ZBar] library (http://zbar.sourceforge.net). It should be installed using pip: +To program actions of the copter upon detection of [QR codes](https://en.wikipedia.org/wiki/QR_code) you can use the [pyZBar](https://pypi.org/project/pyzbar/). It should be installed using pip: ```bash -sudo pip install zbar +sudo pip install pyzbar ``` QR codes recognition in Python: ```python -import cv2 -import zbar +import rospy +from pyzbar import pyzbar from cv_bridge import CvBridge from sensor_msgs.msg import Image bridge = CvBridge() -scanner = zbar.ImageScanner() -scanner.parse_config('enable') + +rospy.init_node('barcode_test') # Image subscriber callback function def image_callback(data): cv_image = bridge.imgmsg_to_cv2(data, 'bgr8') # OpenCV image - gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY, dstCn=0) - - pil = ImageZ.fromarray(gray) - raw = pil.tobytes() - - image = zbar.Image(320, 240, 'Y800', raw) # Image params - scanner.scan(image) - - for symbol in image: - # print detected QR code - print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data + barcodes = pyzbar.decode(cv_image) + for barcode in barcodes: + b_data = barcode.data.encode("utf-8") + b_type = barcode.type + (x, y, w, h) = barcode.rect + xc = x + w/2 + yc = y + h/2 + print ("Found {} with data {} with center at x={}, y={}".format(b_type, b_data, xc, yc)) image_sub = rospy.Subscriber('main_camera/image_raw', Image, image_callback, queue_size=1) + +rospy.spin() ``` The script will take up to 100% CPU capacity. To slow down the script artificially, you can use [throttling](http://wiki.ros.org/topic_tools/throttle) of frames from the camera, for example, at 5 Hz (`main_camera.launch`):