我试图在Google Collab上用PyGame和OpenCV在视频上显示文本,但输出中没有显示文本.输出文件仍与原始视频相同.在OpenCV视图(cv2_imshow(frame_with_text))中,也看不到文本.

import pygame
import cv2
from google.colab.patches import cv2_imshow

# Upload video
cap = cv2.VideoCapture(os.path.join('sample_data', 'BACKGROUND.mp4'))

# Get video information
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = 30 #cap.get(cv2.CAP_PROP_FPS)
frames_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frames_count / fps

# Export video
directory = r'sample_data'
output_file = os.path.join(directory, f'teste.mp4')
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('/content/drive/MyDrive/teste.mp4', fourcc, fps, (width, height))

# Create a pygame suface with the same dimention as the video
pygame.init()
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()

def edit():
  while cap.isOpened():
      ret, frame = cap.read()

      if not ret:
          break
      
      font = pygame.font.SysFont('Arial', 50)
      text = "This is a test"
      pista_surface = font.render(text, True, (255, 255, 255))
      screen.blit(pista_surface, (94, 479))
      # Actual time in seconds
      current_time = cap.get(cv2.CAP_PROP_POS_FRAMES) / fps

      # Convert OpenCV frame to a pygame surface
      pygame_frame = pygame.surfarray.make_surface(frame.swapaxes(0, 1))

      # Update Pygame screen
      pygame.display.flip()

      # Convert Pygame surface to a OpenCV frame
      frame_with_text = pygame.surfarray.array3d(pygame_frame).swapaxes(0, 1)

      # Save frame to the output
      out.write(frame_with_text)

      # Visualize frame 
      cv2_imshow(frame_with_text)

      if cv2.waitKey(1) & 0xFF == ord('q'):
        break

      # Set the same FPS as the original video
      clock.tick(fps)

      # Verify if the end of the video was reached
      if current_time >= frames_count:
        break

  cap.release()
  out.release()
  cv2.destroyAllWindows()


edit()

我try 了电影,但我得到了图像魔术错误的谷歌协作,因此我使用这个方法.我试图安装图像魔术,但我找到的所有解决方案都不起作用.

推荐答案

您必须在写入到输出的表面上绘制文本,并且需要在PYGAME屏幕上绘制该表面

def edit():
    while cap.isOpened():
        # [...]

        # Convert OpenCV frame to a pygame surface
        pygame_frame = pygame.surfarray.make_surface(frame.swapaxes(0, 1))

        # blit text on surface
        pygame_frame.blit(pista_surface, (94, 479))

        # blit surface on Pygame screen
        screen.blit(pygame_frame, (0, 0))

        # Update Pygame screen
        pygame.display.flip()

        # Convert Pygame surface to a OpenCV frame
        frame_with_text = pygame.surfarray.array3d(pygame_frame).swapaxes(0, 1)

        # Save frame to the output
        out.write(frame_with_text)

        # [...]

Python相关问答推荐

如何在msgraph.GraphServiceClient上进行身份验证?

Matlab中是否有Python的f-字符串等效物

将jit与numpy linSpace函数一起使用时出错

在Google Colab中设置Llama-2出现问题-加载判断点碎片时Cell-run失败

Odoo 16使用NTFS使字段只读

在pandas中使用group_by,但有条件

如何在表中添加重复的列?

如何使用Numpy. stracards重新编写滚动和?

判断solve_ivp中的事件

在单次扫描中创建列表

基于Scipy插值法的三次样条系数

如何将数据帧中的timedelta转换为datetime

Python将一个列值分割成多个列,并保持其余列相同

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

numpy数组和数组标量之间的不同行为

Python日志(log)库如何有效地获取lineno和funcName?

EST格式的Azure数据库笔记本中的当前时间戳

Pandas ,快速从词典栏中提取信息到新栏

如何批量训练样本大小为奇数的神经网络?

大Pandas 每月重新抽样200万只和300万只