简体   繁体   中英

How can I open up webcam and process images with docker and OpenCV?

I have a python script that uses OpenCV and when somebody runs my script I want process the image from their webcam and give back a result. How can I make it?

This is how I tried:

My simple test python script:

import cv2
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    print ret

This is in my dockerfile:

FROM gaborvecsei/opencvinstall

ADD testcode.py ./testcode.py

#Start sample app
CMD ["python", "testcode.py"]

After I build this and I run it it always prints False so that means I do not have any image from the webcam.

How can I get the images?

要使您的 cam 从 docker 容器可见,您应该在启动容器时传递 --device 参数。

docker start --device /dev/video0 helloworld:1.0.0

You have to show the frame. Using cv2.imshow('Preview', frame) . And outside the while loop you have to release the camera, so you should type cap.release() .

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