Ask Your Question
0

No module named 'cv2'

asked 2019-12-15 02:29:25 -0500

Fahad Ahmed gravatar image

updated 2019-12-15 03:33:14 -0500

LBerger gravatar image

I have install opencv using pip install opencv-python ,it install but when i try to run code error pops up "no module named'cv2'" The code is

import cv2
from darkflow.net.build import TFNet
import numpy as np
import time

option= {
    'model':'cfg/yolo.cfg',
    'load':'bin/yolov2.weights',
    'threshold': 0.13,
    }

tfnet = TFNet(option)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]

capture = cv2.VideoCapture('me.mp4')
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 512)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 512)

while True:
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame)
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            label = result['label']
            confidence = result['confidence']
            text = '{}: {:.0f}%'.format(label, confidence * 100)
            frame = cv2.rectangle(frame, tl, br, color, 5)
            frame = cv2.putText(
                frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow('frame', frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
cv2.destroyAllWindows()

The error is

RESTART: C:\Users\Raja kashif\Desktop\project\darkflow-master\video test.py Traceback (most recent call last): File "C:\Users\Raja kashif\Desktop\project\darkflow-master\video test.py", line 1, in <module> import cv2 ModuleNotFoundError: No module named 'cv2'

edit retag flag offensive close merge delete

Comments

Ist seems as of the only line of code relevant is the first one.

Do you have multiple python versions installed? Are you using a virtualenv/conda or so?

bong0 gravatar imagebong0 ( 2019-12-15 03:09:16 -0500 )edit
1

@bong0 that's not really an answer. can you post as a comment?

LBerger gravatar imageLBerger ( 2019-12-15 03:34:23 -0500 )edit

@bong0. You already got OpenCV working your file.Actually, what wrong is that?

supra56 gravatar imagesupra56 ( 2019-12-15 08:16:16 -0500 )edit

@supra56 please read carrefully it is Fahad Ahmed's question and not bong0's

LBerger gravatar imageLBerger ( 2019-12-15 09:50:04 -0500 )edit
1

@LBerger. Apologized. I made a mistaken.

supra56 gravatar imagesupra56 ( 2019-12-15 10:34:09 -0500 )edit
1

@LBerger thanks for fixing this and sorry. I wanted to add this as a comment but didn't see any option to do so on Safari mobile (i'm now using the desktop browser)

bong0 gravatar imagebong0 ( 2019-12-15 11:11:15 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-12-17 01:35:22 -0500

arafat gravatar image

Did you check your environment variable? does your program gets the python location? If using pip list gives you opencv-python package name then the program may not be able to find your python folder location.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-12-15 02:29:25 -0500

Seen: 6,737 times

Last updated: Dec 17 '19