Skip to content
- import cv2.cv as cv
- import os
- import cv2
- import numpy
- import Image
- import xlrd
- class videoReader:
- frame_count=0
- def __init__(self,videoPath):
- self.videoPath=videoPath
- def video_init(self):
- self.capture = cv.CaptureFromFile(self.videoPath)
- self.win_name = “test”
- cv.NamedWindow(self.win_name, cv.CV_WINDOW_AUTOSIZE)
- def read(self):
- self.video_init()
- while 1:
- self.frame_count+=1
- image = cv.QueryFrame(self.capture )
- cv.ShowImage(self.win_name, image)
- print self.frame_count
- c = cv.WaitKey(10)
- if c == 27:
- break
- cv.DestroyWindow(self.win_name)
- class videoFramesExtractor(videoReader):
- save_path=‘d:/’
- def __init__(self,videoPath,frameSpan,image_prefix):
- videoReader.__init__(self,videoPath)
- self.frameSpan=frameSpan
- self.image_prefix=image_prefix
- def read(self):
- capture = cv2.VideoCapture(self.videoPath)
- win_name = “test”
- cv.NamedWindow(win_name, cv.CV_WINDOW_AUTOSIZE)
- success,frame = capture.read()
- init_row=0
- while success:
- self.frame_count+=1
- success,frame = capture.read()
-
- time_duration=self.frameSpan[init_row][1]-self.frameSpan[init_row][0]+1
- if self.frame_count>=self.frameSpan[init_row][0] and \
- self.frame_count<=self.frameSpan[init_row][1] :
- self.image_save(frame,init_row,time_duration)
- if self.frame_count==self.frameSpan[init_row][1]:
- init_row+=1
- if init_row==len(self.frameSpan):
- cv.DestroyWindow(win_name)
- return 0
- c = cv.WaitKey(10)
- if c == 27:
- break
- cv.DestroyWindow(win_name)
- def image_save(self,frame,init_row,time_duration):
- save_path=self.make_folder(init_row,time_duration)
-
-
-
- frame=cv2.resize(frame,(300,300))
- cv2.imwrite( ‘%s//%s.jpg’ % (save_path,self.frame_count), frame)
- def make_folder(self,init_row,time_duration):
- temp= self.image_prefix+‘_’+str(init_row)+’_’+str(time_duration)
- new_path = os.path.join(self.save_path,temp)
- if not os.path.isdir(new_path):
- os.makedirs(new_path)
- return new_path
- def read_excel_single(excel_path,event_instance):
- data=[]
- data=xlrd.open_workbook(excel_path)
-
- table = data.sheets()[0]
-
- nrows = table.nrows
- rowmsg=[]
- for i in xrange(nrows):
- start_frame,end_frame=table.row_values(i)[1].split(‘:’)
- rowmsg.append([int(start_frame),int(end_frame),table.row_values(i)[3]])
- rowmsg.sort(lambda x,y:cmp(x[0],y[0]))
- event_id=[]
- event_id=event_instance
-
- event=[]
- for item in xrange(len(rowmsg)):
- if rowmsg[item][2]==event_id:
- event.append(rowmsg[item])
- if event==[]:
- print (“there is no %s occurs” % event_instance)
- return 0
-
- cur_pos=1
- new_rowmsg=[]
- pre_s=event[0][0]
- pre_e=event[0][1]
- cur_s=event[cur_pos][0]
- cur_e=event[cur_pos][1]
- while cur_pos<len(event)-1:
- while not (cur_s>pre_e):
- if cur_s>pre_s:
- cur_s=pre_s
- if cur_e<pre_e:
- cur_e=pre_e
- still_pos=[cur_s,cur_e,event_id]
- cur_pos+=1
- if cur_pos==len(event)-1:
- break
- pre_s=cur_s
- pre_e=cur_e
- cur_s=event[cur_pos][0]
- cur_e=event[cur_pos][1]
- new_rowmsg.append(still_pos)
- if cur_pos==len(event)-1:
- break
- cur_pos+=1
- pre_s=cur_s
- pre_e=cur_e
- cur_s=event[cur_pos][0]
- cur_e=event[cur_pos][1]
- return new_rowmsg
- excel_source=‘E:/08ann/dev_20071101/LGW_20071101_E1_CAM1.mpeg.xlsx’
- event_instance=‘CellToEar’
- row_msg=read_excel_single(excel_source,event_instance)
- new_row=[ item[0:2] for item in row_msg ]
- filename = “G:/TrecvidData/08/DEV/LGW_20071101_E1_CAM1.mpg”
- videoread=videoFramesExtractor(filename,new_row,‘CellToEar’)
- videoread.read()