Skip to content

Commit d91bd61

Browse files
committed
Add a YOLO example
1 parent 3d70196 commit d91bd61

13 files changed

+1087
-0
lines changed
54.1 KB
Loading
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 dbr
15+
```
16+
- Obtain a [Dynamsoft Barcode Reader trial license](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr) and update your code with the provided license key:
17+
18+
```python
19+
from dbr import *
20+
21+
license_key = "LICENSE-KEY"
22+
BarcodeReader.init_license(license_key)
23+
reader = BarcodeReader()
24+
```
25+
26+
27+
## Usage
28+
29+
#### QR Detection
30+
31+
- From Image File:
32+
33+
```
34+
python3 opencv-yolo.py
35+
```
36+
37+
- From Camera:
38+
39+
```
40+
python3 opencv-yolo-camera.py
41+
```
42+
43+
![OpenCV YOLO for QR detection](https://www.dynamsoft.com/codepool/img/2020/11/opencv-dnn-yolo3-qr-detection.gif)
44+
45+
#### QR Reading with Dynamsoft Barcode Reader
46+
47+
Below is a sample code snippet for reading QR codes with the Dynamsoft Barcode Reader:
48+
49+
```py
50+
from dbr import *
51+
52+
license_key = "LICENSE-KEY"
53+
BarcodeReader.init_license(license_key)
54+
reader = BarcodeReader()
55+
settings = reader.reset_runtime_settings()
56+
settings = reader.get_runtime_settings()
57+
settings.region_bottom = bottom
58+
settings.region_left = left
59+
settings.region_right = right
60+
settings.region_top = top
61+
reader.update_runtime_settings(settings)
62+
63+
try:
64+
text_results = reader.decode_buffer(frame)
65+
66+
if text_results != None:
67+
for text_result in text_results:
68+
print("Barcode Format :")
69+
print(text_result.barcode_format_string)
70+
print("Barcode Text :")
71+
print(text_result.barcode_text)
72+
print("Localization Points : ")
73+
print(text_result.localization_result.localization_points)
74+
print("-------------")
75+
except BarcodeReaderError as bre:
76+
print(bre)
77+
```
78+
79+
- From Image File:
80+
81+
```
82+
python3 yolo-dbr.py
83+
```
84+
85+
- From Camera:
86+
87+
```
88+
python3 yolo-dbr-camera.py
89+
```
90+
91+
## Blog
92+
[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)
17.8 KB
Loading
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
from time import sleep
2+
import cv2 as cv
3+
import numpy as np
4+
import time
5+
from threading import Thread
6+
import queue
7+
from dbr import *
8+
9+
license_key = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
10+
BarcodeReader.init_license(license_key)
11+
reader = BarcodeReader()
12+
color = (0, 0, 255)
13+
thickness = 2
14+
15+
16+
def decodeframe(frame):
17+
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
26+
27+
28+
winName = 'QR Detection'
29+
30+
31+
def postprocess(frame, outs):
32+
if outs == None:
33+
return
34+
35+
for out in outs:
36+
points = out.localization_result.localization_points
37+
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)
44+
45+
46+
cap = cv.VideoCapture(0)
47+
48+
49+
class QueueFPS(queue.Queue):
50+
def __init__(self):
51+
queue.Queue.__init__(self)
52+
self.startTime = 0
53+
self.counter = 0
54+
55+
def put(self, v):
56+
queue.Queue.put(self, v)
57+
self.counter += 1
58+
if self.counter == 1:
59+
self.startTime = time.time()
60+
61+
def getFPS(self):
62+
return self.counter / (time.time() - self.startTime)
63+
64+
65+
process = True
66+
67+
#
68+
# Frames capturing thread
69+
#
70+
framesQueue = QueueFPS()
71+
72+
73+
def framesThreadBody():
74+
global framesQueue, process
75+
76+
while process:
77+
hasFrame, frame = cap.read()
78+
if not hasFrame:
79+
break
80+
framesQueue.put(frame)
81+
82+
83+
#
84+
# Frames processing thread
85+
#
86+
decodingQueue = QueueFPS()
87+
88+
89+
def processingThreadBody():
90+
global decodingQueue, process
91+
92+
while process:
93+
# Get a next frame
94+
frame = None
95+
try:
96+
frame = framesQueue.get_nowait()
97+
framesQueue.queue.clear()
98+
except queue.Empty:
99+
pass
100+
101+
if not frame is None:
102+
outs = decodeframe(frame)
103+
decodingQueue.put((frame, outs))
104+
sleep(0.03)
105+
106+
107+
framesThread = Thread(target=framesThreadBody)
108+
framesThread.start()
109+
110+
processingThread = Thread(target=processingThreadBody)
111+
processingThread.start()
112+
113+
#
114+
# Postprocessing and rendering loop
115+
#
116+
while cv.waitKey(1) < 0:
117+
try:
118+
# Request prediction first because they put after frames
119+
outs = decodingQueue.get_nowait()
120+
frame = outs[0]
121+
postprocess(frame, outs[1])
122+
123+
# Put efficiency information.
124+
if decodingQueue.counter > 1:
125+
label = 'Camera: %.2f FPS' % (framesQueue.getFPS())
126+
cv.putText(frame, label, (0, 15),
127+
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
128+
129+
label = 'DBR SDK: %.2f FPS' % (decodingQueue.getFPS())
130+
cv.putText(frame, label, (0, 30),
131+
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
132+
133+
label = 'Skipped frames: %d' % (
134+
framesQueue.counter - decodingQueue.counter)
135+
cv.putText(frame, label, (0, 45),
136+
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
137+
138+
cv.imshow(winName, frame)
139+
except queue.Empty:
140+
pass
141+
142+
143+
process = False
144+
framesThread.join()
145+
processingThread.join()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cv2 as cv
2+
import numpy as np
3+
import time
4+
from dbr import *
5+
6+
license_key = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="
7+
BarcodeReader.init_license(license_key)
8+
reader = BarcodeReader()
9+
color = (0, 0, 255)
10+
thickness = 2
11+
12+
13+
def decodeframe(frame):
14+
15+
try:
16+
text_results = reader.decode_buffer(frame)
17+
18+
if text_results != None:
19+
for text_result in text_results:
20+
print("Barcode Format :")
21+
print(text_result.barcode_format_string)
22+
print("Barcode Text :")
23+
print(text_result.barcode_text)
24+
print("Localization Points : ")
25+
print(text_result.localization_result.localization_points)
26+
print("-------------")
27+
points = text_result.localization_result.localization_points
28+
29+
cv.line(frame, points[0], points[1], color, thickness)
30+
cv.line(frame, points[1], points[2], color, thickness)
31+
cv.line(frame, points[2], points[3], color, thickness)
32+
cv.line(frame, points[3], points[0], color, thickness)
33+
34+
cv.putText(frame, text_result.barcode_text, (min([point[0] for point in points]), min(
35+
[point[1] for point in points])), cv.FONT_HERSHEY_SIMPLEX, 1, color, thickness)
36+
except BarcodeReaderError as bre:
37+
print(bre)
38+
39+
40+
# Load an frame
41+
frame = cv.imread("416x416.jpg")
42+
decodeframe(frame)
43+
44+
cv.imshow('QR Detection', frame)
45+
cv.waitKey()

0 commit comments

Comments
 (0)