简体   繁体   中英

How can OpenCV with Python Flask read images from folder and stream them to website?

def gen2():

    img = [cv2.imread(file) for file in glob.glob("path/*.jpg")]
    img = cv2.resize(img, (0,0), fx = 0.5, fy = 0.5)
    frame = cv2.imencode('.jpg', img)[1].tobytes()
    yield(b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

I have some images in order with format image_0, image_1,...,image_999.jpg, for example. I try the above code but it does not work, I think I need a loop for the frame and yield , I am new to OpenCV and have no idea. Thanks in advance.

Yes, img is an array of images, so you will need a loop. Try this

def gen2():
    imgs = [cv2.imread(file) for file in glob.glob("path/*.jpg")]
    imgs = [cv2.resize(img, (0,0), fx = 0.5, fy = 0.5) for img in imgs]
    frames = [cv2.imencode('.jpg', img)[1].tobytes() for img in imgs]
    for frame in frames :
        yield(b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

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