python 视频处理,提取视频相关帧,读取Excel

  1. import cv2.cv as cv  
  2. import os  
  3. import cv2  
  4. import numpy  
  5. import Image  
  6. import xlrd  
  7. class videoReader:  
  8.     frame_count=0  
  9.     def __init__(self,videoPath):  
  10.         self.videoPath=videoPath  
  11.     def video_init(self):  
  12.         self.capture = cv.CaptureFromFile(self.videoPath)  
  13.         self.win_name = “test”  
  14.         cv.NamedWindow(self.win_name, cv.CV_WINDOW_AUTOSIZE)  
  15.     def read(self):  
  16.         self.video_init()  
  17.         while 1:  
  18.             self.frame_count+=1  
  19.             image = cv.QueryFrame(self.capture )  
  20.             cv.ShowImage(self.win_name, image)  
  21.             print self.frame_count  
  22.             c = cv.WaitKey(10)  
  23.             if c == 27:  
  24.                 break  
  25.         cv.DestroyWindow(self.win_name)  
  26. class videoFramesExtractor(videoReader):  
  27.     save_path=‘d:/’  
  28.     def __init__(self,videoPath,frameSpan,image_prefix):#image_prefix=event_name  
  29.         videoReader.__init__(self,videoPath)  
  30.         self.frameSpan=frameSpan  
  31.         self.image_prefix=image_prefix  
  32.     def read(self):  
  33.         capture = cv2.VideoCapture(self.videoPath)  
  34.         win_name = “test”  
  35.         cv.NamedWindow(win_name, cv.CV_WINDOW_AUTOSIZE)
  36.         success,frame = capture.read()
  37.         init_row=0  
  38.         while success:  
  39.             self.frame_count+=1  
  40.             success,frame = capture.read()
  41.             #cv2.imshow(win_name,frame) #显示照片浪费时间  
  42.             time_duration=self.frameSpan[init_row][1]-self.frameSpan[init_row][0]+1  
  43.             if self.frame_count>=self.frameSpan[init_row][0] and \  
  44.               self.frame_count<=self.frameSpan[init_row][1] :  
  45.                 self.image_save(frame,init_row,time_duration)  
  46.                 if self.frame_count==self.frameSpan[init_row][1]:  
  47.                     init_row+=1  
  48.                 if init_row==len(self.frameSpan):  
  49.                     cv.DestroyWindow(win_name)
  50.                     return 0  
  51.             c = cv.WaitKey(10)  
  52.             if c == 27:  
  53.                 break  
  54.         cv.DestroyWindow(win_name)
  55.     def image_save(self,frame,init_row,time_duration):  
  56.         save_path=self.make_folder(init_row,time_duration)  
  57.         #frame=Image.fromarray(frame)  
  58.         #frame.resize((300,300))  
  59.         #frame.save(‘%s//%s.jpg’ % (save_path,self.frame_count))  
  60.         frame=cv2.resize(frame,(300,300))#  
  61.         cv2.imwrite( ‘%s//%s.jpg’ % (save_path,self.frame_count), frame)  
  62.     def make_folder(self,init_row,time_duration):  
  63.         temp= self.image_prefix+‘_’+str(init_row)+’_’+str(time_duration)#事件名称_事件顺序_  
  64.         new_path = os.path.join(self.save_path,temp)  
  65.         if not os.path.isdir(new_path):  
  66.             os.makedirs(new_path)
  67.         return new_path  
  68. def read_excel_single(excel_path,event_instance):  
  69.         data=[]
  70.         data=xlrd.open_workbook(excel_path)
  71.         #read the first sheets  
  72.         table = data.sheets()[0]  
  73.         #read the num fo cols  
  74.         nrows = table.nrows
  75.         rowmsg=[]#用来存储帧的范围,及事件的类别,描述。  
  76.         for i in xrange(nrows):  
  77.             start_frame,end_frame=table.row_values(i)[1].split(‘:’)  
  78.             rowmsg.append([int(start_frame),int(end_frame),table.row_values(i)[3]])  
  79.         rowmsg.sort(lambda x,y:cmp(x[0],y[0]))#按照数字大小排序~  
  80.         event_id=[]
  81.         event_id=event_instance
  82.         #print event_id  
  83.         event=[]
  84.         for item in xrange(len(rowmsg)):  
  85.             if rowmsg[item][2]==event_id:#  
  86.                 event.append(rowmsg[item])
  87.         if event==[]:  
  88.             print (“there is no %s occurs” % event_instance)  
  89.             return 0  
  90.         #去除重复  
  91.         cur_pos=1  
  92.         new_rowmsg=[]
  93.         pre_s=event[0][0]  
  94.         pre_e=event[0][1]  
  95.         cur_s=event[cur_pos][0]  
  96.         cur_e=event[cur_pos][1]  
  97.         while cur_pos<len(event)-1:   
  98.             while not (cur_s>pre_e):  
  99.                 if cur_s>pre_s:  
  100.                     cur_s=pre_s
  101.                 if cur_e<pre_e:  
  102.                     cur_e=pre_e
  103.                 still_pos=[cur_s,cur_e,event_id]
  104.                 cur_pos+=1  
  105.                 if cur_pos==len(event)-1:  
  106.                     break  
  107.                 pre_s=cur_s
  108.                 pre_e=cur_e
  109.                 cur_s=event[cur_pos][0]  
  110.                 cur_e=event[cur_pos][1]  
  111.             new_rowmsg.append(still_pos)
  112.             if cur_pos==len(event)-1:  
  113.                 break  
  114.             cur_pos+=1  
  115.             pre_s=cur_s
  116.             pre_e=cur_e
  117.             cur_s=event[cur_pos][0]  
  118.             cur_e=event[cur_pos][1]  
  119.         return new_rowmsg  
  120. excel_source=‘E:/08ann/dev_20071101/LGW_20071101_E1_CAM1.mpeg.xlsx’  
  121. event_instance=‘CellToEar’  
  122. row_msg=read_excel_single(excel_source,event_instance)
  123. new_row=[ item[0:2] for item in row_msg ]   
  124. filename = “G:/TrecvidData/08/DEV/LGW_20071101_E1_CAM1.mpg”  
  125. videoread=videoFramesExtractor(filename,new_row,‘CellToEar’)  
  126. videoread.read()

发表评论