我正在尝试使用 open cv 编写视频输出,但没有输出,我正在使用 google colab 来执行代码
from google.colab import drive
drive.mount('/content/drive')
DataFolder = "/content/gdrive/MyDrive/Colab_Notebooks/toyvideo_folder"
!pip install opencv-python
!pip install glib
!pip install -r https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt
import cv2
%matplotlib inline
from matplotlib import pyplot as plt
from tqdm import tqdm
import torch
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
def annotate_image(image):
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = model(image_rgb)
for i in range(len(results.pandas().xyxy[0].name)):
name = results.pandas().xyxy[0].name[i]
startX = int(results.pandas().xyxy[0].xmin[i])
startY = int(results.pandas().xyxy[0].ymin[i])
endX = int(results.pandas().xyxy[0].xmax[i])
endY = int(results.pandas().xyxy[0].ymax[i])
confidence = results.pandas().xyxy[0].confidence[i]
label = "{}: {:.2f}%".format(name, confidence * 100)
if confidence > 0.6:
cv2.rectangle(image, (startX, startY), (endX, endY),
(255,0,0), 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(image, label, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 2)
return results
video_in_file = DataFolder + "/data/toycars.mp4"
video_out_file = DataFolder + "/data/out.mp4"
print("[INFO] accessing video stream...")
v_in = cv2.VideoCapture(video_in_file)
total_frame = int(v_in.get(cv2.CAP_PROP_FRAME_COUNT))
for frame_no in tqdm(range(total_frame), desc="Processing Video..."):
(grabbed, frame) = v_in.read()
# if the frame was not grabbed then we've reached the end of
# the video stream so exit the script
if not grabbed:
print("[INFO] no frame read from stream - exiting")
break
annotated_img = annotate_image(frame)
if v_out is None:
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
v_out = cv2.VideoWriter(video_out_file, fourcc, int(v_in.get(cv2.CAP_PROP_FPS)), (frame.shape[1], frame.shape[0]), True)
v_out.write(annotated_img)
print("\n[INFO] cleaning up...")
v_out.release()
v_in.release()
video_out = "/content/gdrive/MyDrive/data/out_display.mp4"
!ffmpeg -y -loglevel info -i $video_out_file -vf scale=640:480 $video_out
完整的错误消息:
ffmpeg 版本 4.4.2-0ubuntu0.22.04.1 版权所有 (c) 2000-2021 FFmpeg 开发人员
使用 gcc 11 构建(Ubuntu 11.2.0-19ubuntu1)
配置: --prefix=/usr --extra-version=0ubuntu0.22.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux -gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable -libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable -libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librubberband --enable-libshine --enable -libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable -libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable -opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable -frei0r --enable-libx264 --enable-shared
libavutil 56.70.100 / 56.70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58.76.100 / 58.76.100
libavdevice 58.13.100 / 58.13.100
libavfilter 7.110.100 / 7.110.100
libswscale 5.9.100 / 5.9.100
libsresample 3.9.100 / 3.9.100
libpostproc 55.9.100 / 55.9.100
/content/gdrive/MyDrive/Colab_Notebooks/toyvideo_folder/data/out.mp4:没有这样的文件或目录
from google.colab import drive
drive.mount('/content/drive')
DataFolder = "/content/gdrive/MyDrive/Colab_Notebooks/toyvideo_folder"
您的 /content/drive 和 /content/gdrive 应该是其中之一