免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5183 | 回复: 7

OpenCV的VideoWriter无法将连续图片输出为视频 [复制链接]

论坛徽章:
0
发表于 2018-04-03 23:01 |显示全部楼层
  1. import cv2 as cv
  2. import os

  3. #图片路径
  4. im_dir = 'C:/Users/Muzine/Desktop/jl047t1aaaff'
  5. #输出视频路径
  6. video_dir = 'E:/Graduation thesis'
  7. #帧率
  8. fps = 30
  9. #图片尺寸
  10. img_size = (320,240)
  11. #图片数量
  12. filelist = os.listdir(im_dir)
  13. num = len(filelist)


  14. fourcc = cv.VideoWriter_fourcc('M','J','P','G') #opencv3.0
  15. videoWriter = cv.VideoWriter(video_dir, fourcc, fps, img_size)

  16. for item in filelist:
  17.     if item.endswith('.png'):
  18.         item = im_dir + '/' + item
  19.         frame = cv.imread(item)
  20.         videoWriter.write(frame)
  21.         print(item)

  22. videoWriter.release()
  23. print('finish')
复制代码
很简单的把图片存为视频的功能,
明明print(item)中能正确把目录中的图片路径输出,
但是在输出路径不能找到视频,
甚至在在循环里也没办法cv.imshow输出图片。
有点小懵逼
希望能得到指点,thx

论坛徽章:
0
发表于 2018-04-04 08:25 |显示全部楼层
videoWriter.write(numpy.uint8(frame)) 试试这个,我最近正好弄这块

论坛徽章:
0
发表于 2018-04-04 08:30 |显示全部楼层
才发现,这里上传不了视频啊

论坛徽章:
0
发表于 2018-04-04 09:15 |显示全部楼层
可以上传附件

star_mix.flv

2.04 MB, 下载次数: 4

论坛徽章:
0
发表于 2018-04-08 08:34 |显示全部楼层
回复 4# dahe_1984

谢谢您的解答,清明假期出去玩了没能及时回复您。

我把
  1. videoWriter.write(frame)
复制代码
修改成
  1.         videoWriter.write(numpy.uint8(frame))
复制代码
之后还是没有能够导出视频,
还有这层给出的附件不能下载,
请问能麻烦您给一段测试代码吗?

论坛徽章:
0
发表于 2018-04-08 08:50 |显示全部楼层
    videoCapture = cv2.VideoCapture(args.video)
    fps = int(videoCapture.get(5))

      
    # get size
    size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
      
    #special format, I420-avi, MJPG-mp4  
    videoWriter = cv2.VideoWriter('oto_other.mp4', cv2.VideoWriter_fourcc(*'XVID'), fps, size)  
      
    #read
    success, frame = videoCapture.read()  
      
    while success :  
        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_frame = frame[:, :, ::-1]
        
        # The frame has the face in it
        rects = detector(rgb_frame, 1)
        if(len(rects) > 0):
            print("The frame has face...")
            # return the star face landmarks
            rgb_frame, rgb_landmark = getlmface(rgb_frame, args.pic)
            if(len(rgb_landmark) > 0):
                print("The frame has star face...")
                rep_pic = getclose(face_orientation(rgb_frame, rgb_landmark), posedict)
               
                im2, landmarks2 = read_im_and_landmarks(rep_pic)

               
                # get the affix matrix
                rgb_landmark = numpy.matrix(rgb_landmark)
                #im2 = im2[:, :, ::-1]
                #print(landmarks2[ALIGN_POINTS], rgb_landmark[ALIGN_POINTS])
                M = transformation_from_points(rgb_landmark[ALIGN_POINTS], landmarks2[ALIGN_POINTS])
               
                mask = get_face_mask(im2, landmarks2)            
                warped_mask = warp_im(mask, M, rgb_frame.shape)
               
                combined_mask = numpy.max([get_face_mask(rgb_frame, rgb_landmark), warped_mask], axis=0)            
                warped_im2 = warp_im(im2, M, rgb_frame.shape)            
                warped_corrected_im2 = correct_colours(rgb_frame, warped_im2, rgb_landmark)
                output_im = rgb_frame * (1.0 - combined_mask) + warped_corrected_im2 * combined_mask
                #cv2.imwrite("outputbef.png", output_im)
                frame = output_im[:, :, ::-1]
                #cv2.imwrite("outputaf.png", frame)
        else:
            frame = rgb_frame[:, :, ::-1]
        
        # show video
        #cv2.imshow("Oto Video", frame)
        # delay
        cv2.waitKey(int(1000/int(fps)))
        # generate video
        videoWriter.write(numpy.uint8(frame))
        # next frame
        success, frame = videoCapture.read()


代码比较长,没有全贴上来

论坛徽章:
0
发表于 2018-04-09 08:23 |显示全部楼层
回复 6# dahe_1984

谢谢,昨天按这个试着修改了一下终于能运行了

论坛徽章:
0
发表于 2018-04-09 09:10 |显示全部楼层
回复 7# muzine1995

没事,你怎么改好的,也发出来看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP