0

I want to run an opencv-python program at Raspbian startup. The program is to capture images continuously from picamera and do some image processing using opencv and display it in full-screen mode. But it failed to start the program and show some errors in the terminal.

For that I did the following things.

Create a folder named autostart in .config folder, then copy-paste terminal shortcut to this folder. this is done to start the terminal at startup. Then added the command

python filename.py at the end of .bashrc file

Error image

Here is my code:

import cv2
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np

camera = PiCamera()
camera.rotation=90
camera.resolution = (320,240)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size = (320,240))
time.sleep(0.1)

def multi_clahe(img, num):
    for i in xrange(num):
        img = cv2.createCLAHE(clipLimit=2.0, tileGridSize=
      (4+i*2,4+i*2)).apply(img)
     return img


sharp_kern = np.array((
[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]), dtype="int")



while True:   
    for frame in camera.capture_continuous(rawCapture, format="bgr", 
use_video_port=True):
    image = frame.array 
small = cv2.resize(image, (0,0), fx=1, fy=1) 


gray = cv2.cvtColor(small, cv2.COLOR_BGR2GRAY)

his_img = multi_clahe(gray,4)

#segments = slic(small, n_segments = 100, sigma = 5)    
#mark_boundaries(img_as_float(his_img), segments)

sharp_img = cv2.filter2D(his_img, -1, sharp_kern)

final = cv2.GaussianBlur(his_img, (5,5),0)
    bod=cv2.copyMakeBorder(final,20,20,20,20,cv2.BORDER_CONSTANT)
    vis = np.concatenate((bod, bod), axis=1)
    cv2.namedWindow("final", cv2.WND_PROP_FULLSCREEN)          
    cv2.setWindowProperty("final", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)

#cv2.imshow('multi_clahe',his_img)
cv2.imshow('final',vis)
   # cv2.imshow('original',small)
    if cv2.waitKey(50) & 0xFF == ord('q'):
        break
rawCapture.truncate(0)  


 cap.release()
 cv2.destroyAllWindows()

How can I fix this?

  • You really don't want to start anything that uses picamera from .bashrc; that file will be sourced in several places due to auto-login and no more than one process can use the camera at once. I'd follow MatsK's suggestion of using /etc/rc.local (but be careful as your script will be running as root) – Dave Jones Sep 14 '17 at 22:27
0

A program that should run at:

  • OS is booting /etc/rc.local
  • User login /home/USER/.bashrc

And to troubleshoot, pipe the output from your python program to a file and add that to your question.

Example:

python filename.py > logfile.log
0

I got the answer,

When I give a delay of 2 seconds, it worked fine.

And I found another solution to start the program at startup. created a script file which contain the program to run at startup.

Add this file in home/pi/.config/lxsession/LXDE-pi/autostart by typing @sh /home/pi/scriptfilename.sh

Actually my script file is placed in /home/pi so that i give the path like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.