Question

Here is my python program:

 #!/usr/bin/env/ python
  import cv
  capture1=cv.CaptureFromCAM(0)
  cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_WIDTH,320)  
  cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_HEIGHT,240)
  while 1:
       cam1=cv.QueryFrame(capture1);
       cv.SaveImage("camera.jpg",cam1);
       cv.WaitKey(11)
       print 'Done!'

On crontab:

@reboot sudo python /home/program.py >/home/result.txt

But its not saving the image.Definitely I have done something wrong! I got the similar problem when I was reading image cv2.imread("image.jpg") but it was returning None so I added full path to the image /home/image.jpg.That problem was solved!.Is cron not getting camera feed? Thanx for help!

Was it helpful?

Solution

First and foremost, make sure the application succeeds communicating with the camera:

import cv
capture1 = cv.CaptureFromCAM(0)
if not capture1 :
    print "!!! Failed to open a camera interface"
    # Ideally, exit the application.

cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_WIDTH,320)  
cv.SetCaptureProperty(capture1,cv.CV_CAP_PROPER_FRAME_HEIGHT,240)

Remember to test if the frame was successfully retrieved from the camera:

while 1:
     frame = cv.QueryFrame(capture1);
     if not frame:
         print "!!! Failed to retrieve frame"
         break

     # Right now, your code overwrites the same file at every iteration of the loop.
     # It might be better to add a BREAK at the end for testing purposes.
     cv.SaveImage("camera.jpg", frame);

     # There's no need to call WaitKey() if the image is not displayed on a window.
     #cv.WaitKey(11)    

     print 'Done!'

SaveImage() will fail when the application doesn't have permission to write files in the directory from where it was executed. Since crontab is responsible to call your application, I imagine that it does that from a directory where the user doesn't have the right permissions. If this is the case, I strongly suggest you to feed SaveImage() with the full path to the file.

OTHER TIPS

The problem was in cv.ShowImage or cv2.imshow.When I commented this line everything worked fine! Previously the program got stuck at this this line.(while execution through cron).[That I was writing in my original program]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top