为什么我的VideoWriter会返回一个黑色的视频?

时间:2016-08-12 15:52:01

标签: python opencv video

我尝试使用VideoWriter导出正在录制的视频。虽然它有效,但视频位于左上角并被黑色包围。我不知道为什么会这样。视频本身正在保存,我可以看到它,但我不知道为什么有这么多的空白空间。这是我的代码:

#!/usr/bin/env python

import time
import picamera
import numpy as np
import cv2
import traceback
from pylepton import Lepton

def main(flip_v = False, alpha = 128, device = "/dev/spidev0.0"):
  # Create an array representing a 1280x720 image of
  # a cross through the center of the display. The shape of
  # the array must be of the form (height, width, color)
  a = np.zeros((240, 320, 3), dtype=np.uint8)
  lepton_buf = np.zeros((60, 80, 1), dtype=np.uint16)

  with picamera.PiCamera() as camera:
    #camera.resolution = (640, 480)
    #camera.framerate = 24
    #camera.vflip = flip_v
    #camera.start_preview()
    #camera.fullscreen = True
    # Add the overlay directly into layer 3 with transparency;
    # we can omit the size parameter of add_overlay as the
    # size is the same as the camera's resolution
    o = camera.add_overlay(np.getbuffer(a), size=(320,240), layer=3, alpha=int(alpha), crop=(0,0,80,60), vflip=flip_v)
    #Creating the VideoWriter object
    filename = time.strftime("%Y.%m.%d  %H.%M.%S", time.localtime()) + ".avi"
    fourcc =  cv2.cv.CV_FOURCC('I','4','2','0')
    out = cv2.VideoWriter(filename, fourcc, 8.0, (60, 80))

    k = 0

    try:
      time.sleep(0.2) # give the overlay buffers a chance to initialize
      with Lepton(device) as l:
        last_nr = 0
        while True:
          _,nr = l.capture(lepton_buf)
          #resize the frame before writing it to a file
          resized_frame = cv2.resize(a, (60, 80))

          out.write(np.uint8(rezised_frame))

          if nr == last_nr:
            # no need to redo this frame
            continue
          last_nr = nr
          cv2.normalize(lepton_buf, lepton_buf, 0, 65535, cv2.NORM_MINMAX)
          np.right_shift(lepton_buf, 8, lepton_buf)
          a[:lepton_buf.shape[0], :lepton_buf.shape[1], :] = lepton_buf
          o.update(np.getbuffer(a))

          # Once the program has run 2000 time break the loop
          k += 1

          if k == 2000:
                 out.release()
                 break

    except Exception:
      traceback.print_exc()
    finally:
      camera.remove_overlay(o)

0 个答案:

没有答案