Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2d38d13

Browse files
committed
add barcode scanner tutorial
1 parentc4a2301 commit2d38d13

File tree

8 files changed

+98
-0
lines changed

8 files changed

+98
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
frompyzbarimportpyzbar
2+
importcv2
3+
4+
5+
defdecode(image):
6+
# decodes all barcodes from an image
7+
decoded_objects=pyzbar.decode(image)
8+
forobjindecoded_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+
returnimage
18+
19+
20+
defdraw_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+
returnimage
30+
31+
32+
if__name__=="__main__":
33+
fromglobimportglob
34+
35+
barcodes=glob("barcode*.png")
36+
forbarcode_fileinbarcodes:
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)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
frompyzbarimportpyzbar
2+
importcv2
3+
4+
5+
defdraw_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+
returnimage
14+
15+
defdecode(image):
16+
# decodes all barcodes from an image
17+
decoded_objects=pyzbar.decode(image)
18+
forobjindecoded_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+
returnimage
27+
28+
29+
if__name__=="__main__":
30+
cap=cv2.VideoCapture(0)
31+
whileTrue:
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+
ifcv2.waitKey(1)==ord("q"):
40+
break
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
pyzbar

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp