简体   繁体   中英

On stopping and starting ubuntu EC2 server, no module named 'cv2' error. (Import cv2 error)

I installed opencv from the source as ( i tried pip and sudo ways but that didn't switch on the cam on the client's machine). So, I installed it successfully and cv2.so file was rightly linked to the virtual environment created. Even on importing cv2 it was working. Post I stopped the EC2 instance as it was chargeable to work further on it next day, I found out that now on importing cv2 its throwing 'no found module named cv2'. Please guide.

Update 1 - When i run the code, it executes without error however, when i load the page then following error comes in - cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:877: error: (-215:Assertion failed).image.empty() in function 'imencode'

I understand that imencode received empty image and thats why above code. But code runs perfectly fine on local machine. When i run it on ec2 then this error pops in.

 run.py


from flask import Flask, render_template, Response, url_for
import io
import cv2

app = Flask(__name__)


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


def gen():
    vc = cv2.VideoCapture(0)
    """Video streaming generator function."""
    while True:
        read_return_code, frame = vc.read()
        encode_return_code, image_buffer = cv2.imencode('.jpg', frame)
        io_buf = io.BytesIO(image_buffer)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + io_buf.read() + b'\r\n')


@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(
        gen(),
        mimetype='multipart/x-mixed-replace; boundary=frame'
    )



# main driver function 
if __name__ == '__main__': 
    app.run(host='0.0.0.0',port=5000)

index.html
<!DOCTYPE html>
<html>
<head>



    
</head>
<body>
    <div class="container" style="width: 90vw; text-align: center;">
        <h1 style="font-size: 4vh; color: blue;">Intelligent Camera</h1>    
        <h4 style="font-size: 2vh;">Best Object Detection Cam</h4>    
        <img src="{{url_for('video_feed')}}" style="width: calc(70% - 10px); height: calc(60% - 10px); background-color: white; border: double black;">
       

    </div>



</body>

</html>

You can try this Command to install CV2.

pip install opencv-python

That error is telling you that frame is None .

When warming up a video stream, test for errors ( read_return_code in your case). Depending on the video source, you might get a few of these before you start getting valid frames.

But I think you have a different problem. If you're deploying on ec2, how are you connecting a camera?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM