-2
from collections import deque
from imutils.video import VideoStream
import numpy as np
import argparse
import cv2
import imutils
import time


ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
    help="Users/ejr/Desktop/curry.mp4")
ap.add_argument("-b", "--buffer", type=int, default=64,
    help="max buffer size")
args = vars(ap.parse_args())
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
pts = deque(maxlen=args["buffer"])
if not args.get("video", False):
    vs = VideoStream(src=0).start()
else:
    vs = cv2.VideoCapture(args["video"])
time.sleep(2.0)
while True:
    frame = vs.read()
    frame = frame[1] if args.get("video", False) else frame
    if frame is None:
        break
    frame = imutils.resize(frame, width=600)
    blurred = cv2.GaussianBlur(frame, (11, 11), 0)
    hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, greenLower, greenUpper)
    mask = cv2.erode(mask, None, iterations=2)
    mask = cv2.dilate(mask, None, iterations=2)
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    center = None
    if len(cnts) > 0:
        c = max(cnts, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
        if radius > 10:
            cv2.circle(frame, (int(x), int(y)), int(radius),
                (0, 255, 255), 2)
            cv2.circle(frame, center, 5, (0, 0, 255), -1)
    pts.appendleft(center)
    for i in range(1, len(pts)):
        if pts[i - 1] is None or pts[i] is None:
            continue
        thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
        cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
if not args.get("video", False):
    vs.stop()
else:
    vs.release()
cv2.destroyAllWindows()

This is my code, I have no idea why I keep getting the cv2 there is no module error when I try to pip install it. (I am on a Mac, Python 3.7.4). I am wondering what is the import error here, has the version changed, do I need to use Python 2, or is there like a newer cv that I should use

2
  • 1
    Your description is not clear: is the error upon import, or installation? If it's on installation, your posted code is irrelevant to the problem; we need the sequence that produces the error. If you you're failing upon import, we need that code and message only ... and most of your posted code is irrelevant.
    – Prune
    Commented Nov 23, 2019 at 0:11
  • Welcome to StackOverflow. See minimal, reproducible example. We cannot effectively help you until you post your MRE code and accurately specify the problem.
    – Prune
    Commented Nov 23, 2019 at 0:11

1 Answer 1

0

Try:

pip install opencv-python

or

pip3.7 install opencv-python

I think you are running pip install cv2, which it does not exists since the project of cv2 is: https://pypi.org/project/opencv-python/

Not the answer you're looking for? Browse other questions tagged or ask your own question.