GMane Python wrote:
Hello All.
Using a network camera with built-in webserver, I'd like to have a python
program download .jpg files on a local lan. the location is
http://<ip-address>/jpg/image.jpg.
Currently, I'm importing urllib and using urlopen to the address, then
read()-ing it, saving it to a binary file. All that is working great, but
maybe a bit slowly. I'm getting ~2.3 frames per second, and would like
between 5-10 frames per second.
Am I approaching this incorrectly? I have to do a urlopen, then .read()
for each image. Is there any way to 'persist' the urlopen so I just have to
keep read()-ing or maybe is there a type of streaming read? I have many
cameras, so there are many threads simultaneously reading and dropping them
in a central Queue for saving later.
I appreciate it!
-Dave
WebImage = urllib.urlopen("http://<ip-address>/jpg/image.jpg").read()
QueuePacket = []
QueuePacket.append(WebImage)
What your doing, downloading images one at a time, is not the most
efficient method. What you want to do is to enable the mjpeg (motion
jpeg) mode of the camera. I am using a Trendnet camera. To get the mjpeg
stream out of this camera, I use: "camera-url/VIDEO.CGI". If you put
this address into the Firefox address bar, and then press refresh (for
some reason it doesn't start unless you press refresh) it will start.
The camera will hold the connection open and send jpeg images as a
multipart mime message.
The camera will respond with the header:
--------------------------------
Server: Camera Web Server/1.0
Auther: Steven Wu
MIME-version: 1.0
Cache-Control: no-cache
Content-Type:multipart/x-mixed-replace;boundary=--video boundary--
--------------------------------
Then it will repeatedly send send:
-------------------------------
--video boundary--
Content-length: 27577
Content-type: image/jpeg
<jpeg data>
--video boundary--
-------------------------------
I had a url with a good explanation of this but I can't find it now. If
I find it I will post it.
Here is a simplified version of my current thinking.
I took out the error handeling stuff to make the logic
clear.
--------------------------------------------
Thread that reads from the camera
--------------------------------------------
h=httplib.HTTP(camera url)
h.putrequest('GET','VIDEO.CGI')
h.putheader('Accept','text/html')
h.putheader('Accept','image/jpeg') # Trendnet camera works without this
h.endheaders()
errcode,errmsg,headers=h.getreply()
f=h.getfile()
<if errcode is 200 go on the the next part>
while 1:
data=f.readline()
if data[0:15]=='Content-length:':
count=int(data[16:])
n=f.readline() # skip over Content-type: image/jpeg\n'
n=f.readline() # skip over \n'
s = f.read(count)
p=file(tempfile,'wb')
p.write(s)
p.close()
<trigger main thread to display image from tempfile>
--------------------------------------------
The first part sets it up and the while loop gets the jpeg data
as fast is the camera will send it.
Mark Rainess