Guest User

cam tcp mode

a guest
Jan 10th, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import sys
  4. import base64
  5. import urllib2
  6. import time
  7.  
  8.  
  9.  
  10.  
  11. class ipCamera(object):
  12.  
  13.     def __init__(self, url, user=None, password=None):
  14.         self.url = url
  15.         auth_encoded = base64.encodestring('%s:%s' % (user, password))[:-1]
  16.  
  17.         self.req = urllib2.Request(self.url)
  18.         if not user==None:
  19.             self.req.add_header('Authorization', 'Basic %s' % auth_encoded)
  20.  
  21.     def read(self):
  22.         try:
  23.             response = urllib2.urlopen(self.req)
  24.             data = response.read()
  25.             if data.find("Disconnect the other client and Take Over") > -1:
  26.                 raise Exception("Disconnect the other client and Take Over")
  27.             img_array = np.asarray(bytearray(data), dtype=np.uint8)
  28.             frame = cv2.imdecode(img_array, 1)
  29.             return [True, frame]
  30.         except Exception as e:
  31.             print self.req.get_full_url()
  32.             print str(e)
  33.             if str(e) == "Disconnect the other client and Take Over":
  34.                 sys.exit(0)
  35.             return [False, None]
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. blue  = (255,   0,   0)
  43. green = (0  , 255,   0)
  44. red   = (0  ,   0, 255)
  45.  
  46. white = (255, 255, 255)
  47. yellow = (0, 255, 255)
  48.  
  49.  
  50.  
  51.  
  52. ret = True
  53. delay=40
  54.  
  55.  
  56.  
  57. vidsrc=sys.argv[1]
  58. vidsrc=0
  59. #vidsrc='http://192.168.137.226:8081/video'
  60. vidsrc='http://192.168.137.226:8081/shot.jpg'
  61. #vidsrc='http://192.168.137.226:8080/mjpegfeed?dummy=mjpeg'
  62. #vidsrc='http://localhost:8000/fullbody1.jpg'
  63. #delay=1
  64.  
  65.  
  66.  
  67. #cap = cv2.VideoCapture(vidsrc)
  68. #print "testing open status: ", cap.isOpened()
  69. cap = ipCamera(vidsrc)
  70.  
  71.  
  72. faceCascade = cv2.CascadeClassifier(sys.argv[2])
  73.  
  74.  
  75.  
  76. #if vidsrc == 0:
  77. #    delay=1
  78. i=0
  79.  
  80. while ret:
  81.     time.sleep(int(sys.argv[3]))
  82.     ret, frame = cap.read()
  83.     print "\ngot frame " + str(i) + "\n"
  84.     #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  85.     gray = frame
  86.  
  87.  
  88.     faces = faceCascade.detectMultiScale(
  89.         gray,
  90.         scaleFactor=1.1,
  91.         minNeighbors=5,
  92.         minSize=(30, 30),
  93.         flags=cv2.cv.CV_HAAR_SCALE_IMAGE
  94.     )
  95.  
  96.  
  97.     facelist = [list(face) for face in faces]
  98.     rect_color = red
  99.     for face in facelist:
  100.         (x, y, w, h) = face
  101.         cv2.rectangle(frame, (x, y), (x+w, y+h), rect_color, 2)
  102.  
  103.  
  104.  
  105.     i = i + 1
  106.     # cv2.imwrite("frames/frame-" + str(i) + ".png", frame)
  107.  
  108.  
  109.  
  110.     key1 = key2 = 'placeholder'
  111.     cv2.imshow("myvideo", frame)
  112.     # cap.set(CV_CAP_PROP_FPS, 25)
  113.     key1 = cv2.waitKey(delay) & 0xFF
  114.     # cv2.imshow("mygrayvideo", gray)
  115.     # key2 = cv2.waitKey(delay) & 0xFF
  116.     if key1 == ord('w') or key2 == ord('w'):
  117.         delay = max(1, delay-1)
  118.         print delay, "milliseconds per frame"
  119.     if key1 == ord('s') or key2 == ord('s'):
  120.         delay = delay + 1
  121.         print delay, "milliseconds per frame"
  122.     if key1 == ord('q') or key2 == ord('q'):
  123.         break
  124.  
  125.  
  126. # cap.release()
  127. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment