python - 汽车跟踪级联::Typeerror 整数到 float

标签 python python-3.x numpy

我正在使用此代码,但一直遇到此错误,我不知道如何解决...:S 我真的要疯了...提前致谢。

文件“c:\xampp\htdocs\DeteccionVehiculos\detect.py”,第 54 行,位于 detectorRegionsOfInterest 中 框架 = cv2.resize(框架, (frameWidth/scaleDown,frameHeight/scaleDown)) 类型错误:需要整数参数,但得到了 float

import cv2
import numpy as np

def diffUpDown(img):
    # compare top and bottom size of the image
    # 1. cut image in two
    # 2. flip the top side
    # 3. resize to same size
    # 4. compare difference  
    height, width, depth = img.shape
    half = height/2
    top = img[0:half, 0:width]
    bottom = img[half:half+half, 0:width]
    top = cv2.flip(top,1)
    bottom = cv2.resize(bottom, (32, 64)) 
    top = cv2.resize(top, (32, 64))  
    return ( mse(top,bottom) )


def diffLeftRight(img):
    # compare left and right size of the image
    # 1. cut image in two
    # 2. flip the right side
    # 3. resize to same size
    # 4. compare difference  
    height, width, depth = img.shape
    half = width/2
    left = img[0:height, 0:half]
    right = img[0:height, half:half + half-1]
    right = cv2.flip(right,1)
    left = cv2.resize(left, (32, 64)) 
    right = cv2.resize(right, (32, 64))  
    return ( mse(left,right) )


def mse(imageA, imageB):
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])
    return err

def isNewRoi(rx,ry,rw,rh,rectangles):
    for r in rectangles:
        if abs(r[0] - rx) < 40 and abs(r[1] - ry) < 40:
           return False  
    return True

def detectRegionsOfInterest(frame, cascade):
    scaleDown = 2
    frameHeight, frameWidth, fdepth = frame.shape 

    # Resize
    frame = cv2.resize(frame, (frameWidth/scaleDown, frameHeight/scaleDown)) 
    frameHeight, frameWidth, fdepth = frame.shape 

    # haar detection.
    cars = cascade.detectMultiScale(frame, 1.2, 1)

    newRegions = []
    minY = int(frameHeight*0.3)

    # iterate regions of interest
    for (x,y,w,h) in cars:
            roi = [x,y,w,h]
            roiImage = frame[y:y+h, x:x+w]   

            carWidth = roiImage.shape[0]
            if y > minY:
                diffX = diffLeftRight(roiImage)
                diffY = round(diffUpDown(roiImage))

                if diffX > 1600 and diffX < 3000 and diffY > 12000:
                    rx,ry,rw,rh = roi
                    newRegions.append( 
[rx*scaleDown,ry*scaleDown,rw*scaleDown,rh*scaleDown] )

    return newRegions


def detectCars(filename):
    rectangles = []
    cascade = cv2.CascadeClassifier('cars.xml')
    vc = cv2.VideoCapture(filename)

    if vc.isOpened():
        rval , frame = vc.read()
    else:
        rval = False

    roi = [0,0,0,0]
    frameCount = 0

    while rval:
        rval, frame = vc.read()
        frameHeight, frameWidth, fdepth = frame.shape 

        newRegions = detectRegionsOfInterest(frame, cascade)
        for region in newRegions:
            if isNewRoi(region[0],region[1],region[2],region[3],rectangles):
                rectangles.append(region)

        for r in rectangles:
            cv2.rectangle(frame,(r[0],r[1]),(r[0]+r[2],r[1]+r[3]), 
(0,0,255),3) 

    frameCount = frameCount + 1
    if frameCount > 30: 
        frameCount = 0
        rectangles = []

    # show result
        cv2.imshow("Result",frame)
        cv2.waitKey(1);
    vc.release()

detectCars('../DeteccionVehiculos/road.mp4')

最佳答案

使用地板:

frame = cv2.resize(frame, (math.floor(frameWidth/scaleDown), math.floor(frameHeight/scaleDown))) 

...或ceil:

frame = cv2.resize(frame, (math.ceil(frameWidth/scaleDown), math.ceil(frameHeight/scaleDown))) 

并且一定要导入数学

如您所料,地板向下舍入,天花板向上舍入。这将返回一个 int

关于python - 汽车跟踪级联::Typeerror 整数到 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217439/

相关文章:

python - 重构:合并两个字典,但忽略 None 值

Python:在一个文件行中检索两个单独的列表值

python - 多处理:类型错误: 'int' 对象不可迭代

python - 绘制给定 bin 端点和值的直方图

python - 在 Matplotlib 散点图中突出显示数据间隙 (NaN)

python - 将范围 (n,n+2000) 分成大小为 j 的 block 。

python - 如何获取最大值不重复的行的索引?

python - numpy:屏蔽数组的 ndenumerate?

python - numpy python 3.4.1 安装 : Python 3. 4 在注册表中找不到

python - 当应用堆叠集成时,H2O 如何衡量基础学习器的权重?