当前位置:   article > 正文

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

excel 读取 视频 frame

一共这几个模块:

class videoReader 读取视频

class videoFramesExtractor(videoReader):继承了读取视频,主要是用来限制读取视频中的哪些帧,并保存。

read_excel_single(excel_path,event_instance): 处理Excel中的数组,找到事件的帧数范围:

Excel内容是这样的,需要的只是第二列的帧范围,另外第二列中的这些值还有重复的所以需要去除重复:

每一个范围,例如:23450:23461之内的帧要存放在一个文件夹里。

 

 

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



 

 

有几点发现:

cv2.的函数输出的数据类型基本都是numpy,而cv.不行。所以尽量在Python中使用cv2.的函数。

通过numpy转换后的图片会在颜色上有些失真,不过影响不大,如果是灰度图的话则完全没有影响。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/871132
推荐阅读
相关标签
  

闽ICP备14008679号