def addmosaic_video(opt,netS): path = opt.media_path fps,imagepaths = video_init(opt,path) # get position positions = [] for i,imagepath in enumerate(imagepaths,1): img = impro.imread(os.path.join('./tmp/video2image',imagepath)) mask,x,y,area = runmodel.get_ROI_position(img,netS,opt) positions.append([x,y,area]) cv2.imwrite(os.path.join('./tmp/ROI_mask',imagepath),mask) print('\r','Find ROI location:'+str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='') print('\nOptimize ROI locations...') mask_index = filt.position_medfilt(np.array(positions), 7) # add mosaic for i in range(len(imagepaths)): mask = impro.imread(os.path.join('./tmp/ROI_mask',imagepaths[mask_index[i]]),'gray') img = impro.imread(os.path.join('./tmp/video2image',imagepaths[i])) if impro.mask_area(mask)>100: img = mosaic.addmosaic(img, mask, opt) cv2.imwrite(os.path.join('./tmp/addmosaic_image',imagepaths[i]),img) print('\r','Add Mosaic:'+str(i+1)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='') print() ffmpeg.image2video( fps, './tmp/addmosaic_image/output_%05d.'+opt.tempimage_type, './tmp/voice_tmp.mp3', os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.mp4'))
def addmosaic_video(opt, netS): path = opt.media_path fps, imagepaths = video_init(opt, path)[:2] length = len(imagepaths) start_frame = int(imagepaths[0][7:13]) mask_index = get_roi_positions(opt, netS, imagepaths)[(start_frame - 1):] t1 = time.time() if not opt.no_preview: cv2.namedWindow('preview', cv2.WINDOW_NORMAL) # add mosaic print('Step:3/4 -- Add Mosaic:') t1 = time.time() # print(mask_index) for i, imagepath in enumerate(imagepaths, 1): mask = impro.imread( os.path.join( opt.temp_dir + '/ROI_mask', imagepaths[np.clip(mask_index[i - 1] - start_frame, 0, 1000000)]), 'gray') img = impro.imread( os.path.join(opt.temp_dir + '/video2image', imagepath)) if impro.mask_area(mask) > 100: try: #Avoid unknown errors img = mosaic.addmosaic(img, mask, opt) except Exception as e: print('Warning:', e) t = Thread(target=cv2.imwrite, args=(os.path.join(opt.temp_dir + '/addmosaic_image', imagepath), img)) t.start() os.remove(os.path.join(opt.temp_dir + '/video2image', imagepath)) #preview result and print if not opt.no_preview: cv2.imshow('preview', img) cv2.waitKey(1) & 0xFF t2 = time.time() print('\r', str(i) + '/' + str(length), util.get_bar(100 * i / length, num=35), util.counttime(t1, t2, i, length), end='') print() if not opt.no_preview: cv2.destroyAllWindows() print('Step:4/4 -- Convert images to video') ffmpeg.image2video( fps, opt.temp_dir + '/addmosaic_image/output_%06d.' + opt.tempimage_type, opt.temp_dir + '/voice_tmp.mp3', os.path.join(opt.result_dir, os.path.splitext(os.path.basename(path))[0] + '_add.mp4'))
def addmosaic_video(opt, netS): path = opt.media_path fps, imagepaths = video_init(opt, path)[:2] length = len(imagepaths) # get position positions = [] t1 = time.time() if not opt.no_preview: cv2.namedWindow('preview', cv2.WINDOW_NORMAL) print('Step:2/4 -- Find ROI location') for i, imagepath in enumerate(imagepaths, 1): img = impro.imread( os.path.join(opt.temp_dir + '/video2image', imagepath)) mask, x, y, size, area = runmodel.get_ROI_position(img, netS, opt) positions.append([x, y, area]) cv2.imwrite(os.path.join(opt.temp_dir + '/ROI_mask', imagepath), mask) #preview result and print if not opt.no_preview: cv2.imshow('preview', mask) cv2.waitKey(1) & 0xFF t2 = time.time() print('\r', str(i) + '/' + str(length), util.get_bar(100 * i / length, num=35), util.counttime(t1, t2, i, length), end='') print('\nOptimize ROI locations...') mask_index = filt.position_medfilt(np.array(positions), 7) # add mosaic print('Step:3/4 -- Add Mosaic:') t1 = time.time() for i, imagepath in enumerate(imagepaths, 1): mask = impro.imread( os.path.join(opt.temp_dir + '/ROI_mask', imagepaths[mask_index[i - 1]]), 'gray') img = impro.imread( os.path.join(opt.temp_dir + '/video2image', imagepath)) if impro.mask_area(mask) > 100: try: #Avoid unknown errors img = mosaic.addmosaic(img, mask, opt) except Exception as e: print('Warning:', e) cv2.imwrite(os.path.join(opt.temp_dir + '/addmosaic_image', imagepath), img) os.remove(os.path.join(opt.temp_dir + '/video2image', imagepath)) #preview result and print if not opt.no_preview: cv2.imshow('preview', img) cv2.waitKey(1) & 0xFF t2 = time.time() print('\r', str(i) + '/' + str(length), util.get_bar(100 * i / length, num=35), util.counttime(t1, t2, i, length), end='') print() if not opt.no_preview: cv2.destroyAllWindows() print('Step:4/4 -- Convert images to video') ffmpeg.image2video( fps, opt.temp_dir + '/addmosaic_image/output_%06d.' + opt.tempimage_type, opt.temp_dir + '/voice_tmp.mp3', os.path.join(opt.result_dir, os.path.splitext(os.path.basename(path))[0] + '_add.mp4'))
print('Find images:',len(img_names)) for i,img_name in enumerate(img_names,1): try: img = Image.open(os.path.join(img_path,img_name)) img = transform_img(img) img = np.array(img) img = img[...,::-1] if BOUNDING: mosaic_area = 0 while mosaic_area < 16384: mask = Image.open(os.path.join(ir_mask_path,random.choices(mask_names)[0])) mask = transform_mask(mask) mask = np.array(mask) mosaic_area = impro.mask_area(mask) mosaic_img = mosaic.addmosaic_random(img, mask,'bounding') BOUNDING_flag = '_bound' else: mask = Image.open(os.path.join(ir_mask_path,random.choices(mask_names)[0])) mask = transform_mask(mask) mask = np.array(mask) mosaic_img = mosaic.addmosaic_random(img, mask) BOUNDING_flag = '' if HD:#[128:384,128:384,:] --->256 cv2.imwrite(os.path.join(train_A_path,'%05d' % i+BOUNDING_flag+'.jpg'), mosaic_img) cv2.imwrite(os.path.join(train_B_path,'%05d' % i+BOUNDING_flag+'.jpg'), img) else: merge_img = impro.makedataset(mosaic_img, img) cv2.imwrite(os.path.join(train_path,'%05d' % i+BOUNDING_flag+'.jpg'), merge_img)