我想添加到被显示在我的摄像头的视频一些文字,但我似乎无法得到它。我已经添加文本到图像用opencv实现之前,但该方法似乎对影片不同的,所以我怎么会去这样做。这是我的摄像头脚本:
import cv2
import numpy as np
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video stream or file")
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# Display the resulting frame
cv2.imshow('Frame',frame)
# Press Q on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
看一看在OpenCV's docs about putText。这里有一个快速劈我做了显示一些边框标签:
@staticmethod
def __draw_label(img, text, pos, bg_color):
font_face = cv2.FONT_HERSHEY_SIMPLEX
scale = 0.4
color = (0, 0, 0)
thickness = cv2.FILLED
margin = 2
txt_size = cv2.getTextSize(text, font_face, scale, thickness)
end_x = pos[0] + txt_size[0][0] + margin
end_y = pos[1] - txt_size[0][1] - margin
cv2.rectangle(img, pos, (end_x, end_y), bg_color, thickness)
cv2.putText(img, text, pos, font_face, scale, color, 1, cv2.LINE_AA)
在你的代码这样的事情应该做的:
if ret == True:
# draw the label into the frame
__draw_label(frame, 'Hello World', (20,20), (255,0,0))
# Display the resulting frame
cv2.imshow('Frame',frame)
你以某种方式进行绘制,你叫imshow后?我看不出有任何理由为什么视频应该表现不同。