Skip to content

Commit 5f075b9

Browse files
committed
Added a script that scans both qr and barcodes, and added a readme file as well
1 parent 84b18d1 commit 5f075b9

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## How to run
2+
1. Clone the repository
3+
```
4+
git clone [email protected]:GDSC-RCCIIT/General-Purpose-Scripts.git
5+
```
6+
2. Install the script requirements
7+
```
8+
pip install pyzbar
9+
pip install numpy
10+
pip install opencv-python
11+
```
12+
3. Navigate to `QR and Barcode Scanner` directory
13+
```
14+
cd General-Purpose-Scripts/scripts/QR and Barcode Scanner
15+
```
16+
4. Replace "frame.png" with the image path of the QR/Barcode or, with the name of the png file if it is in the same directory as the script.
17+
### Note
18+
The png file can also be placed in the same directory as the script and renamed to "frame.png" to avoid making changes to the code.
19+
5. Run the script to scan the QR/Barcode
20+
```
21+
python scanner.py
22+
```
23+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from __future__ import print_function
2+
import pyzbar.pyzbar as pyzbar
3+
import numpy as np
4+
import cv2
5+
6+
# Image Path Or Image Name if in Same Directory or Replace the png file with your image
7+
8+
img_path = "frame.png"
9+
10+
11+
def decode(im):
12+
# Find barcodes and QR codes
13+
decodedObjects = pyzbar.decode(im)
14+
15+
# Print results
16+
for obj in decodedObjects:
17+
print("Type : ", obj.type)
18+
s = str(obj.data)
19+
s = s.replace(s[0], "")
20+
s = s.replace("'", "")
21+
print("Data : ", s, "\n")
22+
23+
return decodedObjects
24+
25+
26+
# Display barcode and QR code location
27+
def display(im, decodedObjects):
28+
29+
# Loop over all decoded objects
30+
for decodedObject in decodedObjects:
31+
points = decodedObject.polygon
32+
33+
# If the points do not form a quad, find convex hull
34+
if len(points) > 4:
35+
hull = cv2.convexHull(
36+
np.array([point for point in points], dtype=np.float32)
37+
)
38+
hull = list(map(tuple, np.squeeze(hull)))
39+
else:
40+
hull = points
41+
42+
# Number of points in the convex hull
43+
n = len(hull)
44+
45+
# Draw the convext hull
46+
for j in range(0, n):
47+
cv2.line(im, hull[j], hull[(j + 1) % n], (255, 0, 0), 3)
48+
49+
# Display results
50+
cv2.imshow("Results", im)
51+
cv2.waitKey(0)
52+
print("Done")
53+
54+
55+
# Main
56+
if __name__ == "__main__":
57+
58+
# Read image
59+
im = cv2.imread(img_path)
60+
61+
decodedObjects = decode(im)
62+
63+
display(im, decodedObjects)

0 commit comments

Comments
 (0)