예제 #1
0
def write_video(write_path,
                vidreader,
                smoothMasks,
                window_centers,
                window_sizes,
                num_blocks,
                num_prev_frames,
                rsz_shape=[80, 60]):
    create_folder_structure_if_not_exists(write_path)
    shape = [vidreader.width, vidreader.height]
    vidwriter = VideoWriter(write_path, vidreader.width, vidreader.height)
    vidwriter.build()
    vidreader.__reset__()
    skip_frames = num_prev_frames + num_blocks / 2
    vidreader.skip_frames(skip_frames)
    frame_idx = 0
    numFrames = len(smoothMasks)
    while (frame_idx < numFrames):
        frame = vidreader.read_next()
        if frame is None:
            break
        frame = draw_rect(frame, window_centers[frame_idx], window_sizes,
                          shape)
        name = "test_results/tracking/track{0}.png".format(frame_idx)
        cv2.imwrite(name, frame)
        vidwriter.write(np.uint8(frame))
        frame_idx += 1
    vidwriter.close()
예제 #2
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]"
def write_video(vidreader, out_path, num_prev_frames, num_blocks, smoothMasks,
                vaMasks):
    create_folder_structure_if_not_exists(out_path)
    start = time.time()
    vidwriter = VideoWriter(out_path, vidreader.width * 2, vidreader.height)
    vidwriter.build()
    vidreader.__reset__()
    N = vidreader.frames
    vidreader.skip_frames(num_prev_frames + num_blocks / 2)
    frame_idx = 0
    numFrames = len(smoothMasks)
    while (frame_idx < numFrames):
        print 'Writing video ... {0}%\r'.format((frame_idx * 100 / N)),
        frame = vidreader.read_next()
        zero_frame = np.zeros(frame.shape, dtype=np.uint8)
        zero_frame[:, :, 2] = vaMasks[frame_idx] * 255
        out_frame1 = cv2.addWeighted(frame, 0.6, zero_frame, 0.4, 0)
        out_frame2 = frame * smoothMasks[frame_idx][:, :, None]
        name = "test_results/smoothening/{0}_smooth.png".format(
            frame_idx + num_prev_frames + num_blocks / 2)
        cv2.imwrite(name, out_frame2)
        vidwriter.write(np.uint8(np.hstack((out_frame1, out_frame2))))
        frame_idx += 1
    vidwriter.close()
    time_taken = time.time() - start
    print "Writing video .... [DONE] in ", time_taken, " seconds"
예제 #4
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();
예제 #5
0
def write_video(write_path,vidreader,smoothMasks,window_centers,window_sizes,num_blocks,
					num_prev_frames,rsz_shape=[80,60]):
	create_folder_structure_if_not_exists(write_path);
	shape = [vidreader.width, vidreader.height];
	vidwriter = VideoWriter(write_path,vidreader.width,vidreader.height);
	vidwriter.build();	vidreader.__reset__();	
	skip_frames = num_prev_frames + num_blocks/2; vidreader.skip_frames(skip_frames);
	frame_idx = 0; numFrames= len(smoothMasks); 	
	while(frame_idx < numFrames):
		frame = vidreader.read_next();
		if frame is None:
			break;
		frame = draw_rect(frame,window_centers[frame_idx],window_sizes,shape);
		name = "test_results/tracking/track{0}.png".format(frame_idx);
		cv2.imwrite(name,frame);
		vidwriter.write(np.uint8(frame))
		frame_idx += 1;
	vidwriter.close();	
예제 #6
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()
def write_video(vidreader,out_path,num_prev_frames,num_blocks,smoothMasks,vaMasks):
	create_folder_structure_if_not_exists(out_path);
	start = time.time(); 
	vidwriter = VideoWriter(out_path,vidreader.width*2,vidreader.height)
	vidwriter.build();	vidreader.__reset__();	N = vidreader.frames;
	vidreader.skip_frames(num_prev_frames + num_blocks/2); frame_idx = 0	
	numFrames= len(smoothMasks);
	while(frame_idx < numFrames):
		print 'Writing video ... {0}%\r'.format((frame_idx*100/N)),
		frame = vidreader.read_next();
		zero_frame = np.zeros(frame.shape,dtype=np.uint8);
		zero_frame[:,:,2] = vaMasks[frame_idx]*255
		out_frame1 = cv2.addWeighted(frame,0.6,zero_frame,0.4,0);
		out_frame2 = frame * smoothMasks[frame_idx][:,:,None]
		name = "test_results/smoothening/{0}_smooth.png".format(frame_idx+num_prev_frames + num_blocks/2);
		cv2.imwrite(name,out_frame2)
		vidwriter.write(np.uint8(np.hstack((out_frame1,out_frame2))))
		frame_idx += 1;
	vidwriter.close();
	time_taken = time.time()-start;	
	print "Writing video .... [DONE] in ",time_taken," seconds"
예제 #8
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;