Skip to content

Commit 2d38d13

Browse files
committed
add barcode scanner tutorial
1 parent c4a2301 commit 2d38d13

File tree

8 files changed

+98
-0
lines changed

8 files changed

+98
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5050
- [How to Blur Faces in Images using OpenCV in Python](https://www.thepythoncode.com/article/blur-faces-in-images-using-opencv-in-python). ([code](machine-learning/blur-faces))
5151
- [Skin Cancer Detection using TensorFlow in Python](https://www.thepythoncode.com/article/skin-cancer-detection-using-tensorflow-in-python). ([code](machine-learning/skin-cancer-detection))
5252
- [How to Perform Malaria Cells Classification using TensorFlow 2 and Keras in Python](https://www.thepythoncode.com/article/malaria-cells-classification). ([code](machine-learning/malaria-classification))
53+
- [How to Make a Barcode Reader in Python](https://www.thepythoncode.com/article/making-a-barcode-scanner-in-python). ([code](general/barcode-reader))
5354
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
5455
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
5556
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).

general/barcode-reader/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [How to Make a Barcode Reader in Python](https://www.thepythoncode.com/article/making-a-barcode-scanner-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- To detect barcodes of the images in this directory:
5+
```
6+
python barcode_reader.py
7+
```
8+
- To detect barcodes live using your camera:
9+
```
10+
python live_barcode_reader.py
11+
```

general/barcode-reader/barcode1.png

12.4 KB
Loading

general/barcode-reader/barcode2.png

3.21 KB
Loading

general/barcode-reader/barcode3.png

5.22 KB
Loading
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from pyzbar import pyzbar
2+
import cv2
3+
4+
5+
def decode(image):
6+
# decodes all barcodes from an image
7+
decoded_objects = pyzbar.decode(image)
8+
for obj in decoded_objects:
9+
# draw the barcode
10+
print("detected barcode:", obj)
11+
image = draw_barcode(obj, image)
12+
# print barcode type & data
13+
print("Type:", obj.type)
14+
print("Data:", obj.data)
15+
print()
16+
17+
return image
18+
19+
20+
def draw_barcode(decoded, image):
21+
# n_points = len(decoded.polygon)
22+
# for i in range(n_points):
23+
# image = cv2.line(image, decoded.polygon[i], decoded.polygon[(i+1) % n_points], color=(0, 255, 0), thickness=5)
24+
# uncomment above and comment below if you want to draw a polygon and not a rectangle
25+
image = cv2.rectangle(image, (decoded.rect.left, decoded.rect.top),
26+
(decoded.rect.left + decoded.rect.width, decoded.rect.top + decoded.rect.height),
27+
color=(0, 255, 0),
28+
thickness=5)
29+
return image
30+
31+
32+
if __name__ == "__main__":
33+
from glob import glob
34+
35+
barcodes = glob("barcode*.png")
36+
for barcode_file in barcodes:
37+
# load the image to opencv
38+
img = cv2.imread(barcode_file)
39+
# decode detected barcodes & get the image
40+
# that is drawn
41+
img = decode(img)
42+
# show the image
43+
cv2.imshow("img", img)
44+
cv2.waitKey(0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pyzbar import pyzbar
2+
import cv2
3+
4+
5+
def draw_barcode(decoded, image):
6+
# n_points = len(decoded.polygon)
7+
# for i in range(n_points):
8+
# image = cv2.line(image, decoded.polygon[i], decoded.polygon[(i+1) % n_points], color=(0, 255, 0), thickness=5)
9+
image = cv2.rectangle(image, (decoded.rect.left, decoded.rect.top),
10+
(decoded.rect.left + decoded.rect.width, decoded.rect.top + decoded.rect.height),
11+
color=(0, 255, 0),
12+
thickness=5)
13+
return image
14+
15+
def decode(image):
16+
# decodes all barcodes from an image
17+
decoded_objects = pyzbar.decode(image)
18+
for obj in decoded_objects:
19+
# draw the barcode
20+
image = draw_barcode(obj, image)
21+
# print barcode type & data
22+
print("Type:", obj.type)
23+
print("Data:", obj.data)
24+
print()
25+
26+
return image
27+
28+
29+
if __name__ == "__main__":
30+
cap = cv2.VideoCapture(0)
31+
while True:
32+
# read the frame from the camera
33+
_, frame = cap.read()
34+
# decode detected barcodes & get the image
35+
# that is drawn
36+
frame, decoded_objects = decode(frame)
37+
# show the image in the window
38+
cv2.imshow("frame", frame)
39+
if cv2.waitKey(1) == ord("q"):
40+
break
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
pyzbar

0 commit comments

Comments
 (0)