说安装了imutils时,“没有名为imutils的模块”?

时间:2019-04-08 15:53:22

标签: python raspberry-pi cv2

我想在我的Raspberry Pi 3上运行此代码。我在Pi上使用过pip install imutils,但是,当我通过CLI运行代码时,它返回“ 没有名为imutils的模块”。我不希望使用虚拟环境。我的cv2在Pi上正常运行,并且没有问题,此imutils问题是否已解决?

更新,升级,删除imutils,但这是必需的。

import numpy as np
import cv2
import Person
import time
import imutils
import datetime

cap = cv2.VideoCapture('testVideo.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows=True)  # Create the background substractor

kernelOp = np.ones((3, 3), np.uint8)
kernelOp1 = np.ones((7, 7), np.uint8)
kernelOp2 = np.ones((5, 5), np.uint8)

kernelCl = np.ones((11, 11), np.uint8)
kernelCl1 = np.ones((20, 20), np.uint8)
kernelCl2 = np.ones((25, 25), np.uint8)

# Variables
font = cv2.FONT_HERSHEY_SIMPLEX
persons = []
max_p_age = 5
pid = 1
areaTH = 5000
w_margin = 50
h_margin = 50
wmax = 500

import pdb;

pdb.set_trace()  # debuginimo pradzia

# Atvaizdavimo kintamieji
cnt_up = 0
cnt_down = 0
line_down_color = (255, 0, 0)
line_up_color = (0, 0, 255)
pts_L1 = np.array([[0, 320], [480, 320]])
pts_L2 = np.array([[0, 400], [480, 400]])

counter = 0

while (cap.isOpened()):
    ret, frame = cap.read()  # read a frame

    frame = imutils.resize(frame, width=min(640, frame.shape[1]))

    fgmask = fgbg.apply(frame)  # Use the substractor
    try:
        ret, imBin = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY)

        mask0 = cv2.morphologyEx(imBin, cv2.MORPH_OPEN, kernelOp2)

        mask = cv2.morphologyEx(mask0, cv2.MORPH_CLOSE, kernelCl2)
    except:
        # if there are no more frames to show...
        print('EOF')
        break

    maskOriginal = mask

    _, contours0, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    ########if contour is too big cut in half
    mask2_flag = 0
    for cnt in contours0:
        area = cv2.contourArea(cnt)
        if area > areaTH:
            M = cv2.moments(cnt)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            x, y, w, h = cv2.boundingRect(cnt)
            if w > wmax:
                mask2 = cv2.line(mask, ((x + w / 2), 0), ((x + w / 2), 640), (0, 0, 0), 10)
                mask2_flag = 1

    if mask2_flag == 0:
        mask2 = mask

    cv2.imshow('Mask line', mask2)
    cv2.imshow('mask to open', mask0)
    cv2.imshow('Mask initialize', maskOriginal)
    cv2.imshow('initial subtraction', imBin)

    _, contours0, hierarchy = cv2.findContours(mask2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for cnt in contours0:
        cv2.drawContours(frame, cnt, -1, (0, 255, 0), 3, 8)
        area = cv2.contourArea(cnt)

        for i in persons:
            i.updateDingimas(i.getDingimas() + 1)
            if i.getDingimas() > 25:
                persons.remove(i)

        if area > areaTH:

            M = cv2.moments(cnt)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            x, y, w, h = cv2.boundingRect(cnt)

            print('x{} y{} w{} h{}'.format(x, y, w, h))

            new = True
            for i in persons:
                if abs(x - i.getX()) <= w_margin and abs(y - i.getY()) <= h_margin:
                    new = False
                    i.updateCoords(cx, cy)
                    i.updateDingimas(0)
                    break

            if new == True:
                p = Person.MyPerson(pid, cx, cy, max_p_age)
                persons.append(p)
                pid += 1

            cv2.circle(frame, (cx, cy), 5, (0, 0, 255), -1)
            img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.drawContours(frame, cnt, -1, (0, 255, 0), 3)
            cv2.imshow('img', img)

    #########################
    # Trajectory rendering
    #########################
    for i in persons:
        if len(i.getTracks()) >= 2:
            pts = np.array(i.getTracks(), np.int32)
            pts = pts.reshape((-1, 1, 2))
            frame = cv2.polylines(frame, [pts], False, i.getRGB())

        if i.getDir() == None:
            i.kurEina(pts_L2[0, 1], pts_L1[0, 1])
            if i.getDir() == 'up':
                cnt_up += 1
                print('Timestamp: {:%H:%M:%S} UP {}'.format(datetime.datetime.now(), cnt_up))
            elif i.getDir() == 'down':
                cnt_down += 1
                print('Timestamp: {:%H:%M:%S} DOWN {}'.format(datetime.datetime.now(), cnt_down))

        cv2.putText(frame, str(i.getId()), (i.getX(), i.getY()), font, 0.7, i.getRGB(), 1, cv2.LINE_AA)

    #########################
    # Rendering
    #########################
    str_in = 'In: ' + str(cnt_up)
    str_out = 'Out: ' + str(cnt_down)
    frame = cv2.polylines(frame, [pts_L1], False, line_down_color, thickness=4)
    frame = cv2.polylines(frame, [pts_L2], False, line_up_color, thickness=4)
    cv2.putText(frame, str_in, (10, 50), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
    cv2.putText(frame, str_out, (10, 100), font, 1, (255, 0, 0), 2, cv2.LINE_AA)

    cv2.imshow('Frame', frame)

    # Abort and exit with 'Q' or ESC
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()  # release video file
cv2.destroyAllWindows()  # close all openCV windows

我想运行此代码而不会出现“ 没有名为imutils的模块”错误。

1 个答案:

答案 0 :(得分:0)

如果要在Python 3中使用该模块,则需要使用pip3安装该模块,以便将其安装在正确的位置。