Cron job Issue in raspberry-pi - python

from time import sleep
import datetime
import os
import shutil
import cv2
from filename import opvideo
## Adding New Directory
path= '/home/pi/Desktop/teasr/input-video'+opvideo
os.makedirs(path)
path1= '/home/pi/Desktop/teasr/input-image'+opvideo
os.makedirs(path1)
os.makedirs('/home/pi/Desktop/teasr/output-video'+opvideo)
#######capturing
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('/home/pi/Desktop/teasr/input-video'+opvideo+ '/video.h264',fourcc, 15.0, (640,480))
i=0
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
i=i+1
if cv2.waitKey(1) & i>160:
cap.release()
out.release()
cv2.destroyAllWindows()
break
else:
break
`
I am looking to run my code after every 10 minutes using cron job. But whenever i my code running it is not capturing video but it is making directory.I am using USB camera for making video. If i am running my code in terminal every thing looks fine and camera is also working. But at cron job it is not working properly.Please help me out to run my full code using cron job.My main purpose is to make video after every 10 mintues when raspberry pi gets switched on. Hoping that someone may help.

Related

Increase the capture and stream speed of a video using OpenCV and Python [duplicate]

This question already has answers here:
OpenCV real time streaming video capture is slow. How to drop frames or get synced with real time?
(3 answers)
Closed 10 months ago.
I need to take a video and analyze it frame-by-frame. This is what I have so far:
'''
cap = cv2.VideoCapture(CAM) # CAM = path to the video
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
while cap.isOpened():
ret, capture = cap.read()
cv2.cvtColor(capture, frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', capture)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
analyze_frame(frame)
cap.release()
'''
This works, but it's incredibly slow. Is there any way that I can get it closer to real-time?
The reason VideoCapture is so slow because the VideoCapture pipeline spends the most time on the reading and decoding the next frame. While the next frame is being read, decode, and returned the OpenCV application is completely blocked.
So you can use FileVideoStream which uses queue data structure to process the video concurrently.
The package you need to install:
For virtual environment: pip install imutils
For anaconda environment: conda install -c conda-forge imutils
Example code:
import cv2
import time
from imutils.video import FileVideoStream
fvs = FileVideoStream("test.mp4").start()
time.sleep(1.0)
while fvs.more():
frame = fvs.read()
cv2.imshow("Frame", frame)
Speed-Test
You can do speed-test using any example video using the below code. below code is designed for FileVideoStream test. Comment fvsvariable and uncomment cap variable to calculate VideoCapture speed. So far fvs more faster than cap variable.
from imutils.video import FileVideoStream
import time
import cv2
print("[INFO] starting video file thread...")
fvs = FileVideoStream("test.mp4").start()
cap = cv2.VideoCapture("test.mp4")
time.sleep(1.0)
start_time = time.time()
while fvs.more():
# _, frame = cap.read()
frame = fvs.read()
print("[INFO] elasped time: {:.2f}ms".format(time.time() - start_time))

OpenCV works with threads but not with processes

Hi everyone and thanks for the help.
I've got this function to save a video from frames taken by my webcam.
import cv2
import multiprocessing
import threading
def rec():
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
If i use it with threads, and so with this code, it works well:
s1 = threading.Thread(target=rec)
s1.start()
But if i want to start another process, using the following code, when i open the video it contains only black frames with some noise.
s1 = multiprocessing.Process(target=rec)
s1.start()
I searched all around but couldn't find any solution.
Also, i'm using Python 3.6
where is cap defined ? Try defining that in the function that you give to multiprocessing. If it is defined in the parent and is passed from the parent to the child, it is being pickled and that probably makes it unusable.
I solved the problem.
I was calling cap = cv2.VideoCapture(0) in my main and also in one of my imported modules, and that conflicted. I solved by calling it once.

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...

cv2.videocapture doesn't works on Raspberry-pi

How can i make the cv2.VideoCapture(0) recognize the USB camera of raspberry-pi.
def OnRecord(self, evt):
capture = cv2.VideoCapture(0)
if (not capture.isOpened()):
print "Error"
# video recorder
fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist
video_writer = cv2.VideoWriter.open("output.mp4", fourcc, 20, (640, 480), True)
# record video
while (capture.isOpened()):
ret, frame = capture.read()
if ret==True:
video_writer.write(frame)
cv2.imshow('Video', frame)
else:
break
def OnCancel(self, evt):
capture.release()
video_writer.release()
cv2.destroyAllWindows()
but it only prints Error.
So i guess capture is not opening. What might be the reason?
I tried this code from opencv documentation but doesn't worked out for me.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Any help would be greatly appreciated.
Load the correct video for linux drivers.
sudo modprobe bcm2835-v4l2
In my experience with CV2 replacing a webcam source on linux isn't always easy. How OpenCV works is it automatically draws from the systems default video source, which is known (usually) as video0. Unplug your usb webcam and go into a terminal and typing ls /dev/video*
Remember the number it says. Now plug in your USB webcam and type in ls /dev/video* again and look for any new /video, this is your USB webcam. Now type mv /dev/videoX videoY while X is the number of your USB webcam and Y the original number. This will replace your pi's default camera.
This isn't permanent as you will need to do this every time your pi starts up, an alternative to this is creating a bash file that runs on start up. Create a text file and copy the following into it.
#!/bin/bash
mv /dev/videoX videoY
(replace the X and Y of course)
and place that in /etc/init.d directory of your pi. Don't forget you may need to use
chmod 755 /etc/init.d/FILENAME.sh
to give it permission to run
Go to terminal and type lsusb and check whether the USB camera is recognized or not. If it is recognized then try to give different device ID such as 1 or 2 or 3 rather than 0.
looks Like you might have issue with codec, try using 'MJPG' codec instead of XVID.
For more details have a look here
Make sure that the camera that you are using is UVC compatible, as openCV running on linux based systems (like a raspi) starts to do some silly things when it is working with non UVC cameras.

Video Capture with Python

I'm trying to write some code for automatically capturing video from a webcam when activated through a batch script on Windows. I've managed to piece together a working script, but it does not appear to be saving the file. I know the code is (at least on a basic level) working because there are no errors and the activation light on the webcam lights up when the code is run. I've reproduced the code below if anyone has any suggestions on how to get it writing to a file, that would be great!
import numpy as np
import cv2
import msvcrt
cap = cv2.VideoCapture(0)
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
sub=raw_input("Subject#: ")
#Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
out = cv2.VideoWriter('C:\path\to\output_' + sub + '.mp4', fourcc, 30, (w,h))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
if msvcrt.kbhit():
if ord(msvcrt.getch()) != None:
break
else:
break
#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
The codec FMP4 is just not being supported by your camera I think. Neither in mine. If I change in your code the encoding to 'MJPG' and write to an '.avi' extension, it works very well.
P.S. Wanted to make a comment but I'm still underage (< 50).

Categories

Resources