Limit combinations of backends and targets in demos and benchmark (#145)
Browse files* limit backend and target combination in demos and benchmark
* simpler version checking
- demo.py +41 -16
- wechatqrcode.py +10 -10
demo.py
CHANGED
|
@@ -11,23 +11,43 @@ import cv2 as cv
|
|
| 11 |
|
| 12 |
from wechatqrcode import WeChatQRCode
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
parser = argparse.ArgumentParser(
|
| 23 |
description="WeChat QR code detector for detecting and parsing QR code (https://github.com/opencv/opencv_contrib/tree/master/modules/wechat_qrcode)")
|
| 24 |
-
parser.add_argument('--input', '-i', type=str,
|
| 25 |
-
|
| 26 |
-
parser.add_argument('--
|
| 27 |
-
|
| 28 |
-
parser.add_argument('--
|
| 29 |
-
|
| 30 |
-
parser.add_argument('--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
args = parser.parse_args()
|
| 32 |
|
| 33 |
def visualize(image, res, points, points_color=(0, 255, 0), text_color=(0, 255, 0), fps=None):
|
|
@@ -56,11 +76,16 @@ def visualize(image, res, points, points_color=(0, 255, 0), text_color=(0, 255,
|
|
| 56 |
|
| 57 |
|
| 58 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
| 59 |
# Instantiate WeChatQRCode
|
| 60 |
model = WeChatQRCode(args.detect_prototxt_path,
|
| 61 |
args.detect_model_path,
|
| 62 |
args.sr_prototxt_path,
|
| 63 |
-
args.sr_model_path
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# If input is an image:
|
| 66 |
if args.input is not None:
|
|
@@ -107,4 +132,4 @@ if __name__ == '__main__':
|
|
| 107 |
# Visualize results in a new window
|
| 108 |
cv.imshow('WeChatQRCode Demo', frame)
|
| 109 |
|
| 110 |
-
tm.reset()
|
|
|
|
| 11 |
|
| 12 |
from wechatqrcode import WeChatQRCode
|
| 13 |
|
| 14 |
+
# Check OpenCV version
|
| 15 |
+
assert cv.__version__ >= "4.7.0", \
|
| 16 |
+
"Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python"
|
| 17 |
+
|
| 18 |
+
# Valid combinations of backends and targets
|
| 19 |
+
backend_target_pairs = [
|
| 20 |
+
[cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU],
|
| 21 |
+
[cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA],
|
| 22 |
+
[cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16],
|
| 23 |
+
[cv.dnn.DNN_BACKEND_TIMVX, cv.dnn.DNN_TARGET_NPU],
|
| 24 |
+
[cv.dnn.DNN_BACKEND_CANN, cv.dnn.DNN_TARGET_NPU]
|
| 25 |
+
]
|
| 26 |
|
| 27 |
parser = argparse.ArgumentParser(
|
| 28 |
description="WeChat QR code detector for detecting and parsing QR code (https://github.com/opencv/opencv_contrib/tree/master/modules/wechat_qrcode)")
|
| 29 |
+
parser.add_argument('--input', '-i', type=str,
|
| 30 |
+
help='Usage: Set path to the input image. Omit for using default camera.')
|
| 31 |
+
parser.add_argument('--detect_prototxt_path', type=str, default='detect_2021sep.prototxt',
|
| 32 |
+
help='Usage: Set path to detect.prototxt.')
|
| 33 |
+
parser.add_argument('--detect_model_path', type=str, default='detect_2021sep.caffemodel',
|
| 34 |
+
help='Usage: Set path to detect.caffemodel.')
|
| 35 |
+
parser.add_argument('--sr_prototxt_path', type=str, default='sr_2021sep.prototxt',
|
| 36 |
+
help='Usage: Set path to sr.prototxt.')
|
| 37 |
+
parser.add_argument('--sr_model_path', type=str, default='sr_2021sep.caffemodel',
|
| 38 |
+
help='Usage: Set path to sr.caffemodel.')
|
| 39 |
+
parser.add_argument('--backend_target', '-bt', type=int, default=0,
|
| 40 |
+
help='''Choose one of the backend-target pair to run this demo:
|
| 41 |
+
{:d}: (default) OpenCV implementation + CPU,
|
| 42 |
+
{:d}: CUDA + GPU (CUDA),
|
| 43 |
+
{:d}: CUDA + GPU (CUDA FP16),
|
| 44 |
+
{:d}: TIM-VX + NPU,
|
| 45 |
+
{:d}: CANN + NPU
|
| 46 |
+
'''.format(*[x for x in range(len(backend_target_pairs))]))
|
| 47 |
+
parser.add_argument('--save', '-s', action='store_true',
|
| 48 |
+
help='Usage: Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.')
|
| 49 |
+
parser.add_argument('--vis', '-v', action='store_true',
|
| 50 |
+
help='Usage: Specify to open a new window to show results. Invalid in case of camera input.')
|
| 51 |
args = parser.parse_args()
|
| 52 |
|
| 53 |
def visualize(image, res, points, points_color=(0, 255, 0), text_color=(0, 255, 0), fps=None):
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
if __name__ == '__main__':
|
| 79 |
+
backend_id = backend_target_pairs[args.backend_target][0]
|
| 80 |
+
target_id = backend_target_pairs[args.backend_target][1]
|
| 81 |
+
|
| 82 |
# Instantiate WeChatQRCode
|
| 83 |
model = WeChatQRCode(args.detect_prototxt_path,
|
| 84 |
args.detect_model_path,
|
| 85 |
args.sr_prototxt_path,
|
| 86 |
+
args.sr_model_path,
|
| 87 |
+
backendId=backend_id,
|
| 88 |
+
targetId=target_id)
|
| 89 |
|
| 90 |
# If input is an image:
|
| 91 |
if args.input is not None:
|
|
|
|
| 132 |
# Visualize results in a new window
|
| 133 |
cv.imshow('WeChatQRCode Demo', frame)
|
| 134 |
|
| 135 |
+
tm.reset()
|
wechatqrcode.py
CHANGED
|
@@ -8,27 +8,27 @@ import numpy as np
|
|
| 8 |
import cv2 as cv # needs to have cv.wechat_qrcode_WeChatQRCode, which requires compile from source with opencv_contrib/modules/wechat_qrcode
|
| 9 |
|
| 10 |
class WeChatQRCode:
|
| 11 |
-
def __init__(self, detect_prototxt_path, detect_model_path, sr_prototxt_path, sr_model_path):
|
| 12 |
self._model = cv.wechat_qrcode_WeChatQRCode(
|
| 13 |
detect_prototxt_path,
|
| 14 |
detect_model_path,
|
| 15 |
sr_prototxt_path,
|
| 16 |
sr_model_path
|
| 17 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@property
|
| 20 |
def name(self):
|
| 21 |
return self.__class__.__name__
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def setTarget(self, target_id):
|
| 29 |
-
# self._model.setPreferableTarget(target_id)
|
| 30 |
-
if target_id != 0:
|
| 31 |
raise NotImplementedError("Target {} is not supported by cv.wechat_qrcode_WeChatQRCode()")
|
| 32 |
|
| 33 |
def infer(self, image):
|
| 34 |
-
return self._model.detectAndDecode(image)
|
|
|
|
| 8 |
import cv2 as cv # needs to have cv.wechat_qrcode_WeChatQRCode, which requires compile from source with opencv_contrib/modules/wechat_qrcode
|
| 9 |
|
| 10 |
class WeChatQRCode:
|
| 11 |
+
def __init__(self, detect_prototxt_path, detect_model_path, sr_prototxt_path, sr_model_path, backendId=0, targetId=0):
|
| 12 |
self._model = cv.wechat_qrcode_WeChatQRCode(
|
| 13 |
detect_prototxt_path,
|
| 14 |
detect_model_path,
|
| 15 |
sr_prototxt_path,
|
| 16 |
sr_model_path
|
| 17 |
)
|
| 18 |
+
if backendId != 0:
|
| 19 |
+
raise NotImplementedError("Backend {} is not supported by cv.wechat_qrcode_WeChatQRCode()".format(backendId))
|
| 20 |
+
if targetId != 0:
|
| 21 |
+
raise NotImplementedError("Target {} is not supported by cv.wechat_qrcode_WeChatQRCode()")
|
| 22 |
|
| 23 |
@property
|
| 24 |
def name(self):
|
| 25 |
return self.__class__.__name__
|
| 26 |
|
| 27 |
+
def setBackendAndTarget(self, backendId, targetId):
|
| 28 |
+
if backendId != 0:
|
| 29 |
+
raise NotImplementedError("Backend {} is not supported by cv.wechat_qrcode_WeChatQRCode()".format(backendId))
|
| 30 |
+
if targetId != 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
raise NotImplementedError("Target {} is not supported by cv.wechat_qrcode_WeChatQRCode()")
|
| 32 |
|
| 33 |
def infer(self, image):
|
| 34 |
+
return self._model.detectAndDecode(image)
|