【发布时间】:2021-10-22 01:30:23
【问题描述】:
由于某种原因,当我运行 OpenCV 时,当它立即检测到正图像时,检测器处理时间会大大增加。在下面的代码中,我标记了打印时间的位置。通常该输出约为十分之一秒,但它检测到正图像时会立即达到 50 秒甚至更多!这是什么原因造成的?
注意:这个项目是在树莓派零上完成的。
import picamera
from picamera.array import PiRGBArray
import cv2
import time
import numpy as np
import logging
image_path = '/home/pi/photo.bgr'
def DetectWeeds(img):
time1 = time.time()
weedDetector = cv2.CascadeClassifier('/home/pi/WeedClassifier/data/cascade.xml')
found = weedDetector.detectMultiScale(img, minSize=(20, 20))
time2 = time.time()
print(time2 - time1) #PROCESSING TIMER HERE
amount_found = len(found)
i = 0
time.sleep(.1)
#Taking a lot of time when a weed is detected
if amount_found != 0:
# There may be more than one
# sign in the image
firstset = found[0]
x, y, width, height = firstset
imagewidth = 640
currentx = (x + width) / 2 + x
currenty = (y + height) / 2 + y
targetx = imagewidth / 2
return currentx, targetx, currenty
if amount_found == 0:
return 0, -1, 0
def main():
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.framerate = 16
rawCapture = PiRGBArray(camera, size=(640, 480))
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
currentx, targetx, currenty = DetectWeeds(image)
if currentx != 0:
if currentx > targetx:
print("Move Left!")
if currentx < targetx:
print("Move Right!")
if currentx == targetx:
print("Centered!")
print("Move Forward Now!")
if currentx == 0 and targetx == -1:
print("No Weeds Here")
rawCapture.truncate(0)
time.sleep(.08)
main()
【问题讨论】:
-
能否分享一下你正在使用的图片。
-
你能对计算 currentx 和 currenty 的行计时吗?
-
没有数据就无法复制。请尝试提供一个最小的可重现示例。
标签: python opencv raspberry-pi