def normalize(image): # This method normalizes value of each pixel, converts image to unsigned int # to get only 256 possible pixel values and then normalizes values to range between 0-1 image = image.astype(np.float64) image /= np.max(image) image *= 255 image = image.astype(np.uint8) image = image.astype(np.float32) image /= 255 return image
def scale_between_0_255(image): """ Utility function to scale all pixel values of an integer to be between 0 and 255. Returns the scaled image. """ image = 255 * (image - image.min()) / (image.max() - image.min()) image = image.astype('uint8') return image
def main(request): if request.method == 'POST': form = form_canvas(request.POST) tim = request.POST.get('im') if form.is_valid(): data = base64.b64decode(tim) print(type(data)) print( '***************************************************************' ) img0 = "ml_app/test.bmp" with open(img0, 'wb') as f: f.write(data) f.close o = count(name=img0) o.save() print(type(img0)) print('+++++++++++++++++++++++++++++++++++') # to save image in different files ''' img1 = Image.open(img0) idi = o.id a = "C:/Users/Yzat/Downloads/ML_Django/Credit_Approval/Credit_project/media/" adr = a + str(idi) + '.bmp' img1.save(adr) ''' #################### ML Model ################################## new_model = tf.keras.models.load_model('ml_app/yzat.h5') img = cv2.imread('ml_app/test.bmp', -1) b, g, r, alpha = cv2.split(img) img_BGR = cv2.merge((r, g, alpha)) image = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2GRAY) image = cv2.resize(image, (28, 28)) image = image.astype('float32') image = image.reshape(1, 28, 28, 1) image /= 255 predictions = new_model.predict(image) yzat_predictions = np.argmax(predictions) print('******************', yzat_predictions, '**********************') messages.success(request, '{}'.format(yzat_predictions)) #################### ML Model End #################################### return HttpResponseRedirect('/canvas') else: return HttpResponseRedirect('/canvas') else: return render(request, 'canvas.html')
def pcaProcess(self, img: np.array, dim=15): img = img.astype(float) u, s, vt = np.linalg.svd(img) # 取前dim 维的特征向量(比如(72, 68)-> (68, 68)的cov, 对应特征矩阵为(68, dim)的shape) # 此处的sample操作是图像绘制时使用的,没有达到实际的压缩保存效果 # sample = u[:, :dim].dot(np.diag(s[:dim])).dot(vt[:dim, :]) # 此操作才是真正进行压缩(PCA) # sample = u[:, :dim].dot(np.diag(s[:dim])) sample = img.dot(u[:, :dim]) # 将一个dim * dim 的矩阵reshape成一个行向量 return sample.reshape(sample.size)
def load_using_bioformats(path, z=0, t=0): '''Load the given image file using the Bioformats library path: path to the file z: the frame index in the z (depth) dimension. t: the frame index in the time dimension. Returns either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image ''' rdr = ImageReader() rdr.setId(path) width = rdr.getSizeX() height = rdr.getSizeY() pixel_type = rdr.getPixelType() little_endian = rdr.isLittleEndian() if pixel_type == FormatTools.INT8: dtype = np.char scale = 255 elif pixel_type == FormatTools.UINT8: dtype = np.uint8 scale = 255 elif pixel_type == FormatTools.UINT16: dtype = '<u2' if little_endian else '>u2' scale = 65536 elif pixel_type == FormatTools.INT16: dtype = '<i2' if little_endian else '>i2' scale = 65536 elif pixel_type == FormatTools.UINT32: dtype = '<u4' if little_endian else '>u4' scale = 2**32 elif pixel_type == FormatTools.INT32: dtype = '<i4' if little_endian else '>i4' scale = 2**32 elif pixel_type == FormatTools.FLOAT: dtype = '<f4' if little_endian else '>f4' scale = 1 elif pixel_type == FormatTools.DOUBLE: dtype = '<f8' if little_endian else '>f8' scale = 1 max_sample_value = rdr.getMetadataValue('MaxSampleValue') if max_sample_value is not None: try: scale = formatreader.jutil.call(env, max_sample_value, 'intValue', '()I') except: sys.stderr.write("WARNING: failed to get MaxSampleValue for image. Intensities may be improperly scaled\n") if rdr.getRGBChannelCount() > 1: rdr.close() rdr = ChannelSeparator(ImageReader()) rdr.setId(path) red_image, green_image, blue_image = [ np.frombuffer(rdr.openBytes(rdr.getIndex(z,i,t)),dtype) for i in range(3)] image = np.dstack((red_image, green_image, blue_image)) image.shape=(height,width,3) else: index = rdr.getIndex(z,0,t) image = np.frombuffer(rdr.openBytes(index),dtype) image.shape = (height,width) rdr.close() image = image.astype(float) / float(scale) return image
def load_using_bioformats(path, z=0, t=0): '''Load the given image file using the Bioformats library path: path to the file z: the frame index in the z (depth) dimension. t: the frame index in the time dimension. Returns either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image ''' rdr = ImageReader() rdr.setId(path) width = rdr.getSizeX() height = rdr.getSizeY() pixel_type = rdr.getPixelType() little_endian = rdr.isLittleEndian() if pixel_type == FormatTools.INT8: dtype = np.char scale = 255 elif pixel_type == FormatTools.UINT8: dtype = np.uint8 scale = 255 elif pixel_type == FormatTools.UINT16: dtype = '<u2' if little_endian else '>u2' scale = 65536 elif pixel_type == FormatTools.INT16: dtype = '<i2' if little_endian else '>i2' scale = 65536 elif pixel_type == FormatTools.UINT32: dtype = '<u4' if little_endian else '>u4' scale = 2**32 elif pixel_type == FormatTools.INT32: dtype = '<i4' if little_endian else '>i4' scale = 2**32 elif pixel_type == FormatTools.FLOAT: dtype = '<f4' if little_endian else '>f4' scale = 1 elif pixel_type == FormatTools.DOUBLE: dtype = '<f8' if little_endian else '>f8' scale = 1 max_sample_value = rdr.getMetadataValue('MaxSampleValue') if max_sample_value is not None: try: scale = formatreader.jutil.call(env, max_sample_value, 'intValue', '()I') except: sys.stderr.write( "WARNING: failed to get MaxSampleValue for image. Intensities may be improperly scaled\n" ) if rdr.getRGBChannelCount() > 1: rdr.close() rdr = ChannelSeparator(ImageReader()) rdr.setId(path) red_image, green_image, blue_image = [ np.frombuffer(rdr.openBytes(rdr.getIndex(z, i, t)), dtype) for i in range(3) ] image = np.dstack((red_image, green_image, blue_image)) image.shape = (height, width, 3) else: index = rdr.getIndex(z, 0, t) image = np.frombuffer(rdr.openBytes(index), dtype) image.shape = (height, width) rdr.close() image = image.astype(float) / float(scale) return image
def main(argv=None): train_para = { 'lr': [1e-4, 1e-5, 1e-6], 'lr_iter': [10000, 20000], 'max_iter': 30000, 'show_loss_freq': 1000, 'snapshot_freq': 5000, 'snapshot_dir': 'snapshots_posenet' } # Start TF gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) tf.train.start_queue_runners(sess=sess) # get dataset dataset_GANerate = GANerate(batchnum=32) image_crop_eval, keypoint_uv21_eval, keypoint_uv_heatmap_eval, keypoint_xyz21_normed_eval = dataset_GANerate.get_batch_data_eval # build network evaluation = tf.placeholder_with_default(True, shape=()) net = ColorHandPose3DNetwork() image_crop_eval = tf.add(image_crop_eval, 0, name='input_node_representations') keypoints_scoremap_eval = net.inference_pose2d(image_crop_eval, train=True) s = keypoint_uv_heatmap_eval.get_shape().as_list() keypoints_scoremap_eval = [ tf.image.resize_images(x, (s[1], s[2])) for x in keypoints_scoremap_eval ] # Loss loss_eval = 0.0 for i, pred_item in enumerate(keypoints_scoremap_eval): loss_eval += tf.reduce_sum( tf.sqrt( tf.reduce_mean(tf.square(pred_item - keypoint_uv_heatmap_eval), [1, 2]))) keypoints_scoremap_eval = keypoints_scoremap_eval[-1] keypoints_scoremap_eval = tf.add(keypoints_scoremap_eval, 0, name='final_output_node_representations') init = tf.global_variables_initializer() config = tf.ConfigProto() # occupy gpu gracefully config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: init.run() checkpoint_path = './snapshots_posenet' model_name = 'model-42' if checkpoint_path: saver = tf.train.Saver(max_to_keep=10) saver.restore(sess, checkpoint_path + '/' + model_name) print("restore from " + checkpoint_path + '/' + model_name) create_pb = True if create_pb: input_graph_def = sess.graph.as_graph_def() variable_names = [v.name for v in input_graph_def.node] print( '==================Model Analysis Report variable_names======================' ) print(variable_names) print( '==================Model Analysis Report operations======================' ) for op in sess.graph.get_operations(): print(str(op.name)) stats_graph(sess.graph) output_graph_def = tf.graph_util.convert_variables_to_constants( sess, # The session input_graph_def, # input_graph_def is useful for retrieving the nodes 'final_output_node_representations'.split(",")) with tf.gfile.FastGFile(checkpoint_path + '/' + model_name + ".pb", "wb") as f: f.write(output_graph_def.SerializeToString()) print("Start testing...") path = './snapshots_posenet/baseline' import matplotlib.image loss_eval_v = 0.0 loss_piex_save = 0.0 for one_epoch in tqdm(range(100)): image, heatmap, heatmap_pre, keypoint_uv21, loss_eval_v = sess.run( [ image_crop_eval, keypoint_uv_heatmap_eval, keypoints_scoremap_eval, keypoint_uv21_eval, loss_eval ]) image = (image + 0.5) * 255 image = image.astype(np.int16) #根据热度图计算最大的下标 keypoint_uv21_pre = np.zeros_like(keypoint_uv21) for i in range(heatmap_pre.shape[0]): for j in range(heatmap_pre.shape[-1]): heatmap_pre_tmp = heatmap_pre[i, :, :, j] cor_tmp = unravel_index(heatmap_pre_tmp.argmax(), heatmap_pre_tmp.shape) keypoint_uv21_pre[i, j, 0] = cor_tmp[1] keypoint_uv21_pre[i, j, 1] = cor_tmp[0] loss_piex = keypoint_uv21_pre - keypoint_uv21 loss_piex = np.sqrt( np.square(loss_piex[:, :, 0]) + np.square(loss_piex[:, :, 1])) loss_piex_save = loss_piex_save + np.mean(loss_piex) # visualize fig = plt.figure(1) plt.clf() ax1 = fig.add_subplot(221) ax1.imshow(image[0]) plot_hand(keypoint_uv21[0], ax1) ax3 = fig.add_subplot(223) ax3.imshow(image[0]) ax3.set_title(str(loss_piex[0, :].astype(np.int32)), fontsize=5) plot_hand(keypoint_uv21_pre[0], ax3) plot_hand(keypoint_uv21[0], ax3) ax2 = fig.add_subplot(222) ax4 = fig.add_subplot(224) ax2.imshow(np.sum(heatmap[0], axis=-1)) # 第一个batch的维度 hand1(0~31) back1(32~63) ax2.scatter(keypoint_uv21[0, :, 0], keypoint_uv21[0, :, 1], s=10, c='k', marker='.') ax4.imshow(np.sum(heatmap_pre[0], axis=-1)) # 第一个batch的维度 hand1(0~31) back1(32~63) ax4.scatter(keypoint_uv21_pre[0, :, 0], keypoint_uv21_pre[0, :, 1], s=10, c='k', marker='.') plt.savefig(path + '/image/' + str(one_epoch).zfill(5) + '.png') loss_eval_v = loss_eval_v / 100 loss_piex_save = loss_piex_save / 100 print(loss_piex_save) #4.472415127649567
def standardPCA(self, img: np.array): img = img.astype(float) sample = self.pca.fit_transform(img) return sample.reshape(sample.size)
if (new_img[i, j] == 25): x = np.sum(new_img[i:i + 3, j:j + 3] * kernel) if (x >= 255): new_img2[i, j] = 255 elif (new_img[i, j] == 255): new_img2[i, j] = 255 return new_img2 img = gray_me(img) # smoothning kernel = np.array([[1 / 16, 2 / 16, 1 / 16], [1 / 8, 1 / 4, 1 / 8], [1 / 16, 2 / 16, 1 / 16]]) img = apply_kernel(kernel, img) #vertical kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) imgy = apply_kernel(kernel, img) #horizontal kernel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) imgx = apply_kernel(kernel, img) ##SOBEL img = np.sqrt(np.square(imgx) + np.square(imgy)) ##canny # theta=np.arctan2(imgy,imgx) # img=suppress_nonmax(img,theta) # img=threshold(img) img = img.astype(np.uint8) po = im.fromarray(imgy) # po.save("horizontal.png") im._show(po)
def __call__(self, sample): image, fixation = sample['image'], sample['fixation'] image = image.astype(np.float32) / 255.0 fixation = fixation.astype(np.float32) / 255.0 return {'image': image, 'fixation': fixation}