Skip to content

Commit 8933aa1

Browse files
committed
Update yolo_qr
1 parent 3bad356 commit 8933aa1

18 files changed

Lines changed: 229 additions & 222 deletions

examples/official/9.x/yolo_qr/README.md

Lines changed: 0 additions & 92 deletions
This file was deleted.

examples/official/9.x/yolo_qr/dbr-only.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

examples/official/camera_file/camera.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ def on_captured_result_received(self, captured_result):
7575
y3 = location.points[2].y
7676
x4 = location.points[3].x
7777
y4 = location.points[3].y
78+
pts = np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], np.int32).reshape((-1, 1, 2))
7879
cv2.drawContours(
79-
frame, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
80+
frame, [pts], 0, (0, 255, 0), 2)
8081

8182
cv2.putText(frame, text, (x1, y1),
8283
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

examples/official/camera_file/file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@
6767
print("({}, {})".format(x4, y4))
6868
print("-------------------------------------------------")
6969

70+
pts = np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], np.int32).reshape((-1, 1, 2))
7071
cv2.drawContours(
71-
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
72+
cv_image, [pts], 0, (0, 255, 0), 2)
7273

7374
cv2.putText(cv_image, text, (x1, y1 - 10),
7475
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# QR Detection with OpenCV and YOLO Model in Python
2+
This repository provides samples demonstrating how to detect QR codes using **YOLO** and how to read QR codes with the [Dynamsoft Barcode Reader](https://www.dynamsoft.com/barcode-reader/overview/).
3+
4+
## Prerequisites
5+
- OpenCV 4.x
6+
7+
```
8+
pip install opencv-python
9+
```
10+
11+
- Dynamsoft Barcode Reader
12+
13+
```
14+
pip install dynamsoft-capture-vision-bundle
15+
```
16+
- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform) and update your code with the provided license key:
17+
18+
```python
19+
from dynamsoft_capture_vision_bundle import *
20+
21+
errorCode, errorMsg = LicenseManager.init_license("LICENSE-KEY")
22+
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
23+
print("License initialization failed: ErrorCode:",
24+
errorCode, ", ErrorString:", errorMsg)
25+
26+
cvr_instance = CaptureVisionRouter()
27+
```
28+
29+
30+
## Usage
31+
32+
#### QR Detection
33+
34+
- From Image File:
35+
36+
```
37+
python3 opencv-yolo.py
38+
```
39+
40+
- From Camera:
41+
42+
```
43+
python3 opencv-yolo-camera.py
44+
```
45+
46+
![OpenCV YOLO for QR detection](https://www.dynamsoft.com/codepool/img/2020/11/opencv-dnn-yolo3-qr-detection.gif)
47+
48+
#### QR Reading with Dynamsoft Barcode Reader
49+
50+
Below is a sample code snippet for reading QR codes with the Dynamsoft Barcode Reader:
51+
52+
```py
53+
from dynamsoft_capture_vision_bundle import *
54+
55+
errorCode, errorMsg = LicenseManager.init_license("LICENSE-KEY")
56+
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
57+
print("License initialization failed: ErrorCode:",
58+
errorCode, ", ErrorString:", errorMsg)
59+
60+
cvr_instance = CaptureVisionRouter()
61+
62+
error_code, error_msg, settings = cvr_instance.get_simplified_settings(EnumPresetTemplate.PT_READ_BARCODES.value)
63+
quad = Quadrilateral()
64+
quad.points = [Point(left, top), Point(right, top), Point(right, bottom), Point(left, bottom)]
65+
settings.roi = quad
66+
settings.roi_measured_in_percentage = False
67+
cvr_instance.update_settings(EnumPresetTemplate.PT_READ_BARCODES.value, settings)
68+
69+
result = cvr_instance.capture(frame, EnumPresetTemplate.PT_READ_BARCODES.value)
70+
71+
items = result.get_items()
72+
for item in items:
73+
print("Barcode Format :")
74+
print(item.get_format_string())
75+
print("Barcode Text :")
76+
print(item.get_text())
77+
78+
location = item.get_location()
79+
x1 = location.points[0].x
80+
y1 = location.points[0].y
81+
x2 = location.points[1].x
82+
y2 = location.points[1].y
83+
x3 = location.points[2].x
84+
y3 = location.points[2].y
85+
x4 = location.points[3].x
86+
y4 = location.points[3].y
87+
88+
print("Location Points:")
89+
print("({}, {})".format(x1, y1))
90+
print("({}, {})".format(x2, y2))
91+
print("({}, {})".format(x3, y3))
92+
print("({}, {})".format(x4, y4))
93+
print("-------------------------------------------------")
94+
```
95+
96+
- From Image File:
97+
98+
```
99+
python3 yolo-dbr.py
100+
```
101+
102+
- From Camera:
103+
104+
```
105+
python3 yolo-dbr-camera.py
106+
```
107+
108+
## Blog
109+
[How to Detect and Decode QR Code with YOLO, OpenCV, and Dynamsoft Barcode Reader](https://www.dynamsoft.com/codepool/qr-code-detect-decode-yolo-opencv.html)

examples/official/9.x/yolo_qr/dbr-only-camera.py renamed to examples/official/yolo_qr/dbr-only-camera.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,48 @@
44
import time
55
from threading import Thread
66
import queue
7-
from dbr import *
7+
from dynamsoft_capture_vision_bundle import *
8+
9+
errorCode, errorMsg = LicenseManager.init_license(
10+
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
11+
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
12+
print("License initialization failed: ErrorCode:",
13+
errorCode, ", ErrorString:", errorMsg)
14+
15+
cvr_instance = CaptureVisionRouter()
816

9-
license_key = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
10-
BarcodeReader.init_license(license_key)
11-
reader = BarcodeReader()
1217
color = (0, 0, 255)
1318
thickness = 2
1419

1520

1621
def decodeframe(frame):
1722

18-
try:
19-
outs = reader.decode_buffer(frame)
20-
if outs != None:
21-
return outs
22-
except BarcodeReaderError as bre:
23-
print(bre)
24-
25-
return None
23+
result = cvr_instance.capture(frame, EnumPresetTemplate.PT_READ_BARCODES.value)
24+
return result
2625

2726

2827
winName = 'QR Detection'
2928

3029

31-
def postprocess(frame, outs):
32-
if outs == None:
33-
return
30+
def postprocess(frame, result):
31+
items = result.get_items()
32+
for item in items:
33+
location = item.get_location()
34+
x1 = location.points[0].x
35+
y1 = location.points[0].y
36+
x2 = location.points[1].x
37+
y2 = location.points[1].y
38+
x3 = location.points[2].x
39+
y3 = location.points[2].y
40+
x4 = location.points[3].x
41+
y4 = location.points[3].y
3442

35-
for out in outs:
36-
points = out.localization_result.localization_points
43+
pts = np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], np.int32).reshape((-1, 1, 2))
44+
cv.drawContours(
45+
frame, [pts], 0, (0, 255, 0), 2)
3746

38-
cv.line(frame, points[0], points[1], color, thickness)
39-
cv.line(frame, points[1], points[2], color, thickness)
40-
cv.line(frame, points[2], points[3], color, thickness)
41-
cv.line(frame, points[3], points[0], color, thickness)
42-
cv.putText(frame, out.barcode_text, (min([point[0] for point in points]), min(
43-
[point[1] for point in points])), cv.FONT_HERSHEY_SIMPLEX, 1, color, thickness)
47+
cv.putText(frame, item.get_text(), (x1, y1 - 10),
48+
cv.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness)
4449

4550

4651
cap = cv.VideoCapture(0)
@@ -52,8 +57,8 @@ def __init__(self):
5257
self.startTime = 0
5358
self.counter = 0
5459

55-
def put(self, v):
56-
queue.Queue.put(self, v)
60+
def put(self, item, block=True, timeout=None):
61+
queue.Queue.put(self, item, block=block, timeout=timeout)
5762
self.counter += 1
5863
if self.counter == 1:
5964
self.startTime = time.time()

0 commit comments

Comments
 (0)