作业7:
在视频中显示数字,随机的数字。
参考链接:
https://blog.csdn.net/cherry_yu08/article/details/83591276 (python opencv在图片上显示中文字符)
https://blog.csdn.net/dcrmg/article/details/79108491(OpenCV-Python在图片上输出中文)
https://blog.csdn.net/xiaoyufei117122/article/details/78259415 (基于Python OpenCV加载视频文件,显示,并绘制自定义文字)
http://www.runoob.com/python3/python3-random-number.html(随机数)
https://blog.csdn.net/zzdxxn/article/details/89053623(作业3)
答案:
import cv2 as cv
import numpy as np
import random
capture = cv.VideoCapture(0) # 创建一个VideoCapture对象
result_video = "result.mp4"
while(True):
ret, frame = capture.read() # 一帧一帧读取视频
#gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # 对每一帧做处理,设置为灰度图
# 设置写入视频的编码格式
fourcc = cv.VideoWriter_fourcc(*"mp4v")
fps_video = capture.get(cv.CAP_PROP_FPS) #获取视频帧率
# 获取视频宽度
frame_width = int(capture.get(cv.CAP_PROP_FRAME_WIDTH))
# 获取视频高度
frame_height = int(capture.get(cv.CAP_PROP_FRAME_HEIGHT))
videoWriter = cv.VideoWriter(result_video, fourcc, fps_video, (frame_width, frame_height))
frame += 1
#left_x_up = int(369)
#left_y_up = int(328)
#right_x_down = int(469)
#right_y_down = int(200)
# 文字坐标
word_x = 369
word_y = 328
#cv.rectangle(frame, (left_x_up, left_y_up), (right_x_down, right_y_down), (55, 255, 155), 5)
x = random.randint(0, 9)
cv.putText(frame, '%d' % x, (word_x, word_y), cv.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
videoWriter.write(frame)
cv.imshow('frame', frame) # 显示结果
if cv.waitKey(1) &0xFF == ord('q'): # 按q停止
break
capture.release() # 释放cap,销毁窗口
cv.destroyAllWindows()