Skip to content

Instantly share code, notes, and snippets.

@enric1994
Last active February 18, 2022 11:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save enric1994/7ab05985f775cb2954de6c30b72b07f9 to your computer and use it in GitHub Desktop.
Save enric1994/7ab05985f775cb2954de6c30b72b07f9 to your computer and use it in GitHub Desktop.
Using the webcam in a GPU enabled Docker Compose
import cv2
video_capture = cv2.VideoCapture(0)
anterior = 0
while True:
if not video_capture.isOpened():
print('Unable to load camera. Use the command "xhost +"')
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
cv2.imshow('Video', frame)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
version: '2.3'
services:
webcam:
image: webcam
container_name: webcam
working_dir: /webcam
build:
context: .
dockerfile: Dockerfile.webcam
environment:
- DISPLAY=unix$DISPLAY
volumes:
- ./:/webcam
- /dev/video0:/dev/video0
- /tmp/.X11-unix:/tmp/.X11-unix
command: bash -c "nvidia-smi && python -u camera.py"
privileged: true
runtime: nvidia
FROM tensorflow/tensorflow:latest-gpu-py3
RUN apt update -y && apt install -y \
libsm6 \
libxext6 \
libxrender-dev
RUN pip install \
opencv-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment