미분류

wxpython + opencv 사용하여 ip camera 영상 출력

webnautes 2016. 5. 28. 20:18
반응형









opencv 3.0 부터는 cv 모듈이 제거 된거 같습니다.. 그래서 기존 코드는 동작안될듯 합니다..

대신 사용할 수 있는 코드를 찾았습니다..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
import urllib 
import numpy as np
 
stream=urllib.urlopen('http://plazacam.studentaffairs.duke.edu/mjpg/video.mjpg')
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), 0)
        cv2.imshow('i',i)
        if cv2.waitKey(1==27:
            exit(0)   
cs



출처 

http://stackoverflow.com/questions/21702477/how-to-parse-mjpeg-http-stream-from-ip-camera








아래는 기존 코드 입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import wx
import cv2
 
import cv2.cv as cv
import urllib2
from cStringIO import StringIO
import PIL.Image as pil
url="http:⁄⁄192.168.0.8⁄snapshot.cgi?user=admin&pwd="
 
 
class ShowCapture(wx.Panel):
    def __init__(self, parent, fps=15):
        wx.Panel.__init__(self, parent)
 
        img_file = urllib2.urlopen(url)
        im = StringIO(img_file.read())
        source = pil.open(im).convert("RGB")
        self.bitmap = cv.CreateImageHeader(source.size, cv.IPL_DEPTH_8U, 3)
        cv.SetData(self.bitmap, source.tostring())
        cv.CvtColor(self.bitmap, self.bitmap, cv.CV_RGB2BGR)
 
        width = self.bitmap.width;
        height = self.bitmap.height;
        wxImage = wx.EmptyImage(width, height)
        cv_ImageBuf = cv.CreateImage ((width, height), cv.IPL_DEPTH_8U, 3)
        cv.CvtColor(self.bitmap, cv_ImageBuf, cv.CV_BGR2RGB);
        wxImage.SetData(cv_ImageBuf.tostring())
        self.bmp2 = wxImage.ConvertToBitmap()
 
 
        self.timer = wx.Timer(self)
        self.timer.Start(1000.⁄fps)
 
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_TIMER, self.NextFrame)
 
       
 
 
    def OnPaint(self, evt):      
        dc = wx.BufferedPaintDC(self)
        dc.DrawBitmap(self.bmp2, 00)        
            
 
    def NextFrame(self, event):
        img_file = urllib2.urlopen(url)
        im = StringIO(img_file.read())
        source = pil.open(im).convert("RGB")
        self.bitmap = cv.CreateImageHeader(source.size, cv.IPL_DEPTH_8U, 3)
        cv.SetData(self.bitmap, source.tostring())
        cv.CvtColor(self.bitmap, self.bitmap, cv.CV_RGB2BGR)
 
        width = self.bitmap.width;
        height = self.bitmap.height;
        wxImage = wx.EmptyImage(width, height)
        cv_ImageBuf = cv.CreateImage ((width, height), cv.IPL_DEPTH_8U, 3)
        cv.CvtColor(self.bitmap, cv_ImageBuf, cv.CV_BGR2RGB);
        wxImage.SetData(cv_ImageBuf.tostring())
        self.bmp2 = wxImage.ConvertToBitmap()
        self.Refresh()
 
def main():   
    app = wx.App()
    frame = wx.Frame(None)
    cap = ShowCapture(frame)
    frame.Show()
    app.MainLoop()
    capture.release()
 
 
if __name__ == '__main__':
    main()
cs



참고

http://stackoverflow.com/questions/11253820/python-urllib2-and-opencv

http://mvlab.blogspot.kr/2007/05/day-record-2007-may-15.html



반응형