Skip to content

Commit

Permalink
(qr) function was static, so moved it
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsBurgh committed May 7, 2024
1 parent 3d7313e commit a7b16d8
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions hmi/scripts/qr_code_decoder
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ def validate_image(qr_image: ndarray, qr_code_rect: tuple, threshold_percentage:
return False


def decode_qr_code(image: ndarray) -> str:
"""
Decode the QR code from the image.
:param image: image to run qr decoding on
"""

qr_list: List[Decoded] = qr_decode(image) # Gets all QR codes in the image
qr: Optional[Decoded] = None

# Stores all QRs that occupy the required percentage of the screen
for qr_i in qr_list:
if validate_image(image, qr_i.rect):
qr = qr_i
break

if not qr:
# qr code does not occupy the required percentage of the screen
rospy.logwarn("QR code does not occupy the required percentage of the screen.")
return ""

return qr.data.decode("utf-8")


class QRCodeDecode(AbstractHMIServer):
"""
QRCodeDecode class for decoding QR codes.
Expand All @@ -60,7 +84,7 @@ class QRCodeDecode(AbstractHMIServer):
def _image_callback(self, msg: CompressedImage) -> None:
# Will be called every time a new image is received
image = self._cv_bridge.compressed_imgmsg_to_cv2(msg)
result = self.decode_qr_code(image)
result = decode_qr_code(image)

# If we see a QR code
if result:
Expand Down Expand Up @@ -99,29 +123,6 @@ class QRCodeDecode(AbstractHMIServer):
self.reset_server()
return result

def decode_qr_code(self, image: ndarray) -> str:
"""
Decode the QR code from the image.
:param image: image to run qr decoding on
"""

qr_list: List[Decoded] = qr_decode(image) # Gets all QR codes in the image
qr: Optional[Decoded] = None

# Stores all QRs that occupy the required percentage of the screen
for qr_i in qr_list:
if validate_image(image, qr_i.rect):
qr = qr_i
break

if not qr:
# qr code does not occupy the required percentage of the screen
rospy.logwarn("QR code does not occupy the required percentage of the screen.")
return ""

return qr.data.decode("utf-8")

def reset_server(self) -> None:
"""
Re-initialize the server.
Expand Down

0 comments on commit a7b16d8

Please sign in to comment.