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()
Related
I'm trying to use an example from
https://github.com/ageitgey/face_recognition
for face detection on Raspberry Pi.
This is the 'facerec_on_raspberry_pi.py' code:
# This is a demo of running face recognition on a Raspberry Pi.
# This program will print out the names of anyone it recognizes to the console.
# To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
# the picamera[array] module installed.
# You can follow this installation instructions to get your RPi set up:
# https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
import face_recognition
import picamera
import numpy as np
# Get a reference to the Raspberry Pi camera.
# If this fails, make sure you have a camera connected to the RPi and that you
# enabled your camera in raspi-config and rebooted first.
camera = picamera.PiCamera()
camera.resolution = (320, 240)
output = np.empty((240, 320, 3), dtype=np.uint8)
# Load a sample picture and learn how to recognize it.
print("Loading known face image(s)")
obama_image = face_recognition.load_image_file("obama_small.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Initialize some variables
face_locations = []
face_encodings = []
while True:
print("Capturing image.")
# Grab a single frame of video from the RPi camera as a numpy array
camera.capture(output, format="rgb")
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(output)
print("Found {} faces in image.".format(len(face_locations)))
face_encodings = face_recognition.face_encodings(output, face_locations)
# Loop over each face found in the frame to see if it's someone we know.
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
name = "<Unknown Person>"
if match[0]:
name = "Barack Obama"
print("I see someone named {}!".format(name))
Code is working fine with CSI camera, but how can I change to work with USB webcam?
Thanks
You can use opencv API directly.
Instead of creating a PiCamera object like:
camera = picamera.PiCamera()
camera.resolution = (320, 240)
output = np.empty((240, 320, 3), dtype=np.uint8)
Try:
import cv2
camera = cv2.VideoCapture(0) # 0 is the first camera
Then, when reading an image, replace:
camera.capture(output, format="rgb")
With:
_, output = camera.read() # To read the image in the camera native resolution
output = cv2.resize(output, (320, 240)) #To get the image in the same size as expected
I am running my Python FaceRecognition code on RaspberryPi but I am getting No module named 'picamera' error. I have tried few solutions but none worked for me. Can someone please figure out what is the problem?
Below is my Python Code:
import cv2
import numpy as np
from imutils.video.pivideostream import PiVideoStream
from imutils.video import FPS
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import argparse
import imutils
#Load pretrained cascade face detection
face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
#Load improved FPS video instead of the default Picamera
reader = PiVideoStream().start()
time.sleep(0.2)
#Load the recognizer
rec = cv2.face.createLBPHFaceRecognizer()
#Load trained local binary pattern face data
rec.load("recognizer/trainingData.yml")
id=0
font = cv2.FONT_HERSHEY_SIMPLEX
#Face recognition function
def detectFace(faces,hog,img):
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
result = cv2.face.MinDistancePredictCollector()
rec.predict(hog[y:y+h,x:x+w],result, 0)
id = result.getLabel()
conf = result.getDist()
if(conf<150):
if(id==1):
id="Ibrahim_"+str(conf)
elif(id==2):
id="Minh_"+str(conf)
else:
id="Hyeon_"+str(conf)
else:
id="Unknow"
cv2.putText(img,str(id),(x,y+h),font,1,(255,255,255),2,cv2.LINE_AA)
while(True):
#read each frame in the real-time video
frame = reader.read()
img=frame
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Use histogram equalizer for adjusting face in different light condition
equ = cv2.equalizeHist(gray)
faces = face_cascade.detectMultiScale(equ, 1.05, 5,minSize=(10,10))
#If the face is not frontal face then rotate the face by +30/-30 degree
if len(faces)==0:
rows,cols = equ.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),30,1)
dst = cv2.warpAffine(equ,M,(cols,rows))
faces = face_cascade.detectMultiScale(dst, 1.05, 5,minSize=(10,10))
if len(faces)==0:
rows,cols = equ.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),-30,1)
dst = cv2.warpAffine(equ,M,(cols,rows))
faces = face_cascade.detectMultiScale(dst, 1.05, 5,minSize=(10,10))
detectFace(faces,dst,img)
else:
detectFace(faces,dst,img)
else:
detectFace(faces,equ,img)
cv2.imshow('Face', img)
if(cv2.waitKey(1)==ord('q')):
break;
cap.release()
cv2.destroyAllWindows()
Below is what I run on RaspberryPi terminal and what I get:
(cv) pi#raspberrypi:~/Downloads/Raspberry_pi_face_recognition-master $ python FaceRecognizer.py
Traceback (most recent call last):
File "FaceRecognizer.py", line 3, in <module>
from imutils.video.pivideostream import PiVideoStream
File "/home/pi/.virtualenvs/cv/lib/python3.5/site-packages/imutils/video/pivideostream.py", line 2, in <module>
from picamera.array import PiRGBArray
ImportError: No module named 'picamera'
Just Try it for python3
sudo apt install python3-picamera
sudo -H pip3 install --upgrade picamera[array]
In code file write this steps:
from picamera import PiCamera
camera = PiCamera()
and must be ensure that camera = PiCamera() line is not be in loop.
I try using face recognition from this link: face recognition
then modif the code like this, main.py:
#!/usr/bin/env python
import cgitb, cgi
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print "Content-type:text/html\r\n\r\n"
import base64
import simplejson as json
import re
import face_recognition
import numpy as np
import io
from imageio import imread
from PIL import Image
import datetime
import os, errno
import shutil
params = cgi.FieldStorage()
now = datetime.datetime.now()
date = str(now)
date2 = date.replace(" ","")
img = params.getvalue('img')
data1 = json.loads(img)
data2 = data1['img2']['data']
numparray = data1['img1']
numparray2 = numparray.replace(" ", "+")
b=bytes(numparray2)
imgdata = base64.b64decode(b)
os.makedirs(date2)
with open(date2+"/img1.png", "wb") as f:
f.write(imgdata)
image = face_recognition.load_image_file(date2+'/img1.png')
try:
face_encode = face_recognition.face_encodings(image)[0]
#print("face_encode = ".format(face_encode))
except IndexError:
print("encode image failed")
quit()
known_faces = []
y = 1
for images in data2:
ir = images.replace(" ", "+")
ib = bytes(ir)
imagedata = base64.b64decode(ib)
x = str(y)
with open(date2+"/compare"+x+".png", "wb") as g:
g.write(imagedata)
compare = face_recognition.load_image_file(date2+"/compare"+x+".png")
try:
compare_encode = face_recognition.face_encodings(compare)[0]
#print("face_encode = ".format(face_encode))
except IndexError:
print("encode image compare failed")
quit()
known_faces.append(compare_encode)
y = y+1
results = face_recognition.face_distance(known_faces, face_encode)
datahasil = []
#hasilakhir = "{"
for i, face_distance in enumerate(results):
h = "{:.2}".format(face_distance, i)
#hasilakhir = hasilakhir+"compare{}"
datahasil.append(h)
hasilakhir = ','.join(datahasil)
shutil.rmtree(date2, ignore_errors=True)
print("{\"hasilcompare\" : \"" +hasilakhir+ "\"}")
the final result is compare between 2 image and give the score, in case photo of image is potrait compare is successfull, but when one of image is not on potrait (face not on potrait potision) or like face angle more than 90 degree, that give error message in catch encode failed..
i have try another way with face detection before sending 2 image to main.py to detect the image in face but when it can't detect faces i try to rotate the image untill the code detect face, but sometimes face detection can detect face with angle 90degree but in face recognition(main.py) still can't read the face.
code of rotate is here, rotate.py:import numpy as np
import cv2
from scipy import ndimage, misc
import os
from PIL import Image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
for counter in range (0, 4):
img = cv2.imread('img/1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if(len(faces) == 1):
i = False
print ("face found")
break
else:
print("no face found")
i = False
img = Image.open("img/1.jpg")
img.rotate(90).save("img/1.jpg")
Yes, I think this is the problem with the library. I tested it on the same image. Once after rotation and once on the original image. It does not detect in the rotated image, both the face_locations and face_encodings are empty lists.
I have created a face recognition code using OpenCV. Now the problems occuring are :
-> Sometimes it detects small objects from the wall and show them as me. How can i stop it from doing so ? so that it only detect faces not objets.
-> Whenever a unknown person shows up on scree, it randomly shows any name to it from the database. I waant it to show as a Unknown Face whenever someone unknown hits the cam.
Note : i did not added it to sql database yet but will do it after i get through this.THanks for the help in advance
Data Set creator code:
import cv2
import sqlite3
import numpy as np
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0);
id = raw_input('Enter Your ID : ')
Name = raw_input('Enter Your Name : ')
sampleNum=0;
while(True):
ret,img=cam.read();
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
sampleNum=sampleNum+1;
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.waitKey(500);
cv2.imshow("Face",img);
cv2.waitKey(1);
if(sampleNum>20):
break;
cam.release()
cv2.destroyAllWindows()
Trainer Code :
import os
import cv2
import numpy as np
from PIL import Image
recognizer=cv2.createLBPHFaceRecognizer();
path='dataSet'
def getImagesWithID(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
print ID
IDs.append(ID)
cv2.imshow("training",faceNp)
cv2.waitKey(10)
return IDs, faces
Ids,faces=getImagesWithID(path)
recognizer.train(faces,np.array(Ids))
recognizer.save('recognizer/trainningData.yml')
cv2.destroyAllWindows()
Detector Code :
import cv2
import numpy as np
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0);
rec=cv2.createLBPHFaceRecognizer();
rec.load("recognizer\\trainningData.yml")
id=0
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,2,2,0,2)
while(True):
ret,img=cam.read()
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
id,conf=rec.predict(gray[y:y+h,x:x+w])
if(id==1):
id="Vivek"
cv2.cv.PutText(cv2.cv.fromarray(img),str(id),(x,y+h),font,255);
cv2.imshow("Face",img);
if(cv2.waitKey(1)==ord('q')):
break;
cam.release()
cv2.destroyAllWindows()
I try to load some images into an RDD and use the face recognition library(https://github.com/ageitgey/face_recognition) to compare different images. Following code work
import face_recognition
import numpy as np
from io import BytesIO
from PIL import Image
from pyspark import SparkContext
sc = SparkContext(appName="LoadingImage")
images = sc.binaryFiles("./images/")
image_to_array = lambda rawdata: np.asarray(Image.open(BytesIO(rawdata)))
i_arr = images.values().map(image_to_array)
new_encoding = face_recognition.face_encodings(i_arr.first())
next_encoding = face_recognition.face_encodings(i_arr.first())
result = face_recognition.compare_faces([new_encoding[0]], next_encoding[0])
print(result)
However, when I try to map face_encodings function to all the elements inside the RDD, it always gives me an error:
RuntimeError: Expected writable numpy.ndarray with shape set.
img_to_encodings = lambda img: face_recognition.face_encodings(img)[0]
i_arrm = i_arr.map(img_to_encodings)
result = face_recognition.compare_faces([i_arrm.first()], i_arrm.first())
print(result)
The error is from dlib library, but I reckon I did something wrong with spark. Any idea how to solve this?
The frame returned by picamera has flag set to false i.e Writable : False.
Set frame flag to true so that face_recognition package can use it. Code Snippet:
image.setflags(write=True)
For Demo Code Have A Look:
#import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
#initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
#allow the camera to warmup
time.sleep(0.1)
#capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
image.setflags(write=True)
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the q key was pressed, break from the loop
if key == ord("q"):
break
cv2.destroyAllWindows()