未在opencv python中保存视频

2024-06-23 19:45:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我传递一个视频作为输入,抓取的视频就是输出。输出显示在窗口中,但最后它抛出一个属性错误,视频文件用给定的名称保存,但它是一个空文件。在

我问了很多类似的问题,我试了所有这些答案,但结果都是一样的。有人能告诉我这个代码有什么错误吗?在

import imutils
import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
            help="path to the (optional) video file")
args = vars(ap.parse_args())


lower = np.array([0, 0, 20], dtype="uint8")
upper = np.array([25, 255, 255], dtype="uint8")


if not args.get("video", False):
   camera = cv2.VideoCapture('videos/15-IMG_1090.MOV')

   fourcc = cv2.VideoWriter_fourcc(*'XVID')
   out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))


else:
   camera = cv2.VideoCapture('videos/15-IMG_1093.MOV')

   fourcc = cv2.VideoWriter_fourcc(*'XVID')
   out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))


 while True:
   # grab the current frame
   (grabbed, frame) = camera.read()

   if args.get("video") and not grabbed:
      break


   # resize the frame, convert it to the HSV color space,
   # and determine the HSV pixel intensities that fall into
   # the speicifed upper and lower boundaries

   frame = imutils.resize(frame, width=500)
   converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
   skinMask = cv2.inRange(converted, lower, upper)


   kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
   skinMask = cv2.erode(skinMask, kernel, iterations=2)
   skinMask = cv2.dilate(skinMask, kernel, iterations=2)


   skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0)
   skin = cv2.bitwise_and(frame, frame, mask=skinMask)


   out.write(skin)
   cv2.imshow("images", np.hstack([frame, skin]))

   # if the 'q' key is pressed, stop the loop
   if cv2.waitKey(1) & 0xFF == ord("q"):
      break


camera.release()
out.release()
cv2.destroyAllWindows()

这是错误。在

^{pr2}$

请任何人帮助我修复错误和保存新抓取的视频。 非常感谢。在


Tags: andtheimportifvideo错误npargs

热门问题