import js
import numpy as np
import cv2

def process_frame_with_opencv(video_element):
    # ビデオの寸法を取得
    width, height = video_element.videoWidth, video_element.videoHeight

    # キャンバスを作成してフレームをキャプチャ
    canvas = js.document.createElement("canvas")
    canvas.width, canvas.height = width, height
    ctx = canvas.getContext("2d")
    ctx.drawImage(video_element, 0, 0, width, height)

    # 生のピクセルデータを取得
    image_data = ctx.getImageData(0, 0, width, height).data

    # OpenCV用にnumpy配列に変換後、 RGBAからBGRに変換
    frame = np.asarray(image_data, dtype=np.uint8).reshape((height, width, 4))
    frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGBA2BGR)

    # OpenCVで画像を処理(省略)
    return frame_bgr