How to scan barcode using Raspberry pi camera module V2
This is the link to my previously asked question about barcode scanning.
To be more specific:
Hardware : Raspberry pi and Raspberry pi camera module v2 :
https://www.amazon.in/Raspberry-Camera-Board-Module- V2/dp/B071P2S8LG/ref=sr_1_5?s=computers&ie=UTF8&qid=1525942832&sr=1-5&keywords=raspberry+pi+camera+module
I have tried to scan bar code using
1) pyzbar library 2) SimpleCV 3) OpenCV and zbar
Using 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)
Using 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
Using OpenCV :
https://www.pyimagesearch.com/2014/11/24/detecting-barcodes-images-python-opencv/
I have tried all three ways to scan barcode but none of them is working. Using the last code, I am able to detect the barcode location in the image but cannot scan barcode.
Thanks in advance
go through this. it should work.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.