def write_labels(self, content): """ Content is in form of a list [start, end, name] Where start and end can be given in seconds or as a string """ #If we are writing for the first time, then we open the file in write mode so that previous contents are erased if self.write_count == 0: f = open(self.newfile, 'w') else: f = open(self.newfile, 'a') start = content[0] end = content[1] name = content[2] line = "" if type(start) == type(end) == int: line = timeFunc.get_time_string( start) + " - " + timeFunc.get_time_string(end) + " = " + name elif type(start) == type(end) == str: line = start + " - " + end + " = " + name line += '\n' f.write(line) f.close() self.write_count += 1
def add(request): """ The url /output/add/ maps to this function. The function is called by the click of the button '+'. It is used to add another label. When the function is called, these are the following operations performed. - Obtain the new start time in seconds of the next label - Make the end time of the new label, equal to the end time of the original label(where + was clicked) - Change the end time of the previous label(the label whose + was clicked) to the new start time """ global lines actual_start = int(request.POST.get(u'actual_start')) start = int(request.POST.get(u'start_sec')) end = int(request.POST.get(u'end_sec')) if start in lines.keys(): #If already in the dictionary don't update return HttpResponse(simplejson.dumps({'server_response': '1'})) #Now we add the value in lines as well lines.update({ start: [ timeFunc.get_time_string(start), timeFunc.get_time_string(end), UNCLASSIFIED_CONTENT, start, end ] }) #We change the "end" of the previous start lines[actual_start][1] = timeFunc.get_time_string(start) print len(lines[start]), len(lines[actual_start]) return HttpResponse(simplejson.dumps({'server_response': '1'}))
def write_labels(self, content): """ Content is in form of a list [start, end, name] Where start and end can be given in seconds or as a string """ #If we are writing for the first time, then we open the file in write mode so that previous contents are erased if self.write_count == 0: f = open(self.newfile, 'w') else: f = open(self.newfile, 'a') start = content[0] end = content[1] name = content[2] line = "" if type(start) == type(end) == int: line = timeFunc.get_time_string(start) + " - " + timeFunc.get_time_string(end) + " = " + name elif type(start) == type(end) == str: line = start + " - " + end + " = " + name line += '\n' f.write(line) f.close() self.write_count += 1
def add(request): """ The url /output/add/ maps to this function. The function is called by the click of the button '+'. It is used to add another label. When the function is called, these are the following operations performed. - Obtain the new start time in seconds of the next label - Make the end time of the new label, equal to the end time of the original label(where + was clicked) - Change the end time of the previous label(the label whose + was clicked) to the new start time """ global lines actual_start = int(request.POST.get(u'actual_start')) start = int(request.POST.get(u'start_sec')) end = int(request.POST.get(u'end_sec')) if start in lines.keys(): #If already in the dictionary don't update return HttpResponse(simplejson.dumps({'server_response': '1' })) #Now we add the value in lines as well lines.update({start: [timeFunc.get_time_string(start), timeFunc.get_time_string(end), UNCLASSIFIED_CONTENT, start, end]}) #We change the "end" of the previous start lines[actual_start][1] = timeFunc.get_time_string(start) print len(lines[start]), len(lines[actual_start]) return HttpResponse(simplejson.dumps({'server_response': '1' }))
def delete(request): """ The url /output/delete/ maps to this function. This function is called by the click of the button '-'. It is used to delete the label, intended to be deleted by the user. When a label is deleted the following operations take place: - the end time of the to be deleted label is written onto the end time of the label preceeding it. - the label to be deleted is removed from lines dictionary """ global lines keys = lines.keys() keys.sort() start = int(request.POST.get(u'start_sec')) end = int(request.POST.get(u'end_sec')) #Now we find the preceeding label for i in range(len(keys)): if keys[i] == start: break #This will be the label, just above the label to be deleted old_start = keys[i - 1] #Performing the operations #We assign the endtime of this to the previous start lines[old_start][1] = timeFunc.get_time_string(end) lines[old_start][-1] = end del lines[start] return HttpResponse(simplejson.dumps({'server_response': '1' }))
def delete(request): """ The url /output/delete/ maps to this function. This function is called by the click of the button '-'. It is used to delete the label, intended to be deleted by the user. When a label is deleted the following operations take place: - the end time of the to be deleted label is written onto the end time of the label preceeding it. - the label to be deleted is removed from lines dictionary """ global lines keys = lines.keys() keys.sort() start = int(request.POST.get(u'start_sec')) end = int(request.POST.get(u'end_sec')) #Now we find the preceeding label for i in range(len(keys)): if keys[i] == start: break #This will be the label, just above the label to be deleted old_start = keys[i - 1] #Performing the operations #We assign the endtime of this to the previous start lines[old_start][1] = timeFunc.get_time_string(end) lines[old_start][-1] = end del lines[start] return HttpResponse(simplejson.dumps({'server_response': '1'}))
def recognize_ads_file(self, input_file_path): """ Function: Recognize all the commercials present in the file 1) Convert input_file to wav file 2) Process the file in chunks 3) Skip parts based on the offset and duration of recognize_chunk 4) Append [start_time, end_time, name] of each confident segment Input: input_file_path: Either video or audio file whose ads have to be detected. Returns: List of [start_time, end_time, name] of all the ads detected in the file """ ext = input_file_path.split('.')[-1] audio = pydub.AudioSegment.from_file(input_file_path, ext) audio = audio.set_channels(1) strt = 0 duration = audio.duration_seconds labels = [] while strt < duration - self.config['analyze_span']: end = strt + self.config['analyze_span'] audio_segment = audio[strt * 1000:end * 1000] ad = self.recognize_segment(audio_segment) if ad: strt = int(strt - ad[ComDet.OFFSET_SECS]) end = int(strt + ad[ComDet.AD_DURATION]) strt_string = get_time_string(strt) end_string = get_time_string(end) print "Found:", strt_string, end_string, ad[ ComDet.AD_NAME], ad[ComDet.CONFIDENCE] labels.append([strt_string, end_string, ad[ComDet.AD_NAME]]) strt = end else: strt += self.config['analyze_skip'] return labels
def recognize_ads_file(self, input_file_path): """ Function: Recognize all the commercials present in the file 1) Convert input_file to wav file 2) Process the file in chunks 3) Skip parts based on the offset and duration of recognize_chunk 4) Append [start_time, end_time, name] of each confident segment Input: input_file_path: Either video or audio file whose ads have to be detected. Returns: List of [start_time, end_time, name] of all the ads detected in the file """ ext = input_file_path.split('.')[-1] audio = pydub.AudioSegment.from_file(input_file_path, ext) audio = audio.set_channels(1) strt = 0 duration = audio.duration_seconds labels = [] while strt < duration - self.config['analyze_span']: end = strt + self.config['analyze_span'] audio_segment = audio[strt*1000:end*1000] ad = self.recognize_segment(audio_segment) if ad: strt = int(strt - ad[ComDet.OFFSET_SECS]) end = int(strt + ad[ComDet.AD_DURATION]) strt_string = get_time_string(strt) end_string = get_time_string(end) print "Found:", strt_string, end_string, ad[ComDet.AD_NAME], ad[ComDet.CONFIDENCE] labels.append([strt_string, end_string, ad[ComDet.AD_NAME]]) strt = end else: strt += self.config['analyze_skip'] return labels
def detect(self): # self.X = hickle.load('../data/newdat.hkl') m, n = self.X.shape freqs = np.abs(np.fft.fftfreq(n, 1.0 / 44100)) times = [] print print "Detecting video cuts..", print for i in range(m): magnitudes = self.X[i, :] val = (np.max(magnitudes) + np.min(magnitudes)) / (np.var(magnitudes)) val *= 100 #Pretty output sys.stdout.write('\r') sys.stdout.write("%s done" % (timeFunc.get_time_string(i * WINDOW_SIZE))) sys.stdout.flush() if val > 1: ts = timeFunc.get_time_string(i * WINDOW_SIZE) try: self.times_dic[ts] += 1 except: self.times_dic[ts] = 0 print j = 0 times = self.times_dic.keys() times.sort for time in times: if self.times_dic[ time] == 0: #Occurred only once, very low chances of it being valid del self.times_dic[time] self.times = self.times_dic.keys() self.times.sort() print "Video cuts detected !" print
def recognize(self): labels = LabelsFile(outfile=OUTPUT) print "Now detecting commercials.." i = 0 prev = 0 while i < self.duration: #Pretty printing remaining_time = self.duration - i sys.stdout.write('\r') sys.stdout.write("Duration of video pending analysis: %s" % timeFunc.get_time_string(remaining_time)) sys.stdout.flush() next, data = self.find_commercial(i) #data is an empty list when there is no commercial if len(data) != 0: #If commercial is detected start = data[0] name = data[1] i = next + (VIDEO_GAP / 2) #prev = the end time of the last add that was detected if (start - prev) >= VIDEO_SPAN: #If greater than the amount to scan, has to be marked unclassified labels.write_labels([ timeFunc.get_time_string(prev), timeFunc.get_time_string(start), UNCLASSIFIED_CONTENT ]) else: start = prev #We safely skip prev = next #We mark the commercial detected labels.write_labels([ timeFunc.get_time_string(start), timeFunc.get_time_string(next), name ]) else: #No commercial, proceed scanning i += VIDEO_GAP print if (self.duration - prev) > 1: #Atleast one second labels.write_labels([ timeFunc.get_time_string(prev), timeFunc.get_time_string(self.duration), UNCLASSIFIED_CONTENT ])
def detect(self): # self.X = hickle.load('../data/newdat.hkl') m,n= self.X.shape freqs = np.abs(np.fft.fftfreq(n, 1.0/44100)) times = [] print print "Detecting video cuts..", print for i in range(m): magnitudes = self.X[i, :] val = (np.max(magnitudes) + np.min(magnitudes)) / (np.var(magnitudes)) val *= 100 #Pretty output sys.stdout.write('\r') sys.stdout.write("%s done" % (timeFunc.get_time_string(i * WINDOW_SIZE))) sys.stdout.flush() if val > 1: ts = timeFunc.get_time_string(i * WINDOW_SIZE) try: self.times_dic[ts] += 1 except: self.times_dic[ts] = 0 print j = 0 times = self.times_dic.keys() times.sort for time in times: if self.times_dic[time] == 0: #Occurred only once, very low chances of it being valid del self.times_dic[time] self.times = self.times_dic.keys() self.times.sort() print "Video cuts detected !" print
def get_data(self, window, overlap): """ Gets the freq region of data audio_name = name of the audio file Window = size of the window in seconds, by default it creates a window of 2 milliseconds Overlap = ratio of overlap between frames """ print print "Computing FFT..", self.Fs, frames = wavfile.read(self.audio_name) start = 0 i = window m = int(len(frames) / (window * self.Fs)) # As (k + 1)*window*fs < len(frames) #Creating an empty X matrix n = np.fft.fftfreq(int(self.Fs * window))[:self.Fs // 2].shape[0] #Only real part n = n / 2 + 1 #Since we take only half of real part self.X = np.zeros((m, n), dtype=np.float32) k = 0 print while ((i * self.Fs) < len(frames)): #Pretty output sys.stdout.write('\r') sys.stdout.write("%s done" % (timeFunc.get_time_string(i))) sys.stdout.flush() end = start + int(self.Fs * window) x = np.array( frames[start:end], dtype=np.float32) + 0.0000001 #To remove any zero errors magnitudes = np.abs(np.fft.rfft(x))[:self.Fs / 4] self.X[k] = np.copy(magnitudes) start += int(self.Fs * (1 - overlap) * window) i += window k += 1 print print "Done computing FFT !" print
def recognize(self): labels = LabelsFile(outfile=OUTPUT) print "Now detecting commercials.." i = 0 prev = 0 while i < self.duration: #Pretty printing remaining_time = self.duration - i sys.stdout.write('\r') sys.stdout.write("Duration of video pending analysis: %s" % timeFunc.get_time_string(remaining_time)) sys.stdout.flush() next, data = self.find_commercial(i) #data is an empty list when there is no commercial if len(data) != 0: #If commercial is detected start = data[0] name = data[1] i = next + (VIDEO_GAP / 2) #prev = the end time of the last add that was detected if (start - prev) >= VIDEO_SPAN: #If greater than the amount to scan, has to be marked unclassified labels.write_labels([timeFunc.get_time_string(prev), timeFunc.get_time_string(start), UNCLASSIFIED_CONTENT]) else: start = prev #We safely skip prev = next #We mark the commercial detected labels.write_labels([timeFunc.get_time_string(start), timeFunc.get_time_string(next), name]) else: #No commercial, proceed scanning i += VIDEO_GAP print if (self.duration - prev) > 1: #Atleast one second labels.write_labels([timeFunc.get_time_string(prev), timeFunc.get_time_string(self.duration), UNCLASSIFIED_CONTENT])
def get_data(self, window, overlap): """ Gets the freq region of data audio_name = name of the audio file Window = size of the window in seconds, by default it creates a window of 2 milliseconds Overlap = ratio of overlap between frames """ print print "Computing FFT..", self.Fs, frames = wavfile.read(self.audio_name) start = 0 i = window m = int(len(frames) / (window * self.Fs)) # As (k + 1)*window*fs < len(frames) #Creating an empty X matrix n = np.fft.fftfreq(int(self.Fs*window))[: self.Fs // 2].shape[0] #Only real part n = n / 2 + 1 #Since we take only half of real part self.X = np.zeros((m, n), dtype=np.float32) k = 0 print while ((i*self.Fs) < len(frames)): #Pretty output sys.stdout.write('\r') sys.stdout.write("%s done" % (timeFunc.get_time_string(i))) sys.stdout.flush() end = start + int(self.Fs * window) x = np.array(frames[start:end], dtype=np.float32) + 0.0000001#To remove any zero errors magnitudes = np.abs(np.fft.rfft(x))[:self.Fs / 4] self.X[k] = np.copy(magnitudes) start += int(self.Fs * (1 - overlap) * window) i += window k += 1 print print "Done computing FFT !" print