C:\> Rostislav Persion's Projects

.:: WEBCAM QR CODE IN PYTHON ::.
Python program that decodes QR codes in webcam






This is a simple Python program that decodes QR codes that it sees in the webcam. It can find the QR code in the details of an image and decodes it.

In order to get this program to run, you will need to run a few command line statements to install the necessary modules...

py -m pip install opencv-python
py -m pip install pillow
py -m pip install pyzbar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#FOR WEB CAM
import cv2  
import time

#FOR BAR CODE
from pyzbar.pyzbar import decode
from PIL import Image

cap = cv2.VideoCapture(0) 
ret, img = cap.read()
width, height, junk = img.shape

while True:  
  
    #READ WEBCAM
    ret, img = cap.read()  

    #DECODE BARDCODE
    result = decode(img)
    cnt = 0
    for i in result:

        cnt += 1

        decoded_str = i.data.decode("utf-8")
        (x, y, w, h) = i.rect
        print("BARCODE #" + str(cnt) + " " + decoded_str)
        
        #DRAW LABEL
        font = cv2.FONT_HERSHEY_SIMPLEX 
        org = (10, 20 + (cnt * 20)) 
        fontScale = 0.5
        color = (255, 0, 0) 
        thickness = 1
        cv2.putText(img, "BARCODE #" + str(cnt) + " = " + decoded_str, org, font, fontScale, color, thickness, cv2.LINE_AA)
        
        #DRAW RECTANLGE AROUND BARCODE
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)

    if cnt > 0:
        print("")


    #EXPORT
    cv2.imshow('PERSION - PYTHON WEBCAM QR DECODER',img) 
    time.sleep(0.1)

    #DO EVENTS
    k = cv2.waitKey(1) & 0xff
    if k == 27: 
        break

cap.release() 
cv2.destroyAllWindows()  


THIS IS THE CONSOLE VERSION...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#FOR WEB CAM
import cv2  
import time

#FOR BAR CODE
from pyzbar.pyzbar import decode
from PIL import Image

#FOR TTS
import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) 



cap = cv2.VideoCapture(0) 
ret, img = cap.read()
width, height, junk = img.shape

while True:  
  
    #READ WEBCAM
    ret, img = cap.read()  

    #DECODE BARDCODE
    cnt = 0
    result = decode(img)
    for i in result:

        cnt += 1

        decoded_str = i.data.decode("utf-8")
        (x, y, w, h) = i.rect
        
        print("BARCODE #" + str(cnt) + " " + decoded_str)

        #TTS OUTPUT
        engine.say("BARCODE #" + str(cnt) + " " + decoded_str)
        engine.runAndWait()
    
    if cnt > 0:
        print("")

    time.sleep(0.5)