如何在OpenCV Python中找到实时视频流(Android IPWebcam)的光流?

时间:2016-05-14 09:32:34

标签: python opencv video

我已经编写了一个代码,可以很好地记录我的笔记本电脑的网络摄像头录制的视频或视频..但我需要它直接从我的手机处理视频..现在我已经能够从手机和节目中获取视频它通过python。但是我需要这个视频的帧来应用OpticalFlow功能。 (calcOpticalFlowFarneback)..这是我的两个代码。我会感谢你的帮助。

这是从Android手机的相机到python

获取视频的代码
import cv2
import urllib2
import numpy as np
import sys

host = "192.168.1.2:8080"
if len(sys.argv)>1:
    host = sys.argv[1]

hoststr = 'http://' + host + '/video'
print 'Streaming ' + hoststr

stream=urllib2.urlopen(hoststr)

bytes=''
while True:
    bytes+=stream.read(1024)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
        cv2.imshow(hoststr,i)
        if cv2.waitKey(1) ==27:
            exit(0)

这个是运动流程的相关部分,请注意我需要视频中的帧

vid=cv2.VideoCapture('vidaflv.flv')

ret, frame = vid.read()
imgrayp = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)


while True:
    ret, frame = vid.read()
    if ret:
        imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)


        flow = cv2.calcOpticalFlowFarneback(imgrayp,imgray,None,0.5,3,15,3,5,1.2,0)


    cv2.imshow('Optical flow',draw_flow(frame,flow))
    imgrayp=imgray 
    if cv2.waitKey(1)==ord('e'):
        break

我无法弄清楚的是如何从直播视频代码中获取帧以放入我的光流功能..

1 个答案:

答案 0 :(得分:0)

由于能够测试这个,我尝试类似的事情:

import cv2
import urllib2
import numpy as np
import sys

host = "192.168.1.2:8080"
if len(sys.argv)>1:
    host = sys.argv[1]

hoststr = 'http://' + host + '/video'
print 'Streaming ' + hoststr

stream=urllib2.urlopen(hoststr)

bytes=''
FirstTime=True
while True:
    bytes+=stream.read(1024)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
        if FirstTime=True:
            FirstTime=False
            imgrayp = cv2.cvtColor(i,cv2.COLOR_BGR2GRAY)
        imgray = cv2.cvtColor(i,cv2.COLOR_BGR2GRAY)

        flow = cv2.calcOpticalFlowFarneback(imgrayp,imgray,None,0.5,3,15,3,5,1.2,0)

        cv2.imshow('Optical flow',draw_flow(frame,flow))
        if cv2.waitKey(1) ==27:
            exit(0)