使用raspberry pi相机模块v2扫描条形码

时间:2018-05-10 09:14:27

标签: python opencv raspberry-pi barcode-scanner

How to scan barcode using Raspberry pi camera module V2

这是我之前提到的有关条形码扫描问题的链接。

更具体一点:

硬件: 覆盆子pi 和 Raspberry pi相机模块v2:

https://www.amazon.in/Raspberry-Camera-Board-Module- V2 / DP / B071P2S8LG / REF = sr_1_5 S =&计算机放大器;即= UTF8&安培; QID = 1525942832&安培; SR = 1-5安培;关键字=覆盆子+ PI +相机+模块

我尝试使用

扫描条形码

1)pyzbar库 2)SimpleCV 3)OpenCV和zbar

使用Pyzbar:

from PIL import Image
import pyzbar.pyzbar as pyzbar

file_path = 'image.png'
with open(file_path, 'rb') as image_file:
image = Image.open(image_file)
image.load()

codes = pyzbar.decode(Image.open('image.png'))
print('QR codes: %s' % codes)

使用SimpleCV:

from SimpleCV import Color,Camera,Display

cam = Camera()  #starts the camera
display = Display() 

while(display.isNotDone()):

 img = cam.getImage() #gets image from the camera

 barcode = img.findBarcode() #finds barcode data from image
 if(barcode is not None): #if there is some data processed
   barcode = barcode[0] 
   result = str(barcode.data)
   print result #prints result of barcode in python shell
   barcode = [] #reset barcode data to empty set

 img.save(display) #shows the image on the screen

使用OpenCV:

https://www.pyimagesearch.com/2014/11/24/detecting-barcodes-images-python-opencv/

我已经尝试了三种方法来扫描条形码,但它们都没有工作。 使用最后一个代码,我能够检测到图像中的条形码位置,但无法扫描条形码。

提前致谢

1 个答案:

答案 0 :(得分:0)

进行此操作。应该可以。

from pyzbar.pyzbar import decode
from ftplib import FTP
import os
import numpy as np
import cv2
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
fourcc = cv2.VideoWriter_fourcc(*'X264')

def dec(frame):
     x=decode(frame)
     for i in x:
        (x, y, w, h) = i.rect
        cv2.rectangle(frame,(x, y),(x + w, y + h),(0, 0, 255),2)
        barcodeData = i.data.decode("utf-8")
        barcodeType = i.type
        return(barcodeData,barcodeType,1)
     return('','',0)

camera=PiCamera()
camera.resolution=(1296,730)
camera.framerate=20
rawCapture=PiRGBArray(camera)
time.sleep(0.1)
cv2.namedWindow("Image",cv2.WINDOW_NORMAL)

for frame in camera.capture_continuous(rawCapture,format="bgr",use_video_port=True):
    image=frame.array
    x,y,p=dec(image)
    cv2.imshow("Image",image)
    print(x)
    print(y)

    if cv2.waitKey(2) & 0xFF == ord('q'):
                break
    rawCapture.truncate(0)


        #cap.release()
cv2.destroyAllWindows()