def processing(self, code): frame_count = self.video.frame_count for i in range(frame_count): img = self.video.getFrame() img_out = self.processor(code, img) utils.save_image(img_out, i) utils.images_ToVideo(self.image_path, self.FPS)
def processor(self, process_Code, img): if process_Code == 'hsv': process_out = utils.RGB2HSV(img) elif process_Code == 'gray': process_out = utils.RGB2Gray(img) elif process_Code == 'equal': process_out = utils.equal_histogram(img) elif process_Code == 'canny': process_out = utils.Canny(img) return process_out
def save(self, event): print("save") #find the nearest good ts point nansIndx = np.where(np.isnan(x))[0] isnumIndx = np.where(~np.isnan(x))[0] for nan in nansIndx: replacementCandidates = np.where(isnumIndx > nan)[0] if replacementCandidates.size != 0: rx = x[isnumIndx[replacementCandidates[0]]] ry = y[isnumIndx[replacementCandidates[0]]] rxfilt = xfilt[isnumIndx[replacementCandidates[0]]] ryfilt = yfilt[isnumIndx[replacementCandidates[0]]] else: rx = x[isnumIndx[np.where(isnumIndx < nan)[0][-1]]] ry = y[isnumIndx[np.where(isnumIndx < nan)[0][-1]]] rxfilt = xfilt[isnumIndx[np.where(isnumIndx < nan)[0][-1]]] ryfilt = yfilt[isnumIndx[np.where(isnumIndx < nan)[0][-1]]] x[nan] = rx xfilt[nan] = rxfilt y[nan] = ry yfilt[nan] = ryfilt moving, notmoving, speed = vu.returnMoving(xfilt, yfilt, 30) np.savez("./RawData/POSITION", xfilt, yfilt, ts, x, y, moving, notmoving, speed)
def get_data(csv_fname, video_fname, binary=False, num_frames=None, regression=False, OBJECTS=['person'], resol=(50, 50), center=True, dtype='float32', train_ratio=0.6): def print_class_numbers(Y, nb_classes): classes = np_utils.probas_to_classes(Y) for i in xrange(nb_classes): print 'class %d: %d' % (i, np.sum(classes == i)) print '\tParsing %s, extracting %s' % (csv_fname, str(OBJECTS)) if binary: all_counts = get_binary(csv_fname, limit=num_frames, OBJECTS=OBJECTS) else: all_counts = get_counts(csv_fname, limit=num_frames, OBJECTS=OBJECTS) print '\tRetrieving all frames from %s' % video_fname all_frames = VideoUtils.get_all_frames(len(all_counts), video_fname, scale=resol, dtype=dtype) print '\tSplitting data into training and test sets' X_train, X_test, Y_train, Y_test = to_test_train(all_frames, all_counts, regression=regression, center=center, dtype=dtype, train_ratio=train_ratio) if regression: nb_classes = 1 print '(train) mean, std: %f, %f' % \ (np.mean(Y_train), np.std(Y_train)) print '(test) mean, std: %f %f' % \ (np.mean(Y_test), np.std(Y_test)) else: nb_classes = all_counts.max() + 1 print '(train) positive examples: %d, total examples: %d' % \ (np.count_nonzero(np_utils.probas_to_classes(Y_train)), len(Y_train)) print_class_numbers(Y_train, nb_classes) print '(test) positive examples: %d, total examples: %d' % \ (np.count_nonzero(np_utils.probas_to_classes(Y_test)), len(Y_test)) print_class_numbers(Y_test, nb_classes) print 'shape of image: ' + str(all_frames[0].shape) print 'number of classes: %d' % (nb_classes) data = (X_train, Y_train, X_test, Y_test) return data, nb_classes
def get_url(): video_url = request.args.get('url') videoUtils = VideoUtils.VideoUtils(temp_folder, checkpoint_file) temp_video_file = temp_folder + videoUtils.rnd_folder_label() + '.avi' with open(temp_video_file, 'wb') as f: f.write(urlrequest.urlopen(video_url).read()) f.close() output_video_file = temp_folder + 'output_' + videoUtils.rnd_folder_label() + '.avi' videoUtils.slow_down_video(temp_video_file, output_video_file) return 'Url: ' + video_url + ' Output Video File: ' + output_video_file
def train(self): if not os.path.exists(self.output_video_frames_list): print('Create training file, this could take a while :)' + self.output_video_frames_list) videoUtils = VideoUtils.VideoUtils(self.tmp_folder, '', self.image_width, self.image_height) videoUtils.create_temp_folder(self.output_images_folder) for folder in glob.glob(self.input_video_folder + "*/"): folder_name = folder.replace(self.input_video_folder, '').replace('/', '') for file in glob.glob(folder + "*." + self.video_extension): file_name = file.replace( self.input_video_folder + folder_name + '/', '').replace('.' + self.video_extension, '') print(folder, folder_name, file, file_name) new_video_folder = self.output_images_folder + folder_name + '/' + file_name + '/' videoUtils.create_temp_folder(new_video_folder) f1 = open(self.output_video_frames_list, 'a') cap = cv2.VideoCapture(file) current_frame = 1 while cap.isOpened(): ret, frame = cap.read() if ret == True: output_image_file = new_video_folder + 'frame_' + videoUtils.padded( current_frame) + '.png' cv2.imwrite(output_image_file, frame) image_parts = videoUtils.get_image_parts( output_image_file) count_keys = 1 for key, value in enumerate(image_parts): output_image_file_part = output_image_file.replace( '.png', '_' + str(key + 1) + '.png') cv2.imwrite(output_image_file_part, value['im']) count_keys += 1 if current_frame > 2: for x in range(1, count_keys): line = '' line += new_video_folder + 'frame_' + videoUtils.padded( current_frame - 2) + '_' + str(x) + '.png,' line += new_video_folder + 'frame_' + videoUtils.padded( current_frame - 1) + '_' + str(x) + '.png,' line += new_video_folder + 'frame_' + videoUtils.padded( current_frame) + '_' + str( x) + '.png\n' f1.write(line) current_frame += 1 os.remove(output_image_file) else: break f1.close() model = Model.Model() prediction, reproduction_loss, total_loss, update_op, sess, saver = model.get( "", self.learning_rate, self.model_output_folder) offset = os.path.getsize(self.output_video_frames_list) - 1500 f = open(self.output_video_frames_list) for step in range(0, self.steps): f.seek(offset) f.readline() list_of_image_set = f.readline() image_files = list_of_image_set.split(",") image_file_1 = image_files[0].rstrip() image_file_2 = image_files[1].rstrip() image_file_3 = image_files[2].rstrip() image_raw_1 = [model.read_image(image_file_1)] image_raw_2 = [model.read_image(image_file_2)] image_raw_3 = [model.read_image(image_file_3)] feed_dict = { model.input_placeholder: np.concatenate((image_raw_1, image_raw_3), 3), model.target_placeholder: image_raw_2 } _, loss_value = sess.run([update_op, total_loss], feed_dict=feed_dict) if step % 10 == 0: print("Loss at step %d: %f" % (step, loss_value)) if step % 5000 == 0 or (step + 1) == self.steps: checkpoint_path = os.path.join(self.model_output_folder, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step)
import VideoUtils as vu import numpy as np import GeneralUtils as gu from matplotlib import pyplot as plt makeplot = 0 #pvdfile = './RawData/dwP.pvd' pvdfile = './RawData/sleep_dwPout.pvd' ts, x, y = vu.readPVDfile(pvdfile) x /= 8.2 xsmooth = np.abs(np.convolve(x, np.ones(100, dtype=np.int), 'valid')) / 100 cum = np.cumsum(np.abs(np.diff(xsmooth))) inotmoving = np.where(np.diff(cum) < .025)[0] grouped = gu.group_consecutives(inotmoving) starts = [] stops = [] for group in grouped: if group[-1] - group[0] > 1000: starts.append(ts[group[0]]) stops.append(ts[group[-1]]) # minix = x[group[0]:group[-1]] if makeplot == 1: plt.plot(x[group[0]:group[-1]], y[group[0]:group[-1]], 'b.') plt.ylim([0, 480]) plt.xlim([0, 640]) plt.show()
#plot min and max values xmin = np.amin(xfilt) xmax = np.amax(xfilt) ymin = np.amin(yfilt) ymax = np.amax(yfilt) ylim = 0 #xfilt,yfilt = vu.mask(xfilt,yfilt,0, ylim) #moving,notmoving,speed = vu.returnMoving(xfilt, yfilt, 30) #print(speed) #plt.hist(speed,bins=range(0,1000,1)) #plt.show() tstart, tstop = vu.returnTrajectoryFlags(moving, 240) tspeed = [ np.mean(np.asarray(speed[start:stop])) for start, stop in zip(tstart, tstop) ] #for dwells dmoving, dnotmoving, dspeed = vu.returnMoving(xfilt, yfilt, 5) dstart, dstop = vu.returnTrajectoryFlags(dnotmoving, 240) dspeed = [ np.mean(np.asarray(speed[start:stop])) for start, stop in zip(dstart, dstop) ] fig, ax = plt.subplots()
def __init__(self, tmp_folder): self.tmp_folder = tmp_folder self.videoUtils = VideoUtils.VideoUtils(self.tmp_folder, 'models/model')
# Threshold_mse = 0.000167 Threshold = 0.000167 dist_metric = 'mse' starttime = time.time() while (video_cap.isOpened()): ret, _ = video_cap.read() if ret is False: break frame_count = frame_count + 1 print(frame_count) frames = VideoUtils.get_all_frames( frame_count, video_name, 0, 1, ) length = len(frames) print('length=', length) for i in range(1, length, 4): frame_x = frames[i - 1] frame_x = np.array(frame_x) hog_x = HOG.compute_feature(frame_x, 10) frame_x_delay = frames[i] frame_x_delay = np.array(frame_x_delay) hog_x_delay = HOG.compute_feature(frame_x_delay, 10) distance = RawImage.get_distance_fn(dist_metric, hog_x, hog_x_delay)
import VideoUtils videoUtils = VideoUtils.VideoUtils('tmp/', 'models/') videoUtils.slow_down_video('../data/v_Fencing_g01_c02.avi', 'output_v_Fencing_g01_c02.avi')
f.close() ## check ylim xlim = [0, 640] #ylim = [0, 480] ylim = [240, 480] npzfile = np.load('./RawData/EPOCHS.npz') start = npzfile['arr_0'].astype(int) stop = npzfile['arr_1'].astype(int) videofile = './RawData/VT1.Nvt' timestamps, xpt, ypt, dwP, dnT = vu.getVideoData(videofile) P = list(dwP[start:stop]) T = list(dnT[start:stop]) ts = list(timestamps[start:stop]) mask = makeMask(P) plt.imshow(mask) plt.show() outputPrePVD("./RawData/maze_dwPout.ascii", P, ts, 0, xlim, ylim, applymask=True)
sys.setrecursionlimit(100000) vidfile = './RawData/VT1.Nvt' with open(vidfile, 'rb') as f: videodata = f.read()[16384:] f.close() npzfile = np.load("./RawData/EPOCHS.npz") start_idx = int(npzfile['arr_0']) stop_idx = int(npzfile['arr_1']) print(start_idx) print(stop_idx) x, y, ts = vu.getVideodata(videodata) x = x[start_idx:stop_idx] y = y[start_idx:stop_idx] ts = ts[start_idx:stop_idx] xfilt = vu.smooth(x, window_len=200) yfilt = vu.smooth(y, window_len=200) xmin = np.amin(xfilt) xmax = np.amax(xfilt) ymin = np.amin(yfilt) ymax = np.amax(yfilt) ylim = 0 fig, ax = plt.subplots()
def slider_on_change(val): if dir == "1": l1.set_xdata(xfilt[0:int(val)]) l1.set_ydata(yfilt[0:int(val)]) elif dir == "-1": l1.set_xdata(xfilt[int(val):-1]) l1.set_ydata(yfilt[int(val):-1]) if dir == "0": l2.set_xdata(newx[int(val):(int(val)+10000)]) l2.set_ydata(newy[int(val):(int(val)+10000)]) print(int(val)) fig.canvas.draw_idle() videofile = './RawData/VT1.Nvt' x, y, ts = vu.getTrackerXY_Points(videofile) xfilt = vu.smooth(x, window_len=200) yfilt = vu.smooth(y, window_len=200) #plot min and max values xmin = np.amin(xfilt) xmax = np.amax(xfilt) ymin = np.amin(yfilt) ymax = np.amax(yfilt) ylim=100 #xfilt,yfilt = vu.mask(xfilt,yfilt,0, ylim) fig, ax = plt.subplots()
with open(vidfile, 'rb') as f: videodata = f.read()[16384:] #spikets = readTFile("../RawData/Sc10_5.t") spikets = readOldTFile(sys.argv[1]) print spikets[:20] start_idx = sys.argv[2] stop_idx = sys.argv[3] print(start_idx) print(stop_idx) start_idx = 77670 stop_idx = 239045 x, y, ts = vu.getVideodata(videodata) x = x[start_idx:stop_idx] y = y[start_idx:stop_idx] ts = ts[start_idx:stop_idx] #print("ts") print(ts[0]) print(ts[-1]) #print("spikes") print(spikets[0]) print(spikets[-1]) #vidfile = '../RawData/VT1.Nvt' #testFile = './rec_09152017/VT1.Nvt' xfilt = vu.smooth(x, window_len=200) yfilt = vu.smooth(y, window_len=200)
pure[3], x, y) f.write(outstr) img[x, y] += 1 f.close() xlim = [0, 640] ylim = [0, 480] npzfile = np.load('./RawData/EPOCHS.npz') start = npzfile['arr_0'].astype(int) stop = npzfile['arr_1'].astype(int) timestamps, xpt, ypt, dwP, dnT = vu.getVideoData('./RawData/VT1.Nvt') P = list(dwP[start:stop]) T = list(dnT[start:stop]) ts = list(timestamps[start:stop]) mask = makeMask(P) np.savez("./RawData/mask", mask) #plt.imshow(mask) #plt.show() outputPrePVD("./RawData/maze_dwPout.ascii", P, ts, 0,
l1.set_xdata(x_pix[current_frame:current_frame + 100]) l1.set_ydata(y_pix[current_frame:current_frame + 100]) l2.set_color('g') l2.set_xdata(pvdx[current_frame:current_frame + 100]) l2.set_ydata(pvdy[current_frame:current_frame + 100]) print(int(current_frame)) fig.canvas.draw_idle() current_frame = 1 videofile = './RawData/VT1.Nvt' pvdfile = './RawData/maze_dnTout.pvd' x, y, ts = vu.getTrackerXY_Points(videofile) x = x[79000:] y = y[79000:] x_pix = np.round(x / (np.max(x) / 640)) y_pix = np.round(y / (np.max(y) / 480)) pvdts, pvdx, pvdy = vu.readPVDfile(pvdfile) fig, ax = plt.subplots() l1, = plt.plot(x_pix, y_pix, '.', markersize=5, color='#FFE4C4') l2, = plt.plot(pvdx, pvdy, '.', markersize=5, color='g') cid = fig.canvas.mpl_connect('key_press_event', arrow_key_control) plt.show()
def slider_on_change(val): if dir == "1": l1.set_xdata(xfilt[0:int(val)]) l1.set_ydata(yfilt[0:int(val)]) elif dir == "-1": l1.set_xdata(xfilt[int(val):-1]) l1.set_ydata(yfilt[int(val):-1]) if dir == "0": l2.set_xdata(newx[int(val):(int(val) + 10000)]) l2.set_ydata(newy[int(val):(int(val) + 10000)]) print(int(val)) fig.canvas.draw_idle() x, y, ts = vu.oldVideoData('./RawData/VT1.Nvt') xfilt = vu.smooth(x, window_len=200) yfilt = vu.smooth(y, window_len=200) #plot min and max values xmin = np.amin(xfilt) xmax = np.amax(xfilt) ymin = np.amin(yfilt) ymax = np.amax(yfilt) #ylim=100 #xfilt,yfilt = vu.mask(xfilt,yfilt,0, ylim) fig, ax = plt.subplots()
from matplotlib.gridspec import GridSpec from scipy.stats import entropy import scipy.stats as stats pvdfile = sys.argv[1] tfilename = sys.argv[2] mthresh = sys.argv[3] dsname = sys.argv[4] animal = dsname.split('/')[0] recording_date = dsname.split('/')[1].split('_')[0] recording_time = dsname.split('/')[1].split('_')[1] cellname = tfilename.replace('./RawData/', '') outcellname = dsname + '/' + cellname ts, x, y = vu.readPVDfile(pvdfile) x /= 8.2 xsmooth = np.abs(np.convolve(x, np.ones(100, dtype=np.int), 'valid')) / 100 #use later to detect direction direction = np.where(np.diff(xsmooth) > 0, 1, 0) #print("size of direction: ", np.size(direction)) #inotmoving and imoving are for entire recording cum = np.cumsum(np.abs(np.diff(xsmooth))) inotmoving = np.where(np.diff(cum) < .025)[0] imoving = np.where(np.diff(cum) >= .025)[0] print("reation") print(np.size(inotmoving)) print(np.size(imoving))
for i in range(frame_count): img = self.video.getFrame() img_out = self.processor(code, img) utils.save_image(img_out, i) utils.images_ToVideo(self.image_path, self.FPS) if __name__ == '__main__': stype = input('请输入操作类型:') if stype == '图片': pname = input('请输入图片名称:') ttype = input('请输入图片操作类型:') if ttype == '旋转': utils.rotate(pname) elif ttype == '负片': utils.nagetive(pname) elif ttype == '模糊': utils.dim(pname) elif ttype == '灰度': utils.P2Gray(pname) elif ttype == '图像分割': utils.segmentation(pname) elif ttype == '物品识别': utils.shibie(pname) elif ttype == '嵌入水印': watermark = input('请输入嵌入的水印:') utils.ccode(pname, watermark) elif ttype == '水印解码': utils.decode(pname)