基本上,我尝试通过
st.file_uploader()
上传视频文件,然后将该视频传递给 cv2.VideoCapture()
。但这给了我一个错误..
uploaded_file = st.file_uploader("Upload video", type=["mp4", "avi"])
...
if uploaded_file is not None:
# Read video from the uploaded file
video_bytes = uploaded_file.read()
# Convert the video bytes to OpenCV compatible format
video_np_array = np.frombuffer(video_bytes, np.uint8)
frame = cv2.imdecode(video_np_array, cv2.IMREAD_COLOR)
# Display video frame by frame
while True:
# Convert the frame from BGR to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Display the frame
st.image(frame_rgb, channels="RGB")
# Check if 'q' key is pressed to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Read next frame
if len(video_np_array) == 0:
break
# Update frame
frame = cv2.imdecode(video_np_array, cv2.IMREAD_COLOR)
我想看这个视频文件。
导入CV2 将 numpy 导入为 np 将streamlit导入为st
uploaded_file = st.file_uploader("选择视频文件", type=["mp4", "avi"])
如果 uploaded_file 不是 None: # 从上传的文件中读取视频 video_bytes = uploaded_file.read()
# Convert the video bytes to OpenCV compatible format
video_np_array = np.frombuffer(video_bytes, np.uint8)
frame = cv2.imdecode(video_np_array, cv2.IMREAD_COLOR)
# Display video frame by frame
while True:
# Check if frame is None (no more frames in the video)
if frame is None:
break
# Convert the frame from BGR to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Display the frame
st.image(frame_rgb, channels="RGB")
# Check if 'q' key is pressed to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Read next frame
success, frame = cv2.imdecode(video_np_array, cv2.IMREAD_COLOR)
if not success:
break