Python在視訊處理上的優勢有哪些
阿新 • • 發佈:2019-02-10
今天與大家分享一些Python處理視訊的一下經驗,視訊的處理和圖片的處理類似,只不過視訊處理需要連續處理一系列圖片。
一共這幾個模組:
class videoReader 讀取視訊
class videoFramesExtractor(videoReader):繼承了讀取視訊,主要是用來限制讀取視訊中的哪些幀,並儲存。
read_excel_single(excel_path,event_instance): 處理Excel中的陣列,找到事件的幀數範圍:
Excel內容是這樣的,需要的只是第二列的幀範圍,另外第二列中的這些值還有重複的所以需要去除重複:
每一個範圍,例如:23450:23461之內的幀要存放在一個資料夾裡。
- </pre><pre name="code" class="python"># -*- coding: cp936 -*-
- 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):#image_prefix=event_name
- 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()
- #cv2.imshow(win_name,frame) #顯示照片浪費時間
- 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=Image.fromarray(frame)
- #frame.resize((300,300))
- #frame.save('%s//%s.jpg' % (save_path,self.frame_count))
- 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)
- #read the first sheets
- table = data.sheets()[0]
- #read the num fo cols
- 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
- #print event_id
- 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()
有幾點發現:
cv2.的函式輸出的資料型別基本都是numpy,而cv.不行。所以儘量在Python中使用cv2.的函式。
通過numpy轉換後的圖片會在顏色上有些失真,不過影響不大,如果是灰度圖的話則完全沒有影響。