作业三:
摄像头显示灰色图像,并且在固定位置显示一个矩形框,不能手动框选
参考链接:
*** https://www.cnblogs.com/ArchitecTang/p/10189997.html (Python-opencv摄像头图像捕获)
https://blog.csdn.net/li_haiyu/article/details/80074394
https://blog.csdn.net/LutherK/article/details/80114599(学习笔记:简介yolo在python下对图片的检测及画框)
http://www.cnblogs.com/zhenglisai/p/9888096.html (【深度学习】使用opencv在视频上添加文字和标记框)
答案:
1、两次循环触发摄像头
2、显示视频放在前面了
3、显示得不应该是gray而是新生成的frame
4、注意去掉后一个循环之后得缩进
5、注意视频得长宽高和你设置的框的尺寸匹配
6、
videoWriter = cv.VideoWriter(result_video, fourcc, fps_video, (frame_width, frame_height))
答案:
# 设置写入视频的编码格式
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 = left_x_up + 5
word_y = left_y_up + 25
cv.rectangle(frame, (left_x_up, left_y_up), (right_x_down, right_y_down), (55, 255, 155), 5)
cv.putText(frame, 'frame_%s' % frame, (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()
答案:
import cv2 as cv
import numpy as np
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)
cv.putText(frame, 'frame_%s' % frame, (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()