patternpythonMinor
Script for obtaining images from an IP security camera
Viewed 0 times
scriptobtainingcamerasecurityforimagesfrom
Problem
I am fairly new to Python and programming in general so I hope the code in this post is not too messy. I have the following code which I use for taking images from an IP security camera:
```
def Camera(timecount, cam_name, stream_url, username, password, work_dir, x):
x = x
work_dir = work_dir
h = httplib.HTTP(stream_url)
h.putrequest('GET', '/videostream.cgi')
h.putheader('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (username, password))[:-1])
h.endheaders()
errcode, errmsg, headers = h.getreply()
stream_file = h.getfile()
start = time.time()
end = start + timecount
while time.time() <= end:
if not os.path.isfile("/tmp/sec.lck"):
sys.exit()
x += 1
now = datetime.datetime.now()
dte = str(now.day) + "-" + str(now.month) + "-" + str(now.year)
dte1 = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + "." + str(now.microsecond)
cname = "Cam#: " + cam_name
dnow = """Date: %s """ % dte
dnow1 = """Time: %s""" % dte1
source_name = stream_file.readline() # '--ipcamera'
content_type = stream_file.readline() # 'Content-Type: image/jpeg'
content_length = stream_file.readline() # 'Content-Length: 19565'
b1 = b2 = b''
while True:
b1 = stream_file.read(1)
while b1 != chr(0xff):
b1 = stream_file.read(1)
b2 = stream_file.read(1)
if b2 == chr(0xd8):
break
# pull the jpeg data
framesize = int(content_length[16:])
jpeg_stripped = b''.join((b1, b2, stream_file.read(framesize - 2)))
# throw away the remaining stream data. Sorry I have no idea what it is
junk_for_now = stream_file.readline()
image_as_file = io.BytesIO(jpeg_stripped)
image_as_pil = Image.open(image_as_file)
draw = ImageDraw.Draw(image_as_pil)
dr
```
def Camera(timecount, cam_name, stream_url, username, password, work_dir, x):
x = x
work_dir = work_dir
h = httplib.HTTP(stream_url)
h.putrequest('GET', '/videostream.cgi')
h.putheader('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (username, password))[:-1])
h.endheaders()
errcode, errmsg, headers = h.getreply()
stream_file = h.getfile()
start = time.time()
end = start + timecount
while time.time() <= end:
if not os.path.isfile("/tmp/sec.lck"):
sys.exit()
x += 1
now = datetime.datetime.now()
dte = str(now.day) + "-" + str(now.month) + "-" + str(now.year)
dte1 = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + "." + str(now.microsecond)
cname = "Cam#: " + cam_name
dnow = """Date: %s """ % dte
dnow1 = """Time: %s""" % dte1
source_name = stream_file.readline() # '--ipcamera'
content_type = stream_file.readline() # 'Content-Type: image/jpeg'
content_length = stream_file.readline() # 'Content-Length: 19565'
b1 = b2 = b''
while True:
b1 = stream_file.read(1)
while b1 != chr(0xff):
b1 = stream_file.read(1)
b2 = stream_file.read(1)
if b2 == chr(0xd8):
break
# pull the jpeg data
framesize = int(content_length[16:])
jpeg_stripped = b''.join((b1, b2, stream_file.read(framesize - 2)))
# throw away the remaining stream data. Sorry I have no idea what it is
junk_for_now = stream_file.readline()
image_as_file = io.BytesIO(jpeg_stripped)
image_as_pil = Image.open(image_as_file)
draw = ImageDraw.Draw(image_as_pil)
dr
Solution
These statements are pointless, you can safely remove them:
The second statement is pointless, you can safely remove it:
The string
It would be better to put it in a global variable at the top, for example:
This is very tedious:
You could write it simpler using the
The variable names
not describing what they are, so I tried to give them more meaningful names.
It would be better to not use
This kind of control is best to keep at a central point,
for example in the main method,
where you have the other condition checks that may end with
It would be better to move the entire content of the
x = x
work_dir = work_dirThe second statement is pointless, you can safely remove it:
sys.exit(2)
sys.exit(0)The string
"/tmp/sec.lck" appears in many places in the file.It would be better to put it in a global variable at the top, for example:
LOCKFILE_PATH = "/tmp/sec.lck"This is very tedious:
dte = str(now.day) + "-" + str(now.month) + "-" + str(now.year)
dte1 = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + "." + str(now.microsecond)You could write it simpler using the
strftime method:datestr = now.strftime('%d-%m-%Y')
timestr = now.strftime('%H:%M:%S')The variable names
dte and dte1 were poor,not describing what they are, so I tried to give them more meaningful names.
It would be better to not use
sys.exit inside method calls.This kind of control is best to keep at a central point,
for example in the main method,
where you have the other condition checks that may end with
sys.exit.It would be better to move the entire content of the
if __name__ == "__main__": block to a method called main like this:def main():
# ... (all the code you previously had in the `if`)
if __name__ == "__main__":
main()Code Snippets
x = x
work_dir = work_dirsys.exit(2)
sys.exit(0)LOCKFILE_PATH = "/tmp/sec.lck"dte = str(now.day) + "-" + str(now.month) + "-" + str(now.year)
dte1 = str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + "." + str(now.microsecond)datestr = now.strftime('%d-%m-%Y')
timestr = now.strftime('%H:%M:%S')Context
StackExchange Code Review Q#47726, answer score: 6
Revisions (0)
No revisions yet.