Raspberry Pi CSI camera to USB webcam in Python - python

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

Related

Python Harvesters Image Acquisition GigeCam

I tried get image from my gige camera. In the camera's own software its working just fine, but when I do it with harvesters my image has a weird grid and I don't know why is it there and how to remove it. I need this for a stereovision project. Any idea?
Don't mind the brightness I tried it with higher expo as well, it did not changed a thing. :D
enter image description here
import genicam.genapi as ge
import cv2
from harvesters.core import Harvester
import matplotlib.pyplot as plt
import numpy as np
# Create a Harvester object:
h = Harvester()
# Load a GenTL Producer; you can load many more if you want to:
h.add_file("C:/Program Files\MATRIX VISION/mvIMPACT Acquire/bin/x64/mvGenTLProducer.cti")
# Enumerate the available devices that GenTL Producers can handle:
h.update()
# Select a target device and create an ImageAcquire object that
# controls the device:
ia = h.create(0)
ia2 = h.create(1)
# Configure the target device; it looks very small but this is just
# for demonstration:
ia.remote_device.node_map.Width.value = 1456
ia.remote_device.node_map.Height.value = 1088
# ia.remote_device.node_map.PixelFormat.symbolics
ia.remote_device.node_map.PixelFormat.value = 'BayerRG8'
ia2.remote_device.node_map.Width.value = 1456
ia2.remote_device.node_map.Height.value = 1088
# ia2.remote_device.node_map.PixelFormat.symbolics
ia2.remote_device.node_map.PixelFormat.value = 'BayerRG8'
ia.remote_device.node_map.ChunkSelector.value = 'ExposureTime'
ia.remote_device.node_map.ExposureTime.set_value(100000.0)
ia2.remote_device.node_map.ChunkSelector.value = 'ExposureTime'
ia2.remote_device.node_map.ExposureTime.set_value(100000.0)
# Allow the ImageAcquire object to start image acquisition:
ia.start()
ia2.start()
# We are going to fetch a buffer filled up with an image:
# Note that you'll have to queue the buffer back to the
# ImageAcquire object once you consumed the buffer; the
# with statement takes care of it on behalf of you:
while True:
with ia.fetch() as buffer:
component = buffer.payload.components[0]
_2d = component.data.reshape(1088, 1456)
img = _2d
img = cv2.resize(img,(640,480))
cv2.imshow('right',img)
cv2.imwrite('test_left.png',img)
cv2.waitKey(10)
with ia2.fetch() as buffer:
component = buffer.payload.components[0]
_2d = component.data.reshape(component.height, component.width)
img2 = _2d
img2 = cv2.resize(img2, (640, 480))
cv2.imshow('left', img2)
cv2.imwrite('test_right.png',img2)
cv2.waitKey(10)
ia.stop()
ia2.stop()
ia.destroy()
ia2.destroy()
h.reset()
I just had to convert it to Gray or RGB with cvtColor, and its working.
Thanks anyway.

File size limit in making timelapse video on the fly with OpenCV, Picamera and an IR camera

I am creating a raspberry pi timelapse camera encoding video with CV2 videowriter
Each image captured with picamera is added to the videowriter and once the intended number of images are taken the videowriter closes.
However - while this works for a few thousand images - it stops at some limit with a filesize of 366Mb which is now frustrating me and I ask you - the internet and hoard of coders to tell me why I am bad a coding and how to fix this - you must be tempted by this..
Here is my offering of garbage for you to laugh pitifully at
import os, cv2
from picamera import PiCamera
from picamera.array import PiRGBArray
from datetime import datetime
from time import sleep
now = datetime.now()
x = now.strftime("%Y")+"-"+now.strftime("%m")+"-"+now.strftime("%d")+"-"+now.strftime("%H")+"-"+now.strftime("%M") #string of dateandtimestart
print(x)
def main():
imagenum = 10000 #how many images
period = 1 #seconds between images
os.chdir ("/home/pi/t_lapse")
os.mkdir(x)
os.chdir(x)
filename = x + ".avi"
camera = PiCamera()
camera.resolution=(1920,1088)
camera.vflip = True
camera.hflip = True
camera.color_effects = (128,128) #makes a black and white image for IR camera
sleep(0.1)
out = cv2.VideoWriter(filename, cv2.cv.CV_FOURCC(*'XVID'), 30, (1920,1088))
for c in range(imagenum):
with PiRGBArray(camera, size=(1920,1088)) as output:
camera.capture(output, 'bgr')
imagec = output.array
out.write(imagec)
output.truncate(0) #trying to get more than 300mb files..
pass
sleep(period-0.5)
camera.close()
out.release()
if __name__ == '__main__':
main()
This example is a part of the whole code I've written (https://github.com/gchennell/RPi-PiLapse) which has an OLED display and buttons and selection of how many images as I have this all in an enclosure - the number of images seems to be limited to about 3000-4000 and then it just gives up and goes home... I tried adding the output.truncate(0)
I have also recreated this in python3 before you cry "BUT CV2.CV2.VIDEOWRITER!!!!" and that hasn't changed a thing - I'm missing something here...

Scan bar code using raspberry pi camera module v2

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()

RuntimeError: Expected writable numpy.ndarray with shape set

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()

object(car) detection in opencv python

I am working on one of my college project i.e object(car) detection in opencv python ,i am using opencv 3 and python 3.4. I have a code for it but when i run the code the output is not displayed. It shows that the code is error free but still unable to get the output. I am new to image processing ,so it will be a great help if someone tries to sort out my problem. The code is given below`
import cv2
import numpy as np
import argparse
ap = argparse.ArgumentParser()
ap.add_agrument("-v","--video",
help = "path to the (optional) video file")
args = vars(ap.parse_agrs())
camera = cv2.VideoCapture(agrs["video"])
car_cascade = cv2.CascadeClassifier("cars.xml")
while true:
ret,frames = camera.read(),cv2.rectangle()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectionMultiScale(gray, 1.1,1)
for (x,y,w,h) in cars:
cv2.rectangular()frames,(x,y),(x+w,y+h), (0,0,255),2)
cv2.imshow ('video',frames)
cv2.waitkey(0)
I just remove the argparse command and edited the code little bit and it is working quit well.To see the output click here : https://www.youtube.com/watch?v=phG9inHoAKg
And the code files are uploaded to my github account https://github.com/Juzer2012/Car-detection
You write: "It shows that the code is error free" ...
It isn't (and this multiple times) as for example here:
ap.add_agrument(...
where it should be
ap.add_argument(...
Just check again for more of such syntax errors. Happy coding :) .
Here the by you requested code example which uses argparse for image processing - it works both with python2.x and python3.x showing a video stream for processing in a for this purpose opened window. If you can see the video stream output, just mark this as a valid answer to your question. Thanks in advance (y). Happy coding :) .
import cv2
def showVideoStream_fromWebCam(argsVideo, webCamID=0, showVideoStream=True):
cv2_VideoCaptureObj_webCam = cv2.VideoCapture(webCamID)
while True:
retVal, imshowImgObj = cv2_VideoCaptureObj_webCam.read()
if showVideoStream:
imshowImgObj = cv2.flip(imshowImgObj, 1)
cv2.imshow('webCamVideoStream', imshowImgObj)
#:if
if cv2.waitKey(1) == 27:
break # [Esc] to quit
#:if
#:while
cv2.destroyAllWindows()
#:def
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-v","--video", help = "webCamID (= 0)")
args = vars(ap.parse_args())
showVideoStream_fromWebCam(args["video"])
Let's make the code even a bit more perfect by running the video at approximately it's original speed (25 frames/second), taking out what is not necessary and drawing all the rectangles first, then showing the frame:
import cv2
camera = cv2.VideoCapture("video.avi")
car_cascade = cv2.CascadeClassifier('cars.xml')
# Get frames per second from video file. Syntax depends on OpenCV version:
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if int(major_ver) < 3 :
fps = camera.get(cv2.cv.CV_CAP_PROP_FPS)
else :
fps = camera.get(cv2.CAP_PROP_FPS)
#:if
intTimeToNextFrame=int(1000.0/fps)-12 # '-12' estimation of time for processing
while True:
(grabbed,frame) = camera.read()
grayvideo = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cars = car_cascade.detectMultiScale(grayvideo, 1.1, 1)
for (x,y,w,h) in cars:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,255),1)
cv2.imshow("video",frame)
if cv2.waitKey(intTimeToNextFrame)== ord('q'):
break
camera.release()
cv2.destroyAllWindows()

Categories

Resources