Ejemplo n.º 1
0
def createVideo(datapath,group,outfileName,shape,duration=120,framerate=20,vidLen=32):
	vidOutFileName = outfileName + 'test.avi'
	labelOutFileName = outfileName + 'test.txt'
	values = [];
	create_folder_structure_if_not_exists(vidOutFileName);
	with open(datapath+os.sep+group+".txt",'r') as fp:
		for line in fp:
			values.extend([line.split()]);
	numClass = len(values);
	numExamples = (duration*framerate)/(vidLen);
	randomNumbers = np.random.random_integers(0,numClass-1,numExamples);
	fileWriter = open(labelOutFileName,'w');
	vidWriter = VideoWriter(vidOutFileName,shape[0],shape[1]);
	vidWriter.build();
	for idx,random_idx in enumerate(randomNumbers):
		print 'Creating UCF 50 video ... {0}%\r'.format((idx*100/numExamples)),
		classDetails = values[random_idx];
		classPath = datapath + os.sep + classDetails[0] + os.sep
		classExamples = [os.path.join(classPath, _file) for _file in os.listdir(classPath)
			if os.path.isfile(os.path.join(classPath, _file))];
		chosenExample = classExamples[np.random.randint(len(classExamples))];
		#print chosenExample
		vidreader = VideoReader(chosenExample,None);
		num_frames = vidreader.frames;
		cnt,frames = vidreader.read(np.random.randint(num_frames-vidLen),vidLen);
		fileWriter.writelines("%d\n" % int(item) for item in [classDetails[2]]*cnt) 
		for frame in frames:
			vidWriter.write(frame);
	vidWriter.close();	
	fileWriter.close();
Ejemplo n.º 2
0
	def __init__(self,vidfile,labelfile,modelFile,labelListFile,exportPath,
							perClassFrames=40,frameRate=20,context_size=5,
							banerWidth = 80,scale = 1):
		# input properties
		self.vidreader = VideoReader(vidfile);
		self.labelreader = open(labelfile,'r');
		self.N = self.vidreader.frames;
		self.width,self.height = self.vidreader.width,self.vidreader.height;
		self.context_size=context_size;
		self.perClassFrames = perClassFrames;
		self.labels = load_labels(labelListFile);
		self.n_outs = len(self.labels);
		self.flag_colors = [];
		for index in range(self.n_outs):
			self.flag_colors.extend([tuple(np.array(cm.jet(index/float(self.n_outs))[:3][::-1])*255)])
		self.predictors,self.predictorStartIdx = load_predictors(modelFile);
		self.input_shape = [self.height,self.width,3]
		self.batch_size = self.predictors[0].batch_size
		
		#write properites
		self.banerWidth = banerWidth
		self.vidWriter = VideoWriter(exportPath,self.banerWidth+self.width,self.height,fps=frameRate);
		self.colors = np.random.randint(256, size=(len(self.labels), 3))
		self.scale = scale;
		
		#status
		self.frameIdx = 0;
		self.tasks = deque();
		self.isFinished = False;
		self.vidWriter.build();
Ejemplo n.º 3
0
def test_bgsub_mog(inp):
    vidreader = VideoReader(inp)
    vidwriter = VideoWriter("test_results/bg_sub_mog.avi", vidreader.width,
                            vidreader.height)
    bgsub = get_instance(BGMethods.MOG_SUBSTRACTION)
    start = time.time()
    process_video(bgsub, vidreader, vidwriter)
    time_taken = time.time() - start
    print "Tested Background Subtraction (Mixture of Gaussian)...    [DONE] in " + str(
        time_taken) + " seconds"
Ejemplo n.º 4
0
def test_bgsub_fd(inp):
    vidreader = VideoReader(inp)
    vidwriter = VideoWriter("test_results/bg_sub_fd.avi", vidreader.width,
                            vidreader.height)
    bgsub = get_instance(BGMethods.FRAME_DIFFERENCING)
    start = time.time()
    process_video(bgsub, vidreader, vidwriter, num_blocks=2)
    time_taken = time.time() - start
    print "Tested Background Subtraction (Frame Differencing)...    [DONE] in " + str(
        time_taken) + " seconds"
Ejemplo n.º 5
0
def createVideo(datapath,
                group,
                outfileName,
                shape,
                duration=120,
                framerate=20,
                vidLen=32):
    vidOutFileName = outfileName + 'test.avi'
    labelOutFileName = outfileName + 'test.txt'
    values = []
    create_folder_structure_if_not_exists(vidOutFileName)
    with open(datapath + os.sep + group + ".txt", 'r') as fp:
        for line in fp:
            values.extend([line.split()])
    numClass = len(values)
    numExamples = (duration * framerate) / (vidLen)
    randomNumbers = np.random.random_integers(0, numClass - 1, numExamples)
    fileWriter = open(labelOutFileName, 'w')
    vidWriter = VideoWriter(vidOutFileName, shape[0], shape[1])
    vidWriter.build()
    for idx, random_idx in enumerate(randomNumbers):
        print 'Creating UCF 50 video ... {0}%\r'.format(
            (idx * 100 / numExamples)),
        classDetails = values[random_idx]
        classPath = datapath + os.sep + classDetails[0] + os.sep
        classExamples = [
            os.path.join(classPath, _file) for _file in os.listdir(classPath)
            if os.path.isfile(os.path.join(classPath, _file))
        ]
        chosenExample = classExamples[np.random.randint(len(classExamples))]
        #print chosenExample
        vidreader = VideoReader(chosenExample, None)
        num_frames = vidreader.frames
        cnt, frames = vidreader.read(np.random.randint(num_frames - vidLen),
                                     vidLen)
        fileWriter.writelines("%d\n" % int(item)
                              for item in [classDetails[2]] * cnt)
        for frame in frames:
            vidWriter.write(frame)
    vidWriter.close()
    fileWriter.close()
Ejemplo n.º 6
0
def tracker(inPath,outPath="test_results/tracker.avi"):
	#initialization for default value
	hog = cv2.HOGDescriptor()
	hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector())
	vidreader = VideoReader(inPath)
	vidwriter = VideoWriter(outPath,vidreader.width,vidreader.height)
	vidwriter.build();
	frame_idx =0; N = vidreader.frames;
	while vidreader.num_remaining_frames() > 0:
		frame_idx += 1;
		cur_frame = vidreader.read_next();
		print 'Perform human tracking.... {0}%\r'.format((frame_idx*100/N)),
		if cur_frame is None:
			break;
		gray_cur_frame = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY)
		found, w = hog.detectMultiScale(gray_cur_frame, winStride=(8,8), padding=(32,32), scale=1.05)
		found_filtered = []
		#discarding the bounding box on within other
		for ri, r in enumerate(found):
			for qi, q in enumerate(found):
				if ri != qi and inside(r, q):
					break
				else:
					found_filtered.append(r)
		draw_detections(cur_frame, found_filtered, 1)
		vidwriter.write(cur_frame);
	vidreader.close();
	vidwriter.close()
	cv2.destroyAllWindows()
	print "Implemented Human Tracking.............   [Done]"
Ejemplo n.º 7
0
class UCF50Processor(object):
	def __init__(self,vidfile,labelfile,modelFile,labelListFile,exportPath,
							perClassFrames=40,frameRate=20,context_size=5,
							banerWidth = 80,scale = 1):
		# input properties
		self.vidreader = VideoReader(vidfile);
		self.labelreader = open(labelfile,'r');
		self.N = self.vidreader.frames;
		self.width,self.height = self.vidreader.width,self.vidreader.height;
		self.context_size=context_size;
		self.perClassFrames = perClassFrames;
		self.labels = load_labels(labelListFile);
		self.n_outs = len(self.labels);
		self.flag_colors = [];
		for index in range(self.n_outs):
			self.flag_colors.extend([tuple(np.array(cm.jet(index/float(self.n_outs))[:3][::-1])*255)])
		self.predictors,self.predictorStartIdx = load_predictors(modelFile);
		self.input_shape = [self.height,self.width,3]
		self.batch_size = self.predictors[0].batch_size
		
		#write properites
		self.banerWidth = banerWidth
		self.vidWriter = VideoWriter(exportPath,self.banerWidth+self.width,self.height,fps=frameRate);
		self.colors = np.random.randint(256, size=(len(self.labels), 3))
		self.scale = scale;
		
		#status
		self.frameIdx = 0;
		self.tasks = deque();
		self.isFinished = False;
		self.vidWriter.build();
	
	def __design_frame_banner__(self,frame,_score,_label,top=3):
		if not self.scale == 1:
			frame = cv2.resize(frame,None,fx=self.scale, fy=self.scale, interpolation = cv2.INTER_CUBIC)
		if frame.ndim == 2:
			frame = np.dstack((frame,frame,frame));
		assert(frame.ndim==3),"given frame not in shape"
		baner_frame = np.zeros((self.height,self.banerWidth,3));
		_indices = np.argsort(_score)[::-1];
		col = 5; row = 8; steps = ((self.height-30)/(top+1))-5;
		small_fface = cv2.FONT_HERSHEY_DUPLEX;
		__draw_str__(baner_frame,(col+3,row),">PREDICTION<",color=(255,255,255),fontsize=0.25,fontface=small_fface); row += steps
		for pos,classLbl in enumerate(_indices[:top]):
			_str = "{0}. {1}".format(pos+1,self.labels[classLbl]);
			__draw_str__(baner_frame,(col,row),_str,color=self.colors[classLbl],fontsize=0.25); row += steps
		__draw_str__(baner_frame,(col+3,row),">ACTUAL<",color=(255,255,255),fontsize=0.25,fontface=small_fface); row += steps
		if not _label is None:
			_str = "{0}".format(self.labels[_label]); 
			__draw_str__(baner_frame,(col,row),_str,color=self.colors[_label],fontsize=0.25);
			rank = list(_indices).index(_label); _str = "Rank : {0}".format(rank+1); row += steps;
			__draw_str__(baner_frame,(col+3,row),_str,color = (255,255,255),fontsize=0.25);
			cv2.rectangle(baner_frame,(8,self.height-12),(self.banerWidth-8,self.height-3),(255,255,255),1);
			cv2.rectangle(baner_frame,(10,self.height-10),(self.banerWidth-10,self.height-5),self.flag_colors[rank],-1);
		return np.hstack((baner_frame,frame));	
		
	def __readNextFrame__(self):
		if self.vidreader.has_next():
			frames = []; label = -1;
			for idx in range(self.context_size):
				frame = self.vidreader.read_next();
				label = int(self.labelreader.readline()); 
				frames.extend([frame]); 
			return (frames,label);
		else:
			return (None,-1);
	
	def process(self):
		blockReader = BlockReader(self.batch_size,self.input_shape,self.__readNextFrame__);
		#pool = ThreadPool(processes = 2);
		#task = pool.apply_async(self.__videoWriter__);
		p_frames_cnt = 0
		while not blockReader.isFinished:
			(frameCnt,frames,labels)=blockReader.readNextBlock();
			#print "READ A BLOCK...", len(self.tasks)
			if frameCnt > 0:
				processedWindowCenter = self.__process_block__(frames[:frameCnt]);
				vidTask = BlockTask(frames,processedWindowCenter,labels,frameCnt)
				#self.tasks.append(vidTask);
				vidTask.process(self.vidWriter,self.predictors,self.__design_frame_banner__,self.predictorStartIdx);
		self.isFinished = True;	
		
	
	def __process_block__(self,frameBlock):
		windowCenters = [];
		numGroupsPerClass = self.perClassFrames/self.context_size;
		for idx in range(len(frameBlock)/numGroupsPerClass):
			classFrames = [];
			for frameGroup in frameBlock[idx*numGroupsPerClass:(idx+1)*numGroupsPerClass]:
				for frame in frameGroup:
					classFrames.extend([frame]);
			c_windowCenters = processClassFrames(classFrames,self.context_size)
			windowCenters.extend(c_windowCenters);
		#print windowCenters
		return windowCenters;
Ejemplo n.º 8
0
                        type=int)
    parser.add_argument("--write_bgsub",
                        nargs='?',
                        help="Write bgsub default-0",
                        default=0,
                        type=int)
    parser.add_argument("--window",
                        nargs='?',
                        help="window size default-5",
                        default=5,
                        type=int)
    parser.add_argument("--overlap",
                        nargs='?',
                        help="overlap default-2",
                        default=2,
                        type=int)

    args = parser.parse_args()
    inp = args.input
    out = args.output
    vidreader = VideoReader(inp)
    sal = sal_instance(args.sal, SaliencyProps())
    bg = get_instance(args.bg)
    smoothner = smooth_instance(feats, args.smoothner)
    start = time.time()
    process(vidreader, sal, bg, smoothner, args.num_prev_frames,
            args.num_blocks, args.write, args.extract, args.write_gray == 1,
            args.write_bgsub == 1, args.window, args.overlap, args.rsz_shape)
    print "Event Tracker ...", inp, "[DONE] in", (time.time() -
                                                  start), "seconds"