使用Python OpenCV向流中插入文本
你好,我想在视频流中添加文字,然后把这些文字再发送回视频流里。我的想法是让程序监听特定的IP地址和端口的流,使用OpenCV库把这个流分成一帧一帧的,然后在每一帧中插入文字,最后再把这些帧合并回流中。我需要用Python来实现这个功能。输入和输出的流将使用H.264编码。我找到了可以调整视频的Python代码,但我需要的是从流中进行处理。请给我一些建议。
import numpy as np
import cv2
capture = cv2.VideoCapture(0)
capture = cv2.VideoCapture("simpsnovi,prilis_drsne_pro_tv_03.avi")
flag, frame = capture.read()
width = np.size(frame, 1)
height = np.size(frame, 0)
#fourcc=cv2.cv.CV_FOURCC('I', 'Y', 'U', 'V'), #this is the codec that works for me
writer = cv2.VideoWriter(filename="your_writing_file.avi",
fourcc=cv2.cv.CV_FOURCC('X','V','I','D'), #this is the codec that works for me
fps=25, #frames per second, I suggest 15 as a rough initial estimate
frameSize=(width, height))
while True:
flag, frame = capture.read()
if flag == 0:
break
x = width/2
y = height/2
text_color = (255,0,0)
cv2.putText(frame, "your_string", (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=1,linetype=cv2.CV_AA)
writer.write(frame)
3 个回答
0
我知道这个问题已经过时了,但为了回答你的问题,你可以在你的VideoCapture类中用你的视频流地址来更改视频源。比如:
capture = cv2.VideoCapture("http://192.168.1.1/stream/video.mjpeg")
请注意,你需要在流中包含实际的文件名和使用的格式。
0
这段代码可以在Linux的摄像头上运行!这是我9个月前写的,用来检测角落的!为了方便你,我在里面加了一个文本显示功能!
另外,我删除了索引 linetype
,因为我的opencv报错了,不接受这个!
# -*- coding: utf-8 -*-
import numpy as np
import cv2
import sys
cap = cv2.VideoCapture(0)
#set the width and height, and UNSUCCESSFULLY set the exposure time
cap.set(3,1080)
cap.set(4,1024)
cap.set(15, 0.1)
flag, frame = cap.read()
width = np.size(frame, 1)
height = np.size(frame, 0)
def corner(filename) :
im= filename
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray,25,0.01,10)
corners = np.int0(corners)
for i in corners:
x,y = i.ravel()
cv2.circle(img,(x,y),3,(0,0,255),-1)
return im
while True:
ret, img = cap.read()
#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#blur = cv2.GaussianBlur(gray,(5,5),0)
#thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
x = width/2
y = height/2
text_color = (255,0,0)
cv2.putText(img, "your_string", (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=1)
cv2.imshow("input",corner(img))
#cv2.imshow("thresholded", imgray*thresh2)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
0
首先,你需要一个UDP套接字服务器!这个服务器会在某个IP地址和端口上等待你的连接。然后,你就可以开始使用OpenCV了。