python條碼識別

1.zxing識別二維碼、條形碼python

 

項目地址:https://github.com/oostendo/python-zxinggit

2.pyzbargithub

環境:ide

  • python 3.7
  • Win7

依賴包安裝:測試

1字體

2spa

3日誌

4code

5orm

pip install pillow

pip install opencv-python

pip install opencv-contrib-python

pip install numpy

pip instal pyzbar

 

首先使用cv2打開攝像頭進行測試

1

2

3

4

5

6

7

8

9

10

11

12

import numpy as np

import cv2

capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)

capture.set(3,640)

capture.set(4,480)

while True:

    # 讀取攝像頭中的圖像,ok爲是否讀取成功的判斷參數

    ret,img = capture.read()

    cv2.imshow('frame', img)

    = cv2.waitKey(1)

    if == 27:    # 'ESC'關閉

        break

 

若是看到屏幕出現一個窗口,上面顯示着你的攝像頭所拍攝到的畫面說明測試成功

而後就直接上代碼:

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

55

56

57

58

59

60

61

#written by DY

#http://dyblog.tk

#e-mail:duyun888888@qq.com

import cv2

from PIL import Image, ImageDraw, ImageFont

import csv

import pyzbar.pyzbar as pyzbar

import numpy

import time

#-----------------time----------

time1 = (time.strftime("%Y!%m@%d(%H*%M)%S`", time.localtime()))

time2 = time1.replace('!''年')

time3 = time2.replace('@''月')

time4 = time3.replace('(''日')

time5 = time4.replace('*''時')

time6 = time5.replace(')''分')

timenow = time6.replace('`''秒')

#-----------------time done----

barcodeData1 = ''

 

found = set()

capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)

Files = "qrcode+barcode.dat"#新建一個日誌,方便查閱(不須要能夠刪除)

while(1):

    ret,frame = capture.read()

    test = pyzbar.decode(frame)

    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    barcodes = pyzbar.decode(gray)

    for barcode in barcodes:

        (x, y, w, h) = barcode.rect

        cv2.rectangle(frame, (x, y), (x + w, y + h), (2552550), 2)

        barcodeData = barcode.data.decode("utf-8")

        barcodeType = barcode.type

        if barcodeData=='' or barcodeData!=barcodeData1:

            barcodeData1 = barcodeData

            #======================能夠不須要,這是在圖片上進行標註,裏面採用了字體文件==============================

            img_PIL = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

            font = ImageFont.truetype('arialbd.ttf'25)#前面是字體文件,後面是字號

            fillColor = (0,255,0)

            position = (x, y-25)

            strl = barcodeData

            draw = ImageDraw.Draw(img_PIL)

            draw.text(position, strl,font=font,fill=fillColor)

            img_PIL.save('Identification_results.jpg','jpeg')#保存一張標記過的圖片

            #====================================================

            print("Recognize result>>> type: {0}  content: {1}".format(barcodeType, barcodeData))

        else:

            pass

        if barcodeType not in found or barcodeData not in found:

            with open(Files,'a+') as w:

                csv_write = csv.writer(w)

                date = ['類型:'+barcodeType+'  識別結果:'+barcodeData+'   時間:'+timenow]

                csv_write.writerow(date)

            found.add(barcodeData)

    cv2.imshow('qrcode+barcode',frame)

    = cv2.waitKey(1)

    if == 27:

        break

#written by DY

#http://dyblog.tk

#e-mail:duyun888888@qq.com

相關文章
相關標籤/搜索