【问题标题】:Processing Takes Ridiculous time when OpenCV detects a positive image当 OpenCV 检测到正图像时,处理花费了可笑的时间
【发布时间】: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


【解决方案1】:

我建议如下改进代码,看看你是否及时得到任何改进。也可能是因为当没有检测到杂草时,代码什么也不做,而当检测到杂草时,代码必须再运行几行代码。

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

        x, y, width, height = found[0]

        currentx = (x + width) / 2 + x
        currenty = (y + height) / 2 + y

        targetx = 640 / 2

        return currentx, targetx, currenty

    if amount_found == 0:
        return 0, -1, 0

【讨论】:

    【解决方案2】:

    found = weedDetector.detectMultiScale(img, minSize=(20, 20))

    问题很可能出在detectMultiscale()函数中传递的scaleFactor参数上。

    scaleFactor: Parameter specifying how much the image size is reduced at each image scale.
    

    使用多个设置调整 detectMultiscale() 函数中的 scaleFactor。 增加比例因子会提高准确性,并且计算成本也很高。良好的经验法则是在合理的时间内以足够的准确度找到正确的最佳位置。

    如果还是不行就尝试调整其他参数,比如maxSize,minNeighbors。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多