首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“没有模块名为imutils”时,它说安装imutils?

“没有模块名为imutils”时,它说安装imutils?
EN

Stack Overflow用户
提问于 2019-04-08 15:53:21
回答 1查看 5K关注 0票数 1

我想在我的Raspberry 3上运行这段代码,我已经在Pi上使用了pip install imutils,但是当我通过CLI运行代码时,它返回“没有模块名为imutils”。我不想使用虚拟环境。我在Pi上正确地运行了cv2,这没有问题,对这个imutils问题有修复吗?

更新、升级、删除项目,但这是必要的。

代码语言:javascript
复制
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”错误的情况下运行这段代码。

EN

回答 1

Stack Overflow用户

发布于 2019-04-08 16:33:05

如果您打算在Python3中使用该模块,则需要使用pip3安装它,以便将其安装在正确的位置。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55577297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档