我在Raspberry Pi 3上有一个Python 3脚本,它使用opencv 3.4从Logitech网络摄像头拍摄照片,然后将其上传到Dropbox。代码改编自pyimageserach。
到目前为止,当我从终端或IDE手动执行脚本时,脚本正常工作。 网络摄像头有时候不会抓住图片,所以它进入循环但最终会起作用。 但是,如果我使用其中一种方法在启动时自动加载它,则代码不会执行任何操作。
#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
除了告诉我有一个env变量问题,我没有做任何事情,但是我没有做任何事情。
init.d没有工作。
rc.local没有用。
我试图使用自动启动lxde,但这不起作用
简单地工作了Bashrc我希望它可以在不必登录的情况下工作。
我尝试过systemd并遇到以下错误
Apr 12 01:24:50 raspberrypi python3.5 [743]:无法初始化服务器: 无法连接:连接拒绝4月12日01:24:50 raspberrypi python3.5 [743]:无法打开显示:4月12日01:24:50 raspberrypi systemd [1]:test.service:退出主进程,代码=退出, status = 1 / FAILURE Apr 12 01:24:50 raspberrypi systemd [1]:test.service: 单位进入失败状态。 4月12日01:24:50 raspberrypi systemd [1]: test.service:结果&#39;退出代码&#39;
失败
似乎网络摄像头会短暂亮起然后消失
在使用systemd的其他时间我遇到视频错误V4L2的错误:opencv不支持传入图像的像素格式