最近,我编写了一个python脚本,该脚本使用COCO数据集和经过预先训练的模型来检测对象。我已经使用"Mu Editor“在Raspberry Pi的桌面界面上成功地运行了这段代码。我的目标是能够在Raspberry Pi启动时自动运行这段代码,而不需要监视器或SSH连接。我意识到我的OpenCV程序使用了GUI元素,因此很难简单地打开Raspberry并自动运行我的代码。但是,我很好奇是否有任何方法可以提供这种功能。
方法我尝试过:
我已附上以下守则:
import cv2
import pyttsx3
import time
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
time.sleep(10)
classNames = []
classFile = '/home/pi/Desktop/Object Detection/coco.names'
with open(classFile,'rt') as f:
classNames = [line.rstrip() for line in f]
configurationPath = '/home/pi/Desktop/Object Detection/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = '/home/pi/Desktop/Object Detection/frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath, configurationPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
engine = pyttsx3.init()
while True:
success, img = cap.read()
img = cv2.rotate(img, cv2.ROTATE_180)
classIds, confs, bbox = net.detect(img, confThreshold=0.55, nmsThreshold=0.2)
if len(classIds) == 0:
engine.say("no objects detected")
engine.runAndWait()
continue
if len(classIds) != 0:
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
className = classNames[classId - 1]
str1 = str(className)
print(str1)
engine.say(str1 + "detected")
engine.runAndWait()
continue
cv2.imshow('Output', img)
cv2.waitKey(1)我忘记提到的一点是:我尝试在终端中运行代码,而不是在Mu Editor中运行,并取得了成功。
我在大约一个月前写了一个问题,得到了这个社区的很好的建议!请让我知道是否有任何方式(软件或硬件增加)自动启动此脚本在引导无需使用监视器或SSH连接。如有任何建议,我将不胜感激!
发布于 2021-12-28 18:11:47
您可以使用systemd在您的覆盆子上创建服务。这将在系统启动时在后台运行您的脚本。
你会在互联网上找到很多关于如何配置它的教程(比如这一个)。
https://stackoverflow.com/questions/70510652
复制相似问题