def show_image(img, image_flag): path = 'movies/' img = torchvision.utils.make_grid(img) # torchvision.utils.save_image(img, "coco_AutoEncoder_" + image_flag + "_0607.png") img = img / 2 + 0.5 # npimg = np.clip(npimg, 0, 1) # if image_flag == "out": # # img = normalize_images(img) # img = img.mul(torch.FloatTensor([0.5, 0.5, 0.5]).view(3, 1, 1)) # img = img.add(torch.FloatTensor([0.5, 0.5, 0.5]).view(3, 1, 1)) npimg = img.detach().numpy() * 255 print("max", np.min(npimg)) figure_image = plt.figure() plt.imshow(np.transpose(npimg, (1, 2, 0))) cv2.imshow("aa", npimg) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imsave(path + "coco_AutoEncoder_" + image_flag + "_sample_0702.png", npimg) # figure_image.subplots_adjust(left=0, right=0, bottom=0, top=0) figure_image.savefig(path + "coco_AutoEncoder_" + image_flag + "_sample_0702.png") # figure_image.savefig(path + "coco_AutoEncoder_" + image_flag + "_0615.png") pdb.set_trace() plt.show() return npimg
def save_head(fname, im_fname, im_sample, target_root): with open(fname) as f: data = json.load(f) head_img = np.zeros((HEAD_SIZE, HEAD_SIZE, 3), dtype=np.int) head_cent = np.array([0, 0]) isvalid = False for jj in range(len(data["people"])): face = np.array(data["people"][jj]["face_keypoints_2d"]) # *70 points face[face > 511] = 511 head_cent = np.array([face[3 * 30], face[3 * 30 + 1]], dtype=np.int) if (head_cent[1]-HALF_HEAD>=0 and\ head_cent[1]+HALF_HEAD<BODY_SIZE and\ head_cent[0]-HALF_HEAD>=0 and\ head_cent[0]+HALF_HEAD<BODY_SIZE): head_img=im_sample[head_cent[1]-HALF_HEAD:\ head_cent[1]+HALF_HEAD,\ head_cent[0]-HALF_HEAD:\ head_cent[0]+HALF_HEAD,:] print(head_img.shape, head_img.min(), head_img.max(), im_sample.dtype) cv2.imsave(f"{target_root}/{im_fname}.png", cv2.cvtColor(head_img, cv2.COLOR_RGB2BGR)) isvalid = True else: print("fku") isvalid = False return head_img.astype(np.float), head_cent, isvalid
def demo(): """ Demos the largest_rotated_rect function """ images = glob.glob("/home/apurv/PID-PROJ/sample-image/*.png") for image in images: print('reading image: ', image) filename = os.path.basename(image) name, ext = os.path.splitext(filename) image = cv2.imread(image) image_height, image_width = image.shape[0:2] os.makedirs('training_data/%s' %name, exist_ok=True) for i in np.arange(0, 360, 0.5): image_orig = np.copy(image) image_rotated = rotate_image(image, i) image_rotated_cropped = crop_around_center( image_rotated, *largest_rotated_rect( image_width, image_height, math.radians(i) ) ) cv2.imsave('training_data/%s/%s.png' %(name , i), image_rotated_cropped)
def plotAndSave(flow, save_name, hsv): print('flow', flow.shape) print('hsv', hsv.shape) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180 / np.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) cv2.imsave(DATA_DIR + "/" + save_name + ".png", rgb) cv2.imwrite('opticalfb.png', frame2) cv2.imwrite('opticalhsv.png', rgb)
def save_img(img, path): img = deprocess(img) img = img.numpy() img *= 255.0 img = img.clip(0, 255) img = np.transpose(img, (1, 2, 0)) img = cv2.resize(img, (255, 200, 3)) img = img.astype(np.uint8) cv2.imsave(img, path)
def main(): import cv2 import matplotlib.pyplot as plt img = cv2.imread('donkey.png') img = img[:, :, :3] trimap = cv2.imread('donkeyTrimap.png', -1) trimap = trimap[:, :, :3] alpha = knn_matte(img, trimap) cv2.imsave('donkeyAlpha.png', alpha) plt.title('Alpha Matte') plt.imshow(alpha, cmap='gray') plt.show()
def main(): parser = argparse.ArgumentParser(description='Seg-unravel on a given image.') parser.add_argument('--network', type=str, help='The segmentation network to be used.', required=True) parser.add_argument('--shift_type', type=str, help='The shift type to be used. Read Thesis for more details.',default='full_shift') parser.add_argument('--image', type=str, help='Path to image file.', required=True) args = parser.parse_args() # set caffe before importing seg_fix caffe_version = args.network set_caffe_path(caffe_version) import seg_fix import caffe # get the caffe model prototxt = os.path.join('prototxt',file_map[caffe_version][0]) wts = os.path.join('weights',file_map[caffe_version][1]) net = caffe.Net(prototxt,wts, caffe.TEST) # For forward image_blob = u.get_blob(args.image) net.blobs['data'].data[...] = image_blob top_layer_output = net.forward()['fc1_interp'] # Seg_fix in action fixer = seg_fix.seg_fix(prototxt,caffe_version) top_fixations = fixer.get_top_fixations(top_layer_output) # find class-id seg_map = np.argmax(top_layer_output,1)[0] class_ids = list(np.unique(seg_map)) class_ids.remove(0) # not bg seg_space = [np.sum(seg_map==id) for id in class_ids] class_id = class_ids[np.argmax(seg_space)] # you can get fixations for each of the detected classes by sending each detected class-id in the following function image_fixations, all_fixations= fixer.get_fixations_at_all_layers(top_fixations[class_id],net) # save numpy with fixations np.save('temp.npy',all_fixations) # Get the image_with Fixations img_with_fixations = u.embed_fixations(args.image,image_fixations) cv2.imsave('temp.png', img_with_fixations)
def saveNewFile(new_file_data, name): if args.channel == 'h': new_data = rescale_intensity(new_file_data[:, :, 0], out_range=(0, 1)) elif args.channel == 'e': new_data = rescale_intensity(new_file_data[:, :, 1], out_range=(0, 1)) elif args.channel == 'd': new_data = rescale_intensity(new_file_data[:, :, 2], out_range=(0, 1)) elif args.channel == 'hed': h = rescale_intensity(new_file_data[:, :, 0], out_range=(0, 1)) e = rescale_intensity(new_file_data[:, :, 1], out_range=(0, 1)) d = rescale_intensity(new_file_data[:, :, 2], out_range=(0, 1)) new_data = np.dstack((h, e, d)) new_data = img_as_ubyte(new_data) imsave(os.path.join(args.output, name), new_data)
def display(self, camera: int = 0, step: int = 1) -> None: self._image_dir = os.path.join(self._base_dir, "image_{}".format(camera)) total = len(self._files) while True: filename = self._files[self._current % total] image_path = os.path.join(self._image_dir, "{}.jpg".format(filename)) label_path = os.path.join(self._label_dir, "{}.pb".format(filename)) laser_path = os.path.join(self._laser_dir, "{}.bin".format(filename)) laser_r_path = os.path.join(self._laser_r_dir, "{}.bin".format(filename)) img = cv2.imread(image_path) pcl = np.fromfile(laser_path, dtype=np.float32).reshape(-1, 3) pcl_r = np.fromfile(laser_r_path, dtype=np.float32).reshape(-1, 1) anno = Annotation() anno.ParseFromString(open(label_path, "rb").read()) camera_calibration = anno.context.camera_calibrations[camera] vehicle_to_image = get_image_transform(camera_calibration) if self._project: self._project_laser_on_image(img, pcl, pcl_r, vehicle_to_image) if self._draw_2d: labels = anno.camera_labels[camera].labels for label in labels: self._draw_2d_box(img, label) if self._draw_3d: labels = anno.laser_labels for label in labels: self._draw_3d_box(img, vehicle_to_image, label) cv2.imshow("Viewer2D", img) key = cv2.waitKey(-1) if key == ord("q"): break elif key == ord("s"): cv2.imsave("demo{}.jpg".format(anno.timestamp_micros), img) self._current += step
def evaluate(): output_file = 'answer_%s.csv' % args.tag model, _, _ = load_model() model.eval() dataset_args = {} if args.context: dataset_args.update( dict(mask_detection=True, boarder_ratio=5, patch_size=224)) if args.normalize_size: dataset_args.update(dict(normalize_size=True)) if args.normalize_rotation: dataset_args.update(dict(normalize_rotation=True)) dataset = MafatDataset('data/test.csv', 'data/answer.csv', 'data/test imagery', preload=args.preload, augment=False, **dataset_args) writer = SummaryWriter('runs/%s/%s' % (args.architect, args.tag)) train_loader = torch.utils.data.DataLoader(dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, collate_fn=collate_fn) sigmoid = nn.Sigmoid() collector = PredictionCollector(dataset.get_class_names()) with torch.no_grad(): for it, data in enumerate(train_loader): images, labels, gt_text = data X = Variable(images).cuda() outputs = model(X) prediction = sigmoid(outputs) ids = [int(text.split(',')[0]) for text in gt_text] collector.add(ids, prediction) pred_text = map(dataset.labels_to_text, prediction.detach().cpu().numpy()[:16]) grid = display_images(X[:16], gt_text[:16], pred_text[:16], nrow=4) cv2.imsave(grid.numpy()[0], 'test_grid.png') writer.add_image('Test/images', grid, it) collector.save(output_file)
def YOLO_infer(): ''' Will be a dictionary of objects found''' global image_data global YOLO_result YOLO_result = tfnet.return_predict(image_data) # Need to modify label_colour={"Sink":(255, 0, 0),\ "Bidet":(0, 255, 0),\ "Shower":(0, 0, 255),\ "Tap":(0, 255, 255),\ "Bathtub":(255, 255, 0),\ "Toilet":(255,0, 255),\ "Toilet_paper":(120, 120, 255)} font = cv.FONT_HERSHEY_SIMPLEX for i in result: topleft_coord = (i['topleft']['x'], i['topleft']['y']) bottomright_coord = (i['bottomright']['x'], i['bottomright']['y']) imgcv = cv2.rectangle(imgcv, topleft_coord, bottomright_coord, (0, 255, 0), 3) cv.putText(imgcv, i['label'], topleft_coord, font, 0.8, (0, 255, 0), 2, cv.LINE_AA) cv2.imsave("YOLO_output/YOLO.jpg", imgcv)
def resave(ori_path, tgt_path): flist = load_flist(ori_path) flist_rgb = list(filter(lambda s: "_rgb" in s, flist)) flist_nir = list(filter(lambda s: "_nir" in s, flist)) flist_rgb.sort() flist_nir.sort() total = len(flist_rgb) noise_sigma = 25 index_trainset = random.sample(range(total), int(total*0.8)) index_testset = [x for x in range(total) if x not in index_trainset] index_trainset.sort() index_testset.sort() index_dict = { "train": index_trainset, "test": index_testset } for set in ["train", "test"]: tgt_rgb_path = os.path.join(tgt_path, set, "rgb") tgt_nir_path = os.path.join(tgt_path, set, "nir") tgt_noise_path = os.path.join(tgt_path, set, "noise") create_dir(tgt_rgb_path) create_dir(tgt_nir_path) create_dir(tgt_noise_path) for i in index_dict[set]: print("\rProgress: %d/%d"%(i, total), end='') # rgb fn_rgb = flist_rgb[i] foldername = os.path.basename(os.path.dirname(fn_rgb)) basename = os.path.splitext(os.path.basename(fn_rgb))[0] fn_rgb_tgt = os.path.join(tgt_rgb_path, foldername + "_" + basename + ".png") im = imread(fn_rgb) imsave(fn_rgb_tgt, im) # add noise fn_noise_tgt = os.path.join(tgt_noise_path, foldername + "_" + basename.replace("rgb", "noise") + ".png") im_noise = add_gaussian_noise(im, noise_sigma) imsave(fn_noise_tgt, im_noise) # nir fn_nir = flist_nir[i] foldername = os.path.basename(os.path.dirname(fn_nir)) basename = os.path.splitext(os.path.basename(fn_nir))[0] fn_nir_tgt = os.path.join(tgt_nir_path, foldername + "_" + basename + ".png") im = imread(fn_nir) imsave(fn_nir_tgt, im) print("done")
X1 = np.transpose( 255.0 * X1.clip(0, 1.0)[0, :, intPaddingTop:intPaddingTop + intHeight, intPaddingLeft:intPaddingLeft + intWidth], (1, 2, 0)) timestep = args.time_step numFrames = int(1.0 / timestep) - 1 time_offsets = [kk * timestep for kk in range(1, 1 + numFrames, 1)] # for item, time_offset in zip(y_,time_offsets): # arguments_strOut = os.path.join(gen_dir, dir, "frame10_i{:.3f}_11.png".format(time_offset)) # # imsave(arguments_strOut, np.round(item).astype(numpy.uint8)) # # # copy the first and second reference frame # shutil.copy(arguments_strFirst, os.path.join(gen_dir, dir, "frame10_i{:.3f}_11.png".format(0))) # shutil.copy(arguments_strSecond, os.path.join(gen_dir, dir, "frame11_i{:.3f}_11.png".format(1))) count = 0 shutil.copy(arguments_strFirst, os.path.join(gen_dir, dir, "{:0>4d}.png".format(count))) count = count + 1 for item, time_offset in zip(y_, time_offsets): arguments_strOut = os.path.join(gen_dir, dir, "{:0>4d}.png".format(count)) count = count + 1 imsave(arguments_strOut, np.round(item).astype(np.uint8)) shutil.copy(arguments_strSecond, os.path.join(gen_dir, dir, "{:0>4d}.png".format(count))) count = count + 1
def Capture(image): cv2.imsave(str(image), image)
motionProxy = ses.service('ALMotion') postureProxy = ses.service('ALRobotPosture') videoClient = video.subscribeCamera("python_client", CameraIndex, resolution, colorSpace, 5) GENERATE_IMAGES = True if GENERATE_IMAGES: IP = '192.168.1.5' PORT = 9559 resolution = 2 # VGA colorSpace = 11 X, Y = 50, 50 for YAW in tqdm(np.arange(-0.5, 0.5, 0.05)): for PITCH in np.arange(-0.6, 0.5, 0.03): motionProxy.setAngles("HeadYaw", YAW, 0.7) # motionProxy.setAngles("HeadPitch", PITCH, 0.7) arr = video.getImageRemote(videoClient) naoImage = Image.frombytes('RGB', (arr[0], arr[1]), arr[6]) naoImage = np.array(naoImage) (found, corners) = cv.findChessboardCorners( naoImage, (9, 6), flags=cv.CALIB_CB_ADAPTIVE_THRESH | cv.CALIB_CB_FILTER_QUADS, stop_criteria=(cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)) if found and len(corners) == 54: cv.imsave( 'IMAGE_' + str(X) + '_' + str(Y) + '_' + str(YAW) + '_' + str(PITCH) + '.jpg', naoImage)
import numpy as np import cv2 from sklearn.cluster import KMeans n_colors = 10 sample_img = cv2.imread('resources/girl.jpg') w, h, _ = sample_img.shape sample_img = sample_img.reshape(w * h, 3) kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(sample_img) # find out which cluster each pixel belongs to. labels = kmeans.predict(sample_img) # the cluster centroids is our color palette identified_palette = np.array(kmeans.cluster_centers_).astype(int) # recolor the entire image recolored_img = np.copy(sample_img) for index in range(len(recolored_img)): recolored_img[index] = identified_palette[labels[index]] # reshape for display recolored_img = recolored_img.reshape(w, h, 3) cv2.imsave('kmeans_color_q.jpg', recolored_img)
X1 = X1.data.numpy() X0 = np.transpose(255.0 * X0.clip(0, 1.0)[0, :, intPaddingTop:intPaddingTop + intHeight, intPaddingLeft: intPaddingLeft + intWidth], (1, 2, 0)) y_ = np.transpose(255.0 * y_.clip(0, 1.0)[0, :, intPaddingTop:intPaddingTop + intHeight, intPaddingLeft: intPaddingLeft + intWidth], (1, 2, 0)) offset = [np.transpose( offset_i[0, :, intPaddingTop:intPaddingTop + intHeight, intPaddingLeft: intPaddingLeft + intWidth], (1, 2, 0)) for offset_i in offset] filter = [np.transpose( filter_i[0, :, intPaddingTop:intPaddingTop + intHeight, intPaddingLeft: intPaddingLeft + intWidth], (1, 2, 0)) for filter_i in filter] if filter is not None else None X1 = np.transpose(255.0 * X1.clip(0, 1.0)[0, :, intPaddingTop:intPaddingTop + intHeight, intPaddingLeft: intPaddingLeft + intWidth], (1, 2, 0)) imsave(arguments_strOut, np.round(y_).astype(numpy.uint8)) rec_rgb = imread(arguments_strOut) gt_rgb = imread(gt_path) diff_rgb = 128.0 + rec_rgb - gt_rgb avg_interp_error_abs = np.mean(np.abs(diff_rgb - 128.0)) interp_error.update(avg_interp_error_abs, 1) mse = numpy.mean((diff_rgb - 128.0) ** 2) PIXEL_MAX = 255.0 psnr = 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) print("interpolation error / PSNR : " + str(round(avg_interp_error_abs, 4)) + " / " + str(round(psnr, 4)))
## needs debugging import numpy as np import glob import cv2 def rotateImage(image, angle): image_center = tuple(np.array(image.shape)/2) rot_mat = cv2.getRotationMatrix2D(image_center,angle,1.0) result = cv2.warpAffine(image, rot_mat, image.shape,flags=cv2.INTER_LINEAR) return result if __name__ == "__main__": img_path = '/Users/gzhou/TL_documents/python/video-conf/161024-stereo/test/' img_list = [] for filename in glob.glob(img_path+'*.png'): img = cv2.imread(filename) imgr = rotateImage(img, 90) img_list.append(img) cv2.imsave(filename+'r.png', imgr)
def test(args): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model_file_name = os.path.split(args.model_path)[1] model_name = model_file_name[:model_file_name.find("_")] # Setup image print("Read Input Image from : {}".format(args.img_path)) img = cv2.imread(args.img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) data_loader = get_loader(args.dataset) loader = data_loader(root=None, is_transform=True, img_norm=args.img_norm, test_mode=True) n_classes = loader.n_classes resized_img = cv2.resize(img, (loader.img_size[1], loader.img_size[0]), interpolation=cv2.INTER_CUBIC) orig_size = img.shape[:-1] if model_name in ["pspnet", "icnet", "icnetBN"]: # uint8 with RGB mode, resize width and height which are odd numbers img = cv2.resize( img, (orig_size[1] // 2 * 2 + 1, orig_size[0] // 2 * 2 + 1)) else: img = cv2.resize(img, (loader.img_size[1], loader.img_size[0])) img = img[:, :, ::-1] img = img.astype(np.float64) img -= loader.mean if args.img_norm: img = img.astype(float) / 255.0 # NHWC -> NCHW img = img.transpose(2, 0, 1) img = np.expand_dims(img, 0) img = torch.from_numpy(img).float() # Setup Model model_dict = {"arch": model_name} model = get_model(model_dict, n_classes, version=args.dataset) state = convert_state_dict( torch.load(args.model_path, map_location='cpu')["model_state"]) model.load_state_dict(state) model.eval() model.to(device) images = img.to(device) outputs = model(images) if args.dcrf: unary = outputs.data.cpu().numpy() unary = np.squeeze(unary, 0) unary = -np.log(unary) unary = unary.transpose(2, 1, 0) w, h, c = unary.shape unary = unary.transpose(2, 0, 1).reshape(loader.n_classes, -1) unary = np.ascontiguousarray(unary) resized_img = np.ascontiguousarray(resized_img) d = dcrf.DenseCRF2D(w, h, loader.n_classes) d.setUnaryEnergy(unary) d.addPairwiseBilateral(sxy=5, srgb=3, rgbim=resized_img, compat=1) q = d.inference(50) mask = np.argmax(q, axis=0).reshape(w, h).transpose(1, 0) decoded_crf = loader.decode_segmap(np.array(mask, dtype=np.uint8)) dcrf_path = args.out_path[:-4] + "_drf.png" cv2.imsave(dcrf_path, decoded_crf) print("Dense CRF Processed Mask Saved at: {}".format(dcrf_path)) pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=0) if model_name in ["pspnet", "icnet", "icnetBN"]: pred = pred.astype(np.float32) # float32 with F mode, resize back to orig_size pred = cv2.imresize(pred, orig_size, "nearest", mode="F") decoded = loader.decode_segmap(pred) print("Classes found: ", np.unique(pred)) #cv2.imwrite(args.out_path, decoded) print("Segmentation Mask Saved at: {}".format(args.out_path)) plt.imshow(decoded) return (decoded * 255).astype(int)
import cv2 cap = cv2.VideoCapture(0) flag, frame = cap.read() cv2.imshow('aaaaa', frame) cv2.imsave('aaa.jpg', frame)
img.putpixel((u, v), (0, 0, 0)) return img if __name__ == '__main__': frontRGBFolder = argv[1] rearRGBFolder = argv[2] frontSegFolder = argv[3] rearSegFolder = argv[4] front_H = np.load('front_H.npy') rear_H = np.load('rear_H.npy') for front, rear, fseg, rseg in zip(os.listdir(frontRGBFolder), os.listdir(rearRGBFolder), os.listdir(frontSegFolder), os.listdir(rearSegFolder)): front_road = getRoad(front, fseg) rear_road = getRoad(rear, rseg) ftop, rtop = getTop(front_road, rear_road, front_H, rear_H) cv2.imsave('TOP/front/' + str(i) + '.png', ftop) cv2.imsave('TOP/rear/' + str(i) + '.png', rtop) fig, ax = plt.subplots(1, 2) ax[0].imshow(ftop) ax[1].imshow(rtop) plt.pause(0.000001)
def test(): img = cv2.imread("./test_data/starry_night.jpg") ny = 300 nx = img.shape[1] * ny / img.shape[0] img = cv2.resize(img, (ny, nx)) cv2.imsave("./test_data/test/output.jpg", img)
import cv2 import os import os.path import shutil pathToSaveToPositive = "C:\\Fw%3a_loopback_files\\saveToPositiveFolder" pathToSaveToNegative = "C:\\Fw%3a_loopback_files\\saveToNegativeFolder" pathToSaveToPartial = "C:\\Fw%3a_loopback_files\\saveToPartialFolder" pathToImagesPositive = "C:\\Fw%3a_loopback_files\\images\\Positive-isCar" pathToImagesNegative = "C:\\Fw%3a_loopback_files\\images\\Negative-isNotCar" for image in imagesPositive: cv2.imread('im', image) cv2.imshow('im', image) getInput = input("Enter y for positive, n for negative, and p for partial") while (getInput != 'y' or getInput !='n' or getInput != 'h'): print "Not valid input. Try again" getInput = input("Enter y for positive, n for negative, and p for partial") if getInput = 'y': cv2.imsave(pathToSaveToPositive) else if getInput='n': cv2.imsave(pathToSaveToNegative) else: cv2.imsave(pathToSaveToPartial)
# multiplicação elemental do kernel e da imagem saida[y, x] = (k * img_padded[y:y + 3, x:x + 3]).sum() return saida def cmatri(linhas, colunas): matriz = [] for i in range(linhas): linha = [] for j in range(colunas): v = int(input("Digite o elemento [" + str(i) + "][" + str(j) + "]")) linha.append(v) matriz.append(linha) return matriz def lmatri(): lin = int(input("Insira o número de linhas: ")) col = int(input("Insira o número de colunas: ")) return cmatri(lin, col) img = cv.imread('Fig0417(a)(barbara).tif', 0) # Carrega a imagem cv.imshow('imagem original', img) # Convolve o kernel de nitidez e a imagem k = np.array([lmatri()]) imgc = convol2d(img, k) cv.imsave('com filtro', imgc)
import glob import cv2 glob_path = '../resources/face_224x224/*/ok/*.jpg' image_path_list = glob.glob(glob_path) for i, image_path in enumerate(image_path_list): if i % 100 == 0: print(i) image = cv2.imread(image_path) resized_image = cv2.resize(image, (224, 224)) cv2.imsave(image_path, resized_image)
import cv2 im = cv2.imread("<IMAGE FILE PATH GOES HERE>") cv2.imshow("Original Image", im) #Accessing pixels: do not approach image manipulation this way! #Use vectorized operations with ROIs pixel = im[100,100] print(pixel) #ROI accessing im[100:150,100:150] = [255,255,255] cv2.imshow("ROI", im) #Image attributes print(im.shape) print(im.dtype) cv2.imsave("<OUTPUT FILE PATH GOES HERE>", im) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): save_size = 256 frames_per_vid = 60 batch_size = 32 save_dir = "cropped_samples" dataset_path = "SiW/SiW_release/Train" if(os.path.exists(save_dir)): os.mkdir(save_dir) face_list = [] label_list = [] for folder in os.listdir(dataset_path): # subject folders # process all videos in subject folder for video in get_videos(os.path.join(dataset_path, folder)): dataset_name = video.split("/")[-1].split(".")[0] if(dataset_name.split("-")[2] == "1"): video_label = [1, 0] else: video_label = [0, 1] #[live, spoofed] face_points = open(video[:-3]+"face", "r") vidcap = cv2.VideoCapture(video) success, frame = vidcap.read() count = 0 while(count < frames_per_vid and success): try: assert(len(frame.shape) == 3) assert(frame.shape[2] == 3) frame = crop_image(frame, parse_line(face_points.readline())) frame = cv2.resize(frame, (save_size, save_size)) if(count % 27 == 18): cv2.imsave(os.path.join(save_dir, dataset_name+"_frame"+str(count)),frame) #cv2.imshow('image',frame) #cv2.waitKey(27) # add frame channels to face_list for channel in range(3): face_list.append(frame[:,:,channel]) label_list.append(video_label) if(len(face_list) >= batch_size): h5py_img_file = h5py.File("none.h5", 'a') h5py_label_file = h5py.File("labels.h5", 'a') h5py_img_file.create_dataset(dataset_name+str(count), data=np.asarray(face_list)) h5py_label_file.create_dataset(dataset_name+str(count), data=np.asarray(label_list)) h5py_img_file.close() h5py_label_file.close() face_list = [] label_list = [] except AssertionError: print("could not process frame " +str(count)+" in video "+dataset_name) except Exception as e: raise e finally: success, frame = vidcap.read() count += 1 print(video) if(len(face_list) > 0): h5py_img_file = h5py.File("none.h5", 'a') h5py_label_file = h5py.File("labels.h5", 'a') h5py_img_file.create_dataset(dataset_name+str(count), data=np.asarray(face_list)) h5py_label_file.create_dataset(dataset_name+str(count), data=np.asarray(label_list)) h5py_img_file.close() h5py_label_file.close()
data_dir = '/Users/tornadomeet/project/dmlc/mxNet/example/image-classification/data/' train = mx.io.MNISTIter( image = data_dir + "train-images-idx3-ubyte", label = data_dir + "train-labels-idx1-ubyte", input_shape = data_shape[1:], batch_size = batch_size, shuffle = True) metric_acc = mx.metric.CustomMetric(ferr) for epoch in range(num_epoch): train.reset() metric_acc.reset() for t, batch in enumerate(train): gmod.update(batch) gmod.temp_label[:] = 0.0 metric_acc.update([gmod.temp_label], gmod.outputs_fake) gmod.temp_label[:] = 1.0 metric_acc.update([gmod.temp_label], gmod.outputs_real) if t % 100 == 0: logging.info("epoch: %d, iter %d, metric=%s", epoch, t, metric_acc.get()) gdata = gmod.temp_outG[0].asnumpy() viz.imshow("gout", gdata, 2) diff = gmod.temp_diffD[0].asnumpy() diff = (diff - diff.mean()) / diff.std() + 0.5 viz.imshow("diff", diff) viz.imshow("data", batch.data[0].asnumpy(), 2) if epoch == num_epoch -1: cv2.imsave("epcho-{}-iter-{}".format(epoch, t), gdata)
def compare(source, targets): org = cv2.imread(source) for i, img_path in enumerate(targets): img_to_compare = cv2.imread(img_path) org = org[88:682, 508:843] img_to_compare = img_to_compare[88:682, 508:843] cv2.imshow('Main', org) cv2.imshow('test', img_to_compare) same_image = False if org.shape == img_to_compare.shape: diff = cv2.subtract(img_to_compare, org) b, g, r = cv2.split(diff) if cv2.countNonZero(b) == 0 and cv2.countNonZero( g) == 0 and cv2.countNonZero(r): same_image = True if same_image: print('same image') sys.exit() sift = cv2.xfeatures2d.SIFT_create() # keypoints and descriptors kp_1, desc_1 = sift.detectAndCompute(org, None) kp_2, desc_2 = sift.detectAndCompute(img_to_compare, None) print("Key Points IMG1", len(kp_1)) print("Key Points IMG2", len(kp_2)) index_params = dict(algorithm=0, trees=5) search_params = dict() flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(desc_1, desc_2, k=2) good_points = [] for m, n in matches: if m.distance < 0.4 * n.distance: good_points.append(m) print("Good Matches", len(good_points)) number_keypoints = min(len(kp_1), len(kp_2)) percent = (len(good_points) / number_keypoints) * 100 print("How good is the match: ", round(percent), "%") result = cv2.drawMatches(org, kp_1, img_to_compare, kp_2, good_points, None, flags=2) cv2.imsave('./result-images/Matched' + img_path, result) new_row = {'name': img_path, 'percentage': percent} df.append(new_row, ignore_index=True)
# 15 = 8,0.5 # 7 = 7,2 # 3 = 5,2 filter1 = np.array(build_filters(31, 8, 0.5)) filter2 = np.array(build_filters(15, 8, 0.5)) filter3 = np.array(build_filters(7, 7, 2)) filter4 = np.array(build_filters(3, 4, 3)) # Custom filter / kernel l1_filter = np.zeros((6, 3, 3)) # edge filter l1_filter[0, :, :] = np.array([[[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]]) l1_filter[1, :, :] = np.array([[[1, 1, 1], [0, 0, 0], [-1, -1, -1]]]) # left sobel l1_filter[2, :, :] = np.array([[[1, 0, -1], [2, 0, -2], [1, 0, -1]]]) # right sobel l1_filter[3, :, :] = np.array([[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]]) # top sobel l1_filter[4, :, :] = np.array([[[1, 2, 1], [0, 0, 0], [-1, -2, -1]]]) # bottom sobel l1_filter[5, :, :] = np.array([[[-1, -2, -1], [0, 0, 0], [1, 2, 1]]]) if __name__ == "__main__": for ind, f in enumerate(filter4): print(np.amax(f), np.amin(f)) cv2.imshow('pic', f) cv2.waitKey(1000) cv2.imwrite('img_name' + str(ind) + '.jpg', f * 255) f = np.ones((31, 31)) cv2.imsave('pic', f) cv2.waitKey(1000) exit()