Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

Wednesday, 19 February 2020

Simple Bar Code Reader using a Raspberry Pi

Reading Bar Codes with the Raspberry Pi

Installation 

Install OpenCV
sudo apt get install python-opencv libopencv-dev 
You might find it is already installed.

Install Python bindings 

pip3 install opencv-python 

Install additional libraries 

sudo apt-get install libcblas-dev 
sudo apt-get install libhdf5-dev -y
 && sudo apt-get install libhdf5-serial-dev -y 
 && sudo apt-get install libatlas-base-dev -y
 && sudo apt-get install libjasper-dev -y
 && sudo apt-get install libqtgui4 -y
 && sudo apt-get install libqt4-test -y 

Install ZBAR and PYZBAR 

According to one of the references, zbar does not have a python3 version, however the pyzbar works with Python3.
sudo apt-get install libzbar0 
pip3 install pyzbar 
Remember to use pip3 not pip to install pyzbar for Python3 (your installation may vary - pip defaults to Python2 in most installations).

Test program 

from pyzbar import pyzbar
import cv2

imagefile="/home/pi/Documents/Python/barcodetest.jpg"

print(imagefile)
# load the input image
image = cv2.imread(imagefile)
# find the barcodes in the image and decode each of the barcodes
barcodes = pyzbar.decode(image)

# loop over the detected barcodes
for barcode in barcodes:
    # extract the bounding box location of the barcode and draw the
    # bounding box surrounding the barcode on the image
    (x, y, w, h) = barcode.rect
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # the barcode data is a bytes object so if we want to draw it on
    # our output image we need to convert it to a string first
    barcodeData = barcode.data.decode("utf-8")
    barcodeType = barcode.type
    # draw the barcode data and barcode type on the image
    text = "{} ({})".format(barcodeData, barcodeType)
    cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
        0.5, (0, 0, 255), 2)
    # print the barcode type and data to the terminal
    print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))

# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)

Troubleshooting

There may be issues with the version of numpy installed by the above.
If you getr an import error, remove the installed version of numpy and replace it with one from the Raspbian repository.

pip3 uninstall numpy  # remove previously installed version
apt install python3-numpy
https://numpy.org/devdocs/user/troubleshooting-importerror.html

References 

https://www.pyimagesearch.com/2018/05/21/an-opencv-barcode-and-qr-code-scanner-with-zbar/
https://www.learnopencv.com/barcode-and-qr-code-scanner-using-zbar-and-opencv/
https://www.raspberrypi.org/forums/viewtopic.php?p=848265 
https://stackoverflow.com/questions/19876079/cannot-find-module-cv2-when-using-opencv

Wednesday, 15 May 2019

Using the built-in camera with Raspbian and OpenCV

I have an elderly Samsung Netbook which is running Raspbian Stretch as a LiveUSB.

The netbook has a built in camera, and I thought I would follow up the post on using a Raspberry Pi and a Webcam with one on the Netbook.

Installation

Follow the instructions from the previous post.

Taking a photograph

Open up a Python editor (I used IDLE 3).
Create a new file.
Copy and paste the code from the previous post.
Save the program to your usual directory).
Run the program.

Footnote

It also was a convenient check that the previous post worked on a fresh install. The post required a slight edit as the PIP3 command needed to be run under SUDO.

USB Webcam using a Raspberry Pi and Python - test

Introduction

USB webcams are very cheap, and in many cases you may already own one. Though the Official Raspberry Pi Camera is often far superior (many "High Resolution" webcams are 'Nineties era VGA resolution - 640x480), the fact that webcams are cheap and cased can make them very useful for some projects.

This posting covers the simplest of Python programs to take a photograph using the OpenCV library.

Installation

Install OpenCV

sudo apt-get install python-opencv libopencv-dev

You might find it is already installed.

Install Python bindings

Remember that pip probably installs for Python 2.x, so you will need to use pip3.

sudo pip3 install opencv-python

Please note that if you forget the sudo, you do not get an error, but it also does not work (you get an error "ImportError: No module named 'cv2'").

Taking a photograph

The following Python code takes a photograph and saves it to a file in the (pi) user's home directory.

The program takes a number of frames to allow the camera and its software to determine the best settings for exposure etc.

# Test USB web cam with OpenCV

# Imports
import cv2

# Set camera to be the default webcam
camera_port=0

# Set warm up number of frames
skip_frames=7

# Destination file
file="/home/pi/image.jpg"

# Set up camera properties
camera=cv2.VideoCapture(camera_port)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640) 
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)

# Function to get image
def get_image():
    retval,im=camera.read()
    return im

# Test USB web cam
print("Starting photograph")

# Skip frames to allow camera to settle down
for skipping in range(skip_frames):
    temp=get_image()
print("Taking photograph")

camera_capture=get_image()

# Write image to file
cv2.imwrite(file,camera_capture)

# Remove camera object - ensures release of resources
del(camera)

print("Done")