使用Python OpenCV将文本插入流中

时间:2014-07-31 07:32:18

标签: 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 :(得分:0)

首先你需要 UDP 套接字服务器!该服务器将为您监听一些IP和PORT。然后,您可以使用OpenCV。

答案 1 :(得分:0)

此代码适用于linux网络摄像头!我已经写了9个月前检测角落!所以对你来说我加上了一个puttext!
我也删除索引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()

答案 2 :(得分:0)

我知道它为时已晚,但要回答您的问题,您可以使用流的地址更改VideoCapture类中的视频源。例:

capture = cv2.VideoCapture("http://192.168.1.1/stream/video.mjpeg")

请注意,您必须包含流中使用的实际文件名+格式。