включение веб-камеры через функцию JS в Colab

Я пытаюсь активировать свою веб-камеру через функцию JS, работая в Colab. Основная задача вызвать эту JS функцию из Python кода. Вот код:

def take_photo(filename='photo.jpg', quality=0.8):
  js = Javascript('''
    async function takePhoto(quality) {
      const div = document.createElement('div');

      const video = document.createElement('video');
      video.style.display = 'block';
      const stream = await navigator.mediaDevices.getUserMedia({video: true});

      document.body.appendChild(div);
      div.appendChild(video);
      video.srcObject = stream;
      await video.play();

      // Resize the output to fit the video element.
      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

      const canvas = document.createElement('canvas');
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      canvas.getContext('2d').drawImage(video, 0, 0);
      stream.getVideoTracks()[0].stop();
      div.remove();
      return canvas.toDataURL('image/jpeg', quality);
    }
    ''')
  display(js)
  data = eval_js('takePhoto({})'.format(quality))
  resp = urllib.request.urlopen(data)
  image = np.asarray(bytearray(resp.read()), dtype="uint8")
  image = cv2.imdecode(image, cv2.IMREAD_COLOR)
  # return the image
  return image

# to capture an image
img = take_photo()

scenes_1 = []
scenes_2 = []

scene_manager1 = SceneManager()
scene_manager1.add_detector(ContentDetector(threshold=30.0))

scene_manager2 = SceneManager()
scene_manager2.add_detector(ContentDetector(threshold=30.0))

def callback_1(image, frame_num):
  global scenes_1
  print("callback_1: Found a scene on video 1.")
  scenes_1 += [(image, frame_num)]

def callback_2(image, frame_num):
  global scenes_2
  print("callback_2: Found a scene on video 2.")
  scenes_2 += [(image, frame_num)]

cap = img
scene_manager1.detect_scenes(cap, callback = callback_1)

cap2 =cv2.VideoCapture('Our Story.mp4', 0)
scene_manager2.detect_scenes(cap2, callback = callback_2)

Но я получаю следующую ошибку:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-457214aba434> in <module>()
     75 
     76 cap = img
---> 77 scene_manager1.detect_scenes(cap, callback = callback_1)
     78 
     79 cap2 =cv2.VideoCapture('Our Story.mp4', 0)

/usr/local/lib/python3.7/dist-packages/scenedetect/scene_manager.py in detect_scenes(self, frame_source, end_time, frame_skip, show_progress, callback)
    624         end_frame = None
    625         self._base_timecode = FrameTimecode(
--> 626             timecode=0, fps=frame_source.get(cv2.CAP_PROP_FPS))
    627 
    628         total_frames = math.trunc(frame_source.get(cv2.CAP_PROP_FRAME_COUNT))

AttributeError: 'numpy.ndarray' object has no attribute 'get'

Подскажите пожалуйста, как я могу решить эту проблему.


Ответы (0 шт):