Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- import sys
- import base64
- import urllib2
- import time
- class ipCamera(object):
- def __init__(self, url, user=None, password=None):
- self.url = url
- auth_encoded = base64.encodestring('%s:%s' % (user, password))[:-1]
- self.req = urllib2.Request(self.url)
- if not user==None:
- self.req.add_header('Authorization', 'Basic %s' % auth_encoded)
- def read(self):
- try:
- response = urllib2.urlopen(self.req)
- data = response.read()
- if data.find("Disconnect the other client and Take Over") > -1:
- raise Exception("Disconnect the other client and Take Over")
- img_array = np.asarray(bytearray(data), dtype=np.uint8)
- frame = cv2.imdecode(img_array, 1)
- return [True, frame]
- except Exception as e:
- print self.req.get_full_url()
- print str(e)
- if str(e) == "Disconnect the other client and Take Over":
- sys.exit(0)
- return [False, None]
- blue = (255, 0, 0)
- green = (0 , 255, 0)
- red = (0 , 0, 255)
- white = (255, 255, 255)
- yellow = (0, 255, 255)
- ret = True
- delay=40
- vidsrc=sys.argv[1]
- vidsrc=0
- #vidsrc='http://192.168.137.226:8081/video'
- vidsrc='http://192.168.137.226:8081/shot.jpg'
- #vidsrc='http://192.168.137.226:8080/mjpegfeed?dummy=mjpeg'
- #vidsrc='http://localhost:8000/fullbody1.jpg'
- #delay=1
- #cap = cv2.VideoCapture(vidsrc)
- #print "testing open status: ", cap.isOpened()
- cap = ipCamera(vidsrc)
- faceCascade = cv2.CascadeClassifier(sys.argv[2])
- #if vidsrc == 0:
- # delay=1
- i=0
- while ret:
- time.sleep(int(sys.argv[3]))
- ret, frame = cap.read()
- print "\ngot frame " + str(i) + "\n"
- #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- gray = frame
- faces = faceCascade.detectMultiScale(
- gray,
- scaleFactor=1.1,
- minNeighbors=5,
- minSize=(30, 30),
- flags=cv2.cv.CV_HAAR_SCALE_IMAGE
- )
- facelist = [list(face) for face in faces]
- rect_color = red
- for face in facelist:
- (x, y, w, h) = face
- cv2.rectangle(frame, (x, y), (x+w, y+h), rect_color, 2)
- i = i + 1
- # cv2.imwrite("frames/frame-" + str(i) + ".png", frame)
- key1 = key2 = 'placeholder'
- cv2.imshow("myvideo", frame)
- # cap.set(CV_CAP_PROP_FPS, 25)
- key1 = cv2.waitKey(delay) & 0xFF
- # cv2.imshow("mygrayvideo", gray)
- # key2 = cv2.waitKey(delay) & 0xFF
- if key1 == ord('w') or key2 == ord('w'):
- delay = max(1, delay-1)
- print delay, "milliseconds per frame"
- if key1 == ord('s') or key2 == ord('s'):
- delay = delay + 1
- print delay, "milliseconds per frame"
- if key1 == ord('q') or key2 == ord('q'):
- break
- # cap.release()
- cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment