I found a code that can scan a barcode using the Raspberry Pi camera V2.1.
It works as expected and can detect a barcode when I present it to the camera. But if I move the camera around a little, there is a lag in the video. I tried increasing the camera. framerate but that doesn't do anything. Neither does change the resolution. Even if I remove the dec()
function, the video still looks laggy.
How can I improve the camera framerate so it does not lag?
Also the code opens up a window where I can see the video. For now it is useful for debugging, but I was wondering how I could stop the Pi from opening the video window later on?
from ftplib import FTP
from pyzbar.pyzbar import decode
import os, sys, cv2
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera
import imutils, time
detectedBarcode = False
def dec(frame):
global detectedBarcode
x=decode(frame)
for i in x:
detectedBarcode = True
(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
print(barcodeData, type(barcodeData))
#sys.exit()
return(barcodeData,barcodeType,1)
return('','',0)
def cameraReader():
fourcc = cv2.VideoWriter_fourcc(*'X264')
camera=PiCamera()
camera.resolution=(1296,730)
camera.framerate = 30
rawCapture=PiRGBArray(camera)
cv2.namedWindow("QR Scanner",cv2.WINDOW_NORMAL)
global detectedBarcode
avg = None
for frame in camera.capture_continuous(rawCapture,format="bgr",use_video_port=False):
image=frame.array
cv2.line(image, (650, 0), (650, 1000), (0, 255,0), 2)
x,y,p=dec(image)
cv2.imshow("QR Scanner",image)
if cv2.waitKey(2) & 0xFF == ord('q'):
break
rawCapture.truncate(0)
#cap.release()
cv2.destroyAllWindows()
cameraReader()