Slow VideoCapture with rpi camera in raspbian with opencv - python

i'm making a face/eyes recognition software with python, using the opecv library, in a Raspberry and capturing the video with a rpi camera.
I've already written the code, and everything works fine on my PC. Actually it works fine on my Raspberry as well, but the problem is about the speed of it. On my Raspberry it goes really slow.
Here's the code anyway:
#import numpy as np
import cv2
dims = (240, 120) # webcam dimensions
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 360);
face_cascade = cv2.CascadeClassifier('/home/pi/Desktop/Raspberry/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/pi/Desktop/Raspberry/haarcascade_eye.xml')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
#frame = cv2.imread('./Data/viola.jpg')
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect the face
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(len(faces))
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
As you can see i've set the resolution to 640X480, and with this the video shows about one frame every 1-2 seconds. If i don't specify the resolution it gets me a really small window in where there's my output. I have to say that i connect to my Raspberry throw SSH, but even if i record the video, and then i watch it on my PC is very slow. What i want to say is that it doesn't seem a problem given by the SSH tunnel.
To use the opencv's VideoCapture feature i had to inslall the UV4L driver, following this guide: http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=14 .
I saw many people using this method of capturing a video with opencv in raspberry, and nobody say anything about the framerate. Anyone know what should i do to have it faster?
PS i know that may be this is not the place in where we can make questions like this. But i don't really know where to ask it.

You are capturing the uncompressed frames, set this property to capture compressed frames, cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
ex:
#import numpy as np
import cv2
dims = (240, 120) # webcam dimensions
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 360)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
.......

Related

VideoCapture with usb camera

I´m working with C12741-03 Hamamatsu camera conected by USB, i want to control it with OpenCV, with this code i can control the webcam of my computer and a normal USB camera, but hamamatsu camera, what need a API to work and what i have instaled, doesn´t work. I like to know if there are any way to indicate VideoCapture() function the path of the camera or something like that for start the comunication with it. Im working in windows and if you know another library to work with the camera tell me too. Thanks.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
if(cap.isOpened()==False):
print("ERROR AL ABRIR LA CAMARA")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow("Frame", frame)
if cv2.waitKey(25) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Try changing cap = cv2.VideoCapture(0) to cap = cv2.VideoCapture(1).

OpenCV taking single camera frame while camera still running

I wrote a script for an image processing. I need to take a frame from camera and then make some operations. I can do this but the time when script is initializing the camera is very long. Is there any solution that I will run my script and camera will be working all the time and for example when I will press a button it will save a frame?
This is my code for now:
import cv2
cap = cv2.VideoCapture(1)
cap.set(3, 640)
cap.set(4, 480)
while True:
_, img = cap.read()
cv2.imshow('Output', img)
if cv2.waitKey(1) & 0xFF==ord('s'):
print('DO IMAGE PROCESSING...')
elif cv2.waitKey(1) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The problem is that when I am pressing "q" sometimes it doesn't stop. Can you give me an advice which loop or maybe which lib should I use for that?
Thanks!

Python OpenCV images get blurry after successive shots

I'm new to Open CV and python, and I've been facing a problem:
I've been doing a project on my Raspberry Pi where the webcam takes a grey scale image, removes the background, and saves it in a folder.
This is used by a machine learning algorithm to detect the object in the image.
The webcam is fixed at a particular point so I first take an image of the background and then take a picture of the object. The background is then removed from the object and it looks fine.
But if I repeat the process and overwrite the image, it becomes blurred.
This effect keeps happening until after about three or four shots the image becomes blurry and my program cant identify the object in it.
My code is:
#get Background
import cv2
cam = cv2.videoCapture(0)
ret, frame = cam.read()
if ret:
img_name = '/home/pi/Desktop/background.png'
grey_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imwrite(img_name, grey_img)
print('{} written'.format(img_name))
cam.release()
#takeImage
import cv2
import numpy as np
ret, frame = cam.read()
back = cv2.imread('/home/pi/Desktop/background.png')
if ret:
img_name = '/home/pi/Desktop/img_capture.png'
grey_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imwrite(img_name, grey_img)
grey_obj = cv2.subtract(cv2.imread(img_name), back)
cv2.imwrite(img_name, grey_obj)
print('{} written'.format(img_name))
cam.release()
I'm using a Logitech webcam but I'm not sure of the exact model
Please help me out, and thanks in advance.
Clear at the start:
Not so much:
Not at all:

Corrupt JPEG Data: 1273 extraneous bytes before marker. Opencv 3/python2.7

This is the code when i execute it:
You can see the frame opens but doesnt show anything
I want to use a usb camera with a raspberry pi 3 model b v1.2 using opencv 3.3 and python 2.7.
I work with opencv in an virtual enviroment.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read() #Capture frame-by-frame
#Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Display resulting frame
cv2.imshow('frame',frame)
cv2.waitKey(10)
#if cv2.waitKey(1) & 0xFF == ord('q'):
# break
#when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I just have no idea how to get around this error. I already searched the error and i am getting helpless, anyone having an idea?
EDIT: i am currently playing around with the code and i can get frames but most of the time the screen stays grey. I use # to show how the code looks now
Ok, it now opens a window and shows the output of the camera
Because of this code:
import sys
sys.path.append('/home/pi/.virtualenvs/cv/lib/python2.7/site-
packages/usr/local/lib/python2.7/site-packages')
and i also use sudo python program.py in the terminal
But this Error :"NameError: name 'CV_CAP_PROP_FRAME_HEIGHT' is not defined" still persists...

opencv, python: How to track live trackers from environment and find changes in environment

I'm trying my hands on OpenCV with Python and 'am kind of stuck.
I want to find specific trackers from every frame of a live camera and detect changes in the environment with a tracker of different color (say red).
Right now, my code takes a frame of my video which I select, and shows trackers which are too much to understand.
Can you help me in fixing this code?
import numpy as np
import cv2
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
# Initiate feature detector
orb = cv2.FastFeatureDetector_create()
# find the keypoints with ORB
kp = orb.detect(gray)
img2 = cv2.drawKeypoints(frame, kp, outImage=None, color=(0, 255, 0), flags=0)
plt.imshow(img2), plt.show()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
This is my image without an object (bottle)
This is me with an object
Now what I want to track changes in between the images (with bottle and without bottle), i.e changes in environment of an image should be tracked live on videocam!

Categories

Resources