I have a Python 3 script on the Raspberry Pi 3 that uses opencv 3.4 to take pictures of from a Logitech webcamera and then upload it to dropbox. Code is adapted from pyimageserach.
So far the script works when I manually execute it from terminal or IDE. The webcamera sometimes doesn't grab the picture, so it goes into a loop but eventually works. However, the code does nothing if I use one of the methods to automatically load it at startup.
#system append
import sys
sys.path.append('/home/pi/.local/lib/python3.5/site-packages/')
sys.path.append('/usr/local/lib/python3.5/dist-packages/')
import datetime
import time
import cv2
import imutils
import dropbox
#access dropbox
client=dropbox.Dropbox("dropbox key not shown here")
time.sleep(10)
camera = cv2.VideoCapture(0)
# Initialize the first frame
firstFrame = None #keep the first fram clean to compare to all others
#loop over the frames of the video
while True:
(grabbed, frame) = camera.read()
i = 1
while grabbed != True:
i = i+1
print ("frame not grabbed")
print ("Try AGAIN "+str(i)+" times in 15 seconds")
camera = cv2.VideoCapture(0)
time.sleep(15)
(grabbed, frame) = camera.read()
text = "Unoccupied Room"
#resize the frame, convert it to grayscale, and blur it
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21,21), 0)
#if the first frame is None, initialize it
if firstFrame is None:
firstFrame = gray
continue
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 50, 255, cv2.THRESH_BINARY)[1]
# find contour of the image detected
thresh = cv2.dilate(thresh, None, iterations=2)
(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) < 500:
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Occupied Room"
#draw the text and time stamp on the frame
cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y
%I:%M:%S%p"), (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.35, (0, 0, 255), 1)
# show the frame and record if the user presses a key
cv2.imshow("Security Feed", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("Frame Delta", frameDelta)
key = cv2.waitKey(1) & 0xFF
# add my own stuff for occupied room
if text == "Occupied Room":
filename =
datetime.datetime.now().strftime('%Y%m%d_%Hh%Mm%Ss%f')+'.jpg'
cv2.imwrite('./pictures/'+filename,frame)
file_from = '/home/pi/Desktop/projects/pictures/'+filename
file_to = '/pictures/'+filename
try:
with open (file_from, "rb") as file:
client.files_upload(file.read(), file_to, mute=True)
except ValueError:
print ("Upload Failure")
if key == ord("q"):
# cleanup the camera and close any open window
camera.release()
del camera
del frame
del grabbed
cv2.destroyAllWindows()
break
Crons doesn't do anything, except telling me I had an env variable problem, which I appended.
init.d didn't work.
rc.local did not work.
I tried to use autostart lxde but that did not work
Bashrc briefly worked I wish it could work without having to log in.
I tried systemd and got the following errors
Apr 12 01:24:50 raspberrypi python3.5[743]: Unable to init server: Could not connect: Connection refused Apr 12 01:24:50 raspberrypi python3.5[743]: cannot open display: Apr 12 01:24:50 raspberrypi systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE Apr 12 01:24:50 raspberrypi systemd[1]: test.service: Unit entered failed state. Apr 12 01:24:50 raspberrypi systemd[1]: test.service: Failed with result 'exit-code'.
It seems like the webcamera will light up briefly and then disappear
At other times with systemd I get error with videoio error V4L2: Pixel format of incoming image is unsupported by opencv