Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to read IP Cam in cv2.VideoSource

HI Team,

I have standalone program which does face detection and stored faces in dir. Now this program does works well with webcam in it when i try to give me IP Cam URL to read response which coming as mjpeg programs does not do anything after this line

self.videosource =cv2.VideoCapture(np.asarray(bytearray(response.read()), dtype=np.uint8))

So I am not sure what is wrong with code and why stops after this line.

import base64
import time
import urllib2
import cv2
import numpy as np
import argparse
import os
from cStringIO import StringIO

DEFAULT_OP_PATH = 'FaceCaps\\'
DEFAULT_CASCADE_IP_PATH = 'haarcascade-frontalface-alt.xml'


# This is class which gets URL from IP camera and it reads url response put it into byte array of numpy


class VideoCap:
    def __init__(self):
        response = urllib2.urlopen("http://192.168.0.153:5000/video_feed")
        self.videosource =cv2.VideoCapture(np.asarray(bytearray(response.read()), dtype=np.uint8))
        self.count = 0
        self.argobj = Parse()
        self.facecascade = cv2.CascadeClassifier(self.argobj.input_path)

     def CaptureFrames(self):

        while True:

            #Create Frame Number for each frame we are assigning the 08d with number which is there in self.count
            framenumber = '%08d' % (self.count)

            #Capture frame by Frame
            ret, frame =videosource.read()

            #set screen color to gray so harr cascade can detect edges and faces
            screencolor = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

            #customize how cascade detect your face
            faces = self.facecascade.detectMultiScale(screencolor,scaleFactor = 1.1,minNeighbors=5,minSize = (30,30),flags = cv2.CASCADE_SCALE_IMAGE)

            #Display resulting frame.
            cv2.imshow('Spying on you',screencolor)

            # If face length is 0 then it indicate that no faces detected
            if len(faces) == 0:
                pass
            #If faces is detected then it will return 1 or more than 1 depend upon faces
            elif len(faces) > 0:
                print('Face Detected')

                #Graph face and draw rectangle arround it.
                for (x,y,w,h) in faces:
                    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
                    sub_face= frame[x:x+w,y:y+h]
                    cv2.imwrite(DEFAULT_OP_PATH + framenumber + '.jpg',sub_face)

            #Increment count so we get uniquename for each frame
            self.count +=1

            if cv2.waitKey(1) == 27:
                break

        #When we are done capture we will close the release web connection
        self.videosource.release()
        cv2.waitKey(500)
        cv2.destroyAllWindows()
        cv2.waitKey(500)


def Parse():
    parser = argparse.ArgumentParser(description='Cascade path for face detection')
    parser.add_argument('-i','--input_path',type=str,default = DEFAULT_CASCADE_IP_PATH,help='Cascade input path')
    parser.add_argument('-o','--output_path',type=str,default = DEFAULT_OP_PATH , help = 'Output path for pics taken')
    args = parser.parse_args()
    return args

def ClearImageFolder():
    if not(os.path.exists(DEFAULT_OP_PATH)):
        os.makedirs(DEFAULT_OP_PATH)
    else:
        for files in os.listdir(DEFAULT_OP_PATH):
            filepath = os.path.join(DEFAULT_OP_PATH,files)
            if os.path.isfile(filepath):
                os.unlink(filepath)
            else:
                continue

def main():
    ClearImageFolder()

    #instaniate class object
    faceDetect = VideoCap()

    #Call Capture frames from class to begin face detection
    faceDetect.CaptureFrames()

if __name__ == '__main__':
    main()

Your help and time are most appreciated.