import cv2
from base_camera import BaseCamera


class Camera(BaseCamera):
    video_source = 0

    @staticmethod
    def set_video_source(source):
        Camera.video_source = source

    @staticmethod
    def frames():
        camera = cv2.VideoCapture(Camera.video_source)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

        while True:
            # read current frame
            _, img = camera.read()

            # encode as a jpeg image and return it
            yield cv2.imencode('.jpg', img)[1].tobytes()

import os
import cv2
from base_camera import BaseCamera


class Camera(BaseCamera):
    video_source = 0

    def __init__(self):
        if os.environ.get('OPENCV_CAMERA_SOURCE'):
            Camera.set_video_source(int(os.environ['OPENCV_CAMERA_SOURCE']))
        super(Camera, self).__init__()

    @staticmethod
    def set_video_source(source):
        Camera.video_source = source

    @staticmethod
    def frames():
        camera = cv2.VideoCapture(Camera.video_source)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

        while True:
            # read current frame
            _, img = camera.read()

            # encode as a jpeg image and return it
            yield cv2.imencode('.jpg', img)[1].tobytes()

Use OPENCV_CAMERA_SOURCE environment variable to set source
Use OPENCV_CAMERA_SOURCE environment variable to set source

+ import os
  import cv2
  from base_camera import BaseCamera
  
  
  class Camera(BaseCamera):
      video_source = 0
+ 
+     def __init__(self):
+         if os.environ.get('OPENCV_CAMERA_SOURCE'):
+             Camera.set_video_source(int(os.environ['OPENCV_CAMERA_SOURCE']))
+         super(Camera, self).__init__()
  
      @staticmethod
      def set_video_source(source):
          Camera.video_source = source
  
      @staticmethod
      def frames():
          camera = cv2.VideoCapture(Camera.video_source)
          if not camera.isOpened():
              raise RuntimeError('Could not start camera.')
  
          while True:
              # read current frame
              _, img = camera.read()
  
              # encode as a jpeg image and return it
              yield cv2.imencode('.jpg', img)[1].tobytes()
  
Use OPENCV_CAMERA_SOURCE environment variable to set source
## Code Before:
import cv2
from base_camera import BaseCamera


class Camera(BaseCamera):
    video_source = 0

    @staticmethod
    def set_video_source(source):
        Camera.video_source = source

    @staticmethod
    def frames():
        camera = cv2.VideoCapture(Camera.video_source)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

        while True:
            # read current frame
            _, img = camera.read()

            # encode as a jpeg image and return it
            yield cv2.imencode('.jpg', img)[1].tobytes()

## Instruction:
Use OPENCV_CAMERA_SOURCE environment variable to set source
## Code After:
import os
import cv2
from base_camera import BaseCamera


class Camera(BaseCamera):
    video_source = 0

    def __init__(self):
        if os.environ.get('OPENCV_CAMERA_SOURCE'):
            Camera.set_video_source(int(os.environ['OPENCV_CAMERA_SOURCE']))
        super(Camera, self).__init__()

    @staticmethod
    def set_video_source(source):
        Camera.video_source = source

    @staticmethod
    def frames():
        camera = cv2.VideoCapture(Camera.video_source)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

        while True:
            # read current frame
            _, img = camera.read()

            # encode as a jpeg image and return it
            yield cv2.imencode('.jpg', img)[1].tobytes()
