从视频流里获取第一帧图片



import numpy as np
import cv2 as cv
cap = cv.VideoCapture('/root/autodl-tmp/code/BackgroundMattingV2/src.mp4')

while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # cv2.imshow("frame", frame)
    # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # cv2.imshow("frame", frame)
    cv.imwrite("src.jpg", frame)
    break 
    # oneframereader = OneFrameReader(frame, transform=ToTensor())
    # with torch.no_grad():
    #     for src in DataLoader(oneframereader):                     # RGB tensor normalized to 0 ~ 1.
    #         # print(torch.max(src))
    #         # print(src.shape) # torch.Size([1, 3, 1280, 720])
    #         fgr, pha, *rec = model(src.cuda(), *rec, downsample_ratio)  # Cycle the recurrent states.
    #         com = fgr * pha + bgr * (1 - pha)              # Composite to green background. 
    #         # print(torch.max(com))
    #         # print(com.shape)
    #         com2 = com.mul(255).byte()
    #         com2 = torch.squeeze(com2, 0)
    #         # print(com2.shape)
    #         com2 = torch.permute(com2, (1,2,0))
    #         com2 = com2.detach().cpu().numpy()
    #         com2 = com2[..., ::-1] # h w c rgb2bgr
    #         # print(com2.shape)
    #         # print(type(com2))
    #         # print(np.max(com2))
    #         # print(com2.dtype)
    #         # print(com2.shape)
    #         # cv2.imshow("com2", com2)
    #         # cv2.waitKey(0)

    #         # plt.imshow(com2)
    #         writer.write(com)                              # Write frame.
# plt.show()
cap.release()
cv.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值