Example #1
1
def image_prossesing(input,output,kappa,iters):
	data=img_nump(input)

	if len(data.shape) == 2:
		print "Black and white image"
		data=iso_diffusion_denoising(data,kappa,iters)
		print data
		Image.fromarray(data.astype('uint8')).save("disaster_after.jpg")


	else:
		print "RGB picture"
		HSI=RGBtoHSI(data)
		print HSI[200,336,:]
		for i in xrange(0,2):
			HSI[:,:,i]=iso_diffusion_denoising(HSI[:,:,i],kappa,iters)
		RGB=HSItoRGB(HSI)


		print RGB[200,336,:]

		print HSI[200,336,:]
		print data[200,336,:]

		Image.fromarray(RGB.astype('uint8')).save("disaster_after.jpg")
def manualPhasePlot(function, ranges, granularities):
    params = Params()
    xSpan = ranges[0][1]-ranges[0][0]
    ySpan = ranges[1][1] - ranges[1][0]
    xTrend = int(xSpan/granularities[0])
    yTrend = int(ySpan/granularities[1])
    potentialPhasePlot=[]
    timePhasePlot = []
    for y in range(0,yTrend):
        potentialRow = []
        timeRow = []
        usefulY = ranges[1][0] + y*granularities[1]
        for x in range(0,xTrend):
            usefulX = ranges[0][0]+x*granularities[0]
            stats = function(params, usefulX, usefulY)
            potentialRow.append(stats[0])
            timeRow.append(stats[1])
        potentialPhasePlot.append(potentialRow)
        timePhasePlot.append(timeRow)
    potentialPhasePlot = np.array(potentialPhasePlot)
    timePhasePlot = np.array(timePhasePlot)
    normalize(potentialPhasePlot)
    normalize(timePhasePlot)
    #Normalizing
    potImage = Image.fromarray(potentialPhasePlot,'L')
    potImage.save("potentialPhasePlot.png")
    timeImage = Image.fromarray(timePhasePlot, 'L')
    timeImage.save("timePhasePlot.png")
Example #3
0
def imageStructToPIL(imageRow):
    """
    Convert the immage from image schema struct to PIL image

    :param imageRow: Row, must have ImageSchema
    :return PIL image
    """
    imgType = imageTypeByOrdinal(imageRow.mode)
    if imgType.dtype != 'uint8':
        raise ValueError("Can not convert image of type " +
                         imgType.dtype + " to PIL, can only deal with 8U format")
    ary = imageStructToArray(imageRow)
    # PIL expects RGB order, image schema is BGR
    # => we need to flip the order unless there is only one channel
    if imgType.nChannels != 1:
        ary = _reverseChannels(ary)
    if imgType.nChannels == 1:
        return Image.fromarray(obj=ary, mode='L')
    elif imgType.nChannels == 3:
        return Image.fromarray(obj=ary, mode='RGB')
    elif imgType.nChannels == 4:
        return Image.fromarray(obj=ary, mode='RGBA')
    else:
        raise ValueError("don't know how to convert " +
                         imgType.name + " to PIL")
Example #4
0
 def predextra(self, seedimgpathlist, outputcount=0, show=True, savedir=None):
     u'''種となる画像のseqを受け取って画像列を保存'''
     if outputcount <= 0: outputcount = self.arg['timesteplen']
     if savedir is not None:
         if not os.path.exists(savedir): os.mkdir(savedir)
         if not savedir.endswith('/'): savedir += '/'
     self.resetstate()
     factor = 1.0*self.imgshape[0]*self.imgshape[1]*self.imgshape[2]
     print 'sum E[0] > 0.0:', (self.E[0]>0.0).sum()/factor, ', sum E[0]:', self.E[0].sum()/factor
     count = 0
     for p in seedimgpathlist:
         arr = self.applyimg(p)
         img = Image.fromarray(arr)
         if savedir: img.save(savedir + str(count) + '.png')
         if show: img.show()
         count += 1
         print '%d / %d done'%(count, outputcount), 'sum E[0] > 0.0:', (self.E[0]>0.0).sum()/factor, ', sum E[0]:', self.E[0].sum()/factor
         if count >= outputcount: break
     
     for i in range(0, outputcount - count):
         arr = self.applyimg(arr)
         img = Image.fromarray(arr)
         if savedir: img.save(savedir + str(count) + '.png')
         if show: img.show()
         count += 1
         print '%d / %d done'%(count, outputcount), 'sum E[0] > 0.0:', (self.E[0]>0.0).sum()/factor, ', sum E[0]:', self.E[0].sum()/factor
Example #5
0
File: pil.py Project: APSL/thumbor
    def convertImagesToPIL(self, images, dither, nq=0):
        """ convertImagesToPIL(images, nq=0)

        Convert images to Paletted PIL images, which can then be
        written to a single animaged GIF.

        """

        # Convert to PIL images
        images2 = []
        for im in images:
            if isinstance(im, Image.Image):
                images2.append(im)
            elif np and isinstance(im, np.ndarray):
                if im.ndim == 3 and im.shape[2] == 3:
                    im = Image.fromarray(im, 'RGB')
                elif im.ndim == 3 and im.shape[2] == 4:
                    im = Image.fromarray(im[:, :, :3], 'RGB')
                elif im.ndim == 2:
                    im = Image.fromarray(im, 'L')
                images2.append(im)

        # Convert to paletted PIL images
        images, images2 = images2, []

        # Adaptive PIL algorithm
        AD = Image.ADAPTIVE
        for im in images:
            im = im.convert('P', palette=AD, dither=dither)
            images2.append(im)

        # Done
        return images2
Example #6
0
    def array_to_stimulus(self, array_or_image, spatial_extend, starting_point, velocity):
        if type(array_or_image) == np.ndarray:
            if self.intensity_dim == 1:
                try:
                    im = Image.fromarray(array_or_image[:, :, 0] * 255)
                except:
                    im = Image.fromarray(array_or_image * 255)
            else:
                im = Image.fromarray(array_or_image * 255, "RGB")
        else:
            im = array_or_image

        px_spatial_extend = [
            int(spatial_extend[0] * self.intensities.shape[0]),
            int(spatial_extend[1] * self.intensities.shape[1]),
        ]
        px_start = [
            int(starting_point[0] * self.intensities.shape[0]),
            int(starting_point[1] * self.intensities.shape[1]),
        ]
        px_velocity = [int(velocity[0] * self.intensities.shape[0]), int(velocity[1] * self.intensities.shape[1])]

        stim = stimulus.ImgStimulus(self.intensity_dim, im, px_spatial_extend, px_start, px_velocity)

        self.stimuli.append(stim)
def rotate_training():
    ## Saves rotated versions of each image in the training set
    niter = 10
    k=5
    for i in np.linspace(1,100,100):

        truth = mpimg.imread('training/groundtruth/satImage_'+ '%.3d' % i  +'.png')
        
        image = mpimg.imread('training/images/satImage_'+ '%.3d' % i  +'.png')
        

        
        imgs = mk_rotations(image)
        truths = mk_rotations(truth)

        count =0
        for im in imgs:
            im = format_image(im)
            Image.fromarray(im).save('training_big/Images/satImage_'+ '%.3d' % i  +'_rota'+str(np.int(count))+'.png')
            count+=1
        count =0
        for im in imgs:
            im = format_image(im)
            Image.fromarray(im).save('training_big/Truth/satImage_'+ '%.3d' % i  +'_rota'+str(np.int(count))+'.png')
            count+=1


        print('Writing image ',i)
    return 0
Example #8
0
    def save(self, filename):
        image = numpy.zeros((self.width, self.height, 3), 'uint8')
        for x, row in self.colors.iteritems():
            for y, color in row.iteritems():
                image[x, y, :] = color.get_24bit_tuple()

        Image.fromarray(image).save(filename)
Example #9
0
  def vtkImageDataToPNG(self,imageData,method='VTK'):
    """Return a buffer of png data using the data
    from the vtkImageData.
    """
    if method == 'PIL':
      if imageData:
        imageData.Update()
        imageScalars = imageData.GetPointData().GetScalars()
        imageArray = vtk.util.numpy_support.vtk_to_numpy(imageScalars)
        d = imageData.GetDimensions()
        im = Image.fromarray( numpy.flipud( imageArray.reshape([d[1],d[0],4]) ) )
      else:
        # no data available, make a small black opaque image
        a = numpy.zeros(100*100*4, dtype='uint8').reshape([100,100,4])
        a[:,:,3] = 255
        im = Image.fromarray( a )
      if size:
        im.thumbnail((size,size), Image.ANTIALIAS)
      pngStringIO = StringIO.StringIO()
      im.save(pngStringIO, format="PNG")
      pngData = pngStringIO.getvalue()
    elif method == 'VTK':
      writer = vtk.vtkPNGWriter()
      writer.SetWriteToMemory(True)
      writer.SetInput(imageData)
      writer.Write()
      result = writer.GetResult()
      pngArray = vtk.util.numpy_support.vtk_to_numpy(result)
      pngStringIO = StringIO.StringIO()
      pngStringIO.write(pngArray)
      pngData = pngStringIO.getvalue()

    return pngData
Example #10
0
def overlayOnPart(src_image, overlay_image, posX, posY):

    # オーバレイ画像のサイズを取得
    ol_height, ol_width = overlay_image.shape[:2]

    # OpenCVの画像データをPILに変換
    # BGRAからRGBAへ変換
    src_image_RGBA = cv2.cvtColor(src_image, cv2.COLOR_BGR2RGB)
    overlay_image_RGBA = cv2.cvtColor(overlay_image, cv2.COLOR_BGRA2RGBA)
    
    # PILに変換
    src_image_PIL=Image.fromarray(src_image_RGBA)
    overlay_image_PIL=Image.fromarray(overlay_image_RGBA)

    # 合成のため、RGBAモードに変更
    src_image_PIL = src_image_PIL.convert('RGBA')
    overlay_image_PIL = overlay_image_PIL.convert('RGBA')

    # 同じ大きさの透過キャンパスを用意
    tmp = Image.new('RGBA', src_image_PIL.size, (255, 255,255, 0))
    # 用意したキャンパスに上書き
    tmp.paste(overlay_image_PIL, (posX, posY), overlay_image_PIL)
    # オリジナルとキャンパスを合成して保存
    result = Image.alpha_composite(src_image_PIL, tmp)
    
    # COLOR_RGBA2BGRA から COLOR_RGBA2BGRに変更。アルファチャンネルを含んでいるとうまく動画に出力されない。
    return  cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGR)
Example #11
0
	def exportAsPNG(self):
		if self.mode != 0:
			TFtable=self.genTFtable()

			imInput=Image.open(str(self.input_image))
			input_data=np.asarray(imInput,dtype=np.uint8)
			if self.mode==1:
				if len(input_data[0,0])==3:
					# print 'choose 000'
					output_data=np.asarray([[[0,0,0] for i in range(graphX)] for j in range(graphY)], dtype=np.uint8)
				elif len(input_data[0,0])==4:
					# print 'choose 0000'
					output_data=np.asarray([[[0,0,0,255] for i in range(graphX)] for j in range(graphY)], dtype=np.uint8)
			else:
				output_data=copy.copy(input_data)

			for i in range(int(self.minX),int(self.maxX)):
				for j in range(int(self.minY),int(self.maxY)):
					if self.mode==1:
						if TFtable[j,i]:
							# print input_data[j,i],output_data[j,i]
							output_data[j,i]=copy.copy(input_data[j,i])
					elif self.mode==2:
						if not TFtable[j,i]:
							output_data[j,i]=[0,0,0]
			if len(input_data[0,0])==3:
				imOutput=Image.fromarray(output_data,mode='RGB')
			elif len(input_data[0,0])==4:
				imOutput=Image.fromarray(output_data,mode='RGBA')

			out_image=QFileDialog.getSaveFileName(None,"Save PNG Image","/home/lkit/Pictures",
				"Image Files (*.bmp *.jpg *.png *.xpm)")
			imOutput.save(str(out_image))
		else:
			self.warningMsg("Please choose select/mask mode")
Example #12
0
 def save_image(self, directory = MOVIE_DIR, name = None):
     path = os.path.join(directory, "images")
     file_name = (name or str(self)) + ".png"
     full_path = os.path.join(path, file_name)
     if not os.path.exists(path):
         os.makedirs(path)
     Image.fromarray(self.get_frame()).save(full_path)
def main(args):
    
    in_db = lmdb.open(dataset_name, map_size=int(1e12))
    with in_db.begin(write=True) as in_txn:
        for in_idx, in_ in enumerate(open(file_with_paths)):
            print 'img: ' + str(in_idx)
            
            # load image:
            im = np.array(Image.open(in_.rstrip()))
            # save type
            Dtype = im.dtype
            
            if labels:
                # Resize the input image
                Limg = Image.fromarray(im)
                Limg = Limg.resize([H, W],Image.NEAREST)
                im = np.array(Limg,Dtype)
                # Convert from HxWxC (C=3) to HxWxC (C=1)
                im = im.reshape(im.shape[0],im.shape[1],1)
            else:
                # RGB to BGR
                im = im[:,:,::-1]
                im = Image.fromarray(im)
                im = im.resize([H, W], Image.ANTIALIAS)
                im = np.array(im,Dtype)
            
            # Convert to CxHxW
            im = im.transpose((2,0,1))
            if labels:
                im[im==255]=number_of_classes
            
            # Create the dataset
            im_dat = caffe.io.array_to_datum(im)
            in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
    in_db.close()
Example #14
0
def save_image_clipping(tile, status):
    """Save a tile of training data to disk to visualize."""
    rgbir_matrix = tile[0]
    tile_height = len(rgbir_matrix)

    r_img = numpy.empty([tile_height, tile_height])
    for x in range(len(rgbir_matrix)):
        for y in range(len(rgbir_matrix[x])):
            r_img[x][y] = rgbir_matrix[x][y][0]

    g_img = numpy.empty([tile_height, tile_height])
    for x in range(len(rgbir_matrix)):
        for y in range(len(rgbir_matrix[x])):
            if len(rgbir_matrix[x][y]) > 1:
                g_img[x][y] = rgbir_matrix[x][y][1]
            else:
                g_img[x][y] = rgbir_matrix[x][y][0]

    b_img = numpy.empty([tile_height, tile_height])
    for x in range(len(rgbir_matrix)):
        for y in range(len(rgbir_matrix[x])):
            if len(rgbir_matrix[x][y]) > 2:
                b_img[x][y] = rgbir_matrix[x][y][2]
            else:
                b_img[x][y] = rgbir_matrix[x][y][0]

    im = Image.merge('RGB',
                     (Image.fromarray(r_img).convert('L'), Image.fromarray(g_img).convert('L'),
                      Image.fromarray(b_img).convert('L')))
    outfile_path = tile[2] + '-' + status + '-' + str(tile[1][0]) + ',' + str(tile[1][
        1]) + '-' + '.jpg'
    im.save(outfile_path, "JPEG")
Example #15
0
def pred():
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")
    image = tf.placeholder(tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], name="input_image")
    annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 1], name="annotation")

    pred_annotation, logits = inference(image, keep_probability)
    test_dataset_reader = TestDataset('data/testlist.mat')
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
        saver = tf.train.Saver()
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("Model restored...")
        itr = 0
        test_images, test_annotations, test_orgs = test_dataset_reader.next_batch()
        #print('getting', test_annotations[0, 200:210, 200:210])
        if len(test_annotations) > 0:
            feed_dict = {image: test_images, annotation: test_annotations, keep_probability: 0.5}
            preds = sess.run(pred_annotation, feed_dict=feed_dict)
            org0_im = Image.fromarray(np.uint8(test_orgs[0]))
            org0_im.save('res/org0.jpg')
            org1_im = Image.fromarray(np.uint8(test_orgs[1]))
            org1_im.save('res/org1.jpg')
            save_alpha_img(test_orgs[0], test_annotations[0], 'res/ann0')
            save_alpha_img(test_orgs[1], test_annotations[1], 'res/ann1')
            save_alpha_img(test_orgs[0], preds[0], 'res/pre0')
            save_alpha_img(test_orgs[1], preds[1], 'res/pre1')
Example #16
0
def get_fft(ifile=None,location=None):
    """ get the fft of an image
        FFT information from:
        http://stackoverflow.com/questions/2652415/fft-and-array-to-image-image-to-array-conversion
    """
    if ifile is None:
        ifile= "n20_bw_dots/n20_b_w_dots_0010.tif"
    if location is None:
        x = 424.66; y = 155.56; area = 125
        #x = 302.95; y = 221.87; area = 145
        #x = 386.27; y = 263.62; area = 141
        #x = 35.39; y = 305.92; area = 154
        location = x,y
    wdth = int(24 * np.sqrt(2))
    hght = wdth
    cropbox = map(int,(x - wdth/2., y - hght/2.,\
            x + wdth/2., y + hght/2.))
    i = Im.open(ifile)
    i = i.crop(cropbox)
    i.show()
    a = np.asarray(i)
    aa = np.gradient(a)
    b = np.fft.fft2(a)
    j = Im.fromarray(abs(b))
    ii = Im.fromarray(aa)
    if do_plots:
        ii.show()

    return b
Example #17
0
    def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            tuple: (image, target) where target is index of the target class.
        """
        if self.split == 'train':
            img_path, target_path = self.train_data[index], self.train_labels[index]
        elif self.split == 'val':
            img_path, target_path = self.val_data[index], self.val_labels[index]
        elif self.split == 'test':
            img_path, target_path = self.test_data[index], self.test_labels[index]

        # TODO: Make annotation grayscale so we don't need this hardcoded layer.
        try:
            img = cv2.imread(img_path)
        except:
            pdb.set_trace()
        target = cv2.imread(target_path)[:,:,2]
        
        img = cv2.resize(img, (self.im_size[1],self.im_size[2]))
        target = cv2.resize(target, (self.im_size[1], self.im_size[2]))
        target[target != 0] = 255
        
        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        img = Image.fromarray(img)
        target = Image.fromarray(target)

        if self.transform is not None:
            img = self.transform(img)
            target = self.transform(target)

        return img, target
Example #18
0
def overlayOnPart(src_image, overlay_image, posX, posY, zoom):

    # オーバレイ画像を倍率に合わせて拡大縮小
    resized_overlay_image = resize_image(overlay_image, zoom)

    # オーバレイ画像のサイズを取得
    ol_height, ol_width = resized_overlay_image.shape[:2]

    # OpenCVの画像データをPILに変換
    
    # BGRAからRGBAへ変換
    src_image_RGBA = cv2.cvtColor(src_image, cv2.COLOR_BGR2RGB)
    resized_overlay_image_RGBA = cv2.cvtColor(resized_overlay_image, cv2.COLOR_BGRA2RGBA)
    
    # PILに変換
    src_image_PIL=Image.fromarray(src_image_RGBA)
    resized_overlay_image_PIL=Image.fromarray(resized_overlay_image_RGBA)

    # 合成のため、RGBAモードに変更
    src_image_PIL = src_image_PIL.convert('RGBA')
    resized_overlay_image_PIL = resized_overlay_image_PIL.convert('RGBA')

    # 同じ大きさの透過キャンパスを用意
    tmp = Image.new('RGBA', src_image_PIL.size, (255, 255,255, 0))
    # rect[0]:x, rect[1]:y, rect[2]:width, rect[3]:height
    # 用意したキャンパスに上書き
    tmp.paste(resized_overlay_image_PIL, (int(posX-ol_height/2), int(posY-ol_width/2)), resized_overlay_image_PIL)
    # オリジナルとキャンパスを合成して保存
    result = Image.alpha_composite(src_image_PIL, tmp)

    return  cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGR)
def draw_mask_on_image_array(image, mask, color='red', alpha=0.4):
  """Draws mask on an image.

  Args:
    image: uint8 numpy array with shape (img_height, img_height, 3)
    mask: a uint8 numpy array of shape (img_height, img_height) with
      values between either 0 or 1.
    color: color to draw the keypoints with. Default is red.
    alpha: transparency value between 0 and 1. (default: 0.4)

  Raises:
    ValueError: On incorrect data type for image or masks.
  """
  if image.dtype != np.uint8:
    raise ValueError('`image` not of type np.uint8')
  if mask.dtype != np.uint8:
    raise ValueError('`mask` not of type np.uint8')
  if np.any(np.logical_and(mask != 1, mask != 0)):
    raise ValueError('`mask` elements should be in [0, 1]')
  if image.shape[:2] != mask.shape:
    raise ValueError('The image has spatial dimensions %s but the mask has '
                     'dimensions %s' % (image.shape[:2], mask.shape))
  rgb = ImageColor.getrgb(color)
  pil_image = Image.fromarray(image)

  solid_color = np.expand_dims(
      np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3])
  pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA')
  pil_mask = Image.fromarray(np.uint8(255.0*alpha*mask)).convert('L')
  pil_image = Image.composite(pil_solid_color, pil_image, pil_mask)
  np.copyto(image, np.array(pil_image.convert('RGB')))
Example #20
0
    def save_weights_to_grey_files(self, identifier):
        # save 4 sets of weights:

        #filters_hs
        def arrange_for_show(filters_hs,filters_hs_shape):
	    n_filters_hs_modules, n_filters_hs_per_modules, fcolors, n_filters_hs_rows, n_filters_hs_cols  = filters_hs_shape            
            filters_fs_for_show = filters_hs.reshape(
                       (n_filters_hs_modules*n_filters_hs_per_modules, 
                       fcolors,
                       n_filters_hs_rows,
                       n_filters_hs_cols))
            fn = theano.function([],filters_fs_for_show)
            rval = fn()
            return rval
        filters_fs_for_show = arrange_for_show(self.filters_hs, self.filters_hs_shape)
        Image.fromarray(
                       tile_conv_weights(
                       filters_fs_for_show,flip=False), 'L').save(
                'filters_hs_%s.png'%identifier)
   
        if self.conf['lambda_logdomain']:
            raise NotImplementedError()
        else:
	    conv_lambda_for_show = arrange_for_show(self.conv_lambda, self.filters_hs_shape) 	    
	    Image.fromarray(
                            tile_conv_weights(
                            conv_lambda_for_show,flip=False), 'L').save(
                    'conv_lambda_%s.png'%identifier)
Example #21
0
    def png(self, redBand, greenBand, blueBand, minRadiance=None, maxRadiance=None, fileName="/var/www/quick-look/tmp.png", alpha=True):
        "Turn three bands into an RGB(A) PNG picture for quick viewing."

        if minRadiance is None:
            minRadiance = min(self._geoPicture.picture[:,:,self._geoPicture.bands.index(redBand)].min(),
                              self._geoPicture.picture[:,:,self._geoPicture.bands.index(greenBand)].min(),
                              self._geoPicture.picture[:,:,self._geoPicture.bands.index(blueBand)].min())
        if maxRadiance is None:
            maxRadiance = max(self._geoPicture.picture[:,:,self._geoPicture.bands.index(redBand)].max(),
                              self._geoPicture.picture[:,:,self._geoPicture.bands.index(greenBand)].max(),
                              self._geoPicture.picture[:,:,self._geoPicture.bands.index(blueBand)].max())

        picture = (self._geoPicture.picture[:,:,(self._geoPicture.bands.index(redBand),
                                                 self._geoPicture.bands.index(greenBand),
                                                 self._geoPicture.bands.index(blueBand))] - minRadiance) * 255./(maxRadiance - minRadiance)
        numpy.maximum(picture, 0., picture)
        numpy.minimum(picture, 255., picture)
        picture = numpy.array(picture, dtype=numpy.uint8)

        if alpha:
            alphaBand = numpy.array(self.mask((redBand, greenBand, blueBand)), dtype=numpy.uint8) * 255
            image = Image.fromarray(numpy.dstack((picture[:,:,0], picture[:,:,1], picture[:,:,2], alphaBand)))
            image.save(fileName, "PNG", option="optimize")
        else:
            image = Image.fromarray(picture)
            image.save(fileName, "PNG", option="optimize")
Example #22
0
def simple_window():
  root = tk.Tk()
  
  image = np.zeros((128, 128, 3), dtype=np.uint8)
  image[32:96, 32:96, 0] = 255
  im = Image.fromarray(image)
  im = ImageTk.PhotoImage(im)
  
  image = np.zeros((128, 128, 3), dtype=np.uint8)
  image[32:96, 32:96, 1] = 255
  im2 = Image.fromarray(image)
  im2 = ImageTk.PhotoImage(im2)
  
  panel = tk.Label(root, image=im)
  
  def left_key(event):
    panel.configure(image=im2)
    panel.image = im2

  def quit(event):
    sys.exit()

  panel.bind('<Left>', left_key)
  panel.bind('<Up>', left_key)
  panel.bind('<Down>', left_key)
  panel.bind('q', quit)
  panel.focus_set()
  panel.pack(side = "bottom", fill = "both", expand = "yes")
  root.mainloop() 
Example #23
0
def resetFunction(indices, pixel, labels):
	global changeLog
	global canvas
	global img
	numColors = len(changeLog)
	changeLog = [[] for _ in range(numColors)]
	del changeLogPointers[:]
	for i in range(numColors):
		changeLog[i].append(orgColors[i])
		currentColors.append(orgColors[i])
		changeLogPointers.append(-3)

	for i in range(numColors):
		img = np.zeros((80,80,3), np.uint8)
		img[:,:] = orgColors[i]
		img = Image.fromarray(img)
		img = ImageTk.PhotoImage(img)
		labels[i].configure(image = img)
		labels[i].image = img

	img = orgImg
	cv2.imwrite('curr_img.jpg', img)
	img = Image.fromarray(img)
	img = ImageTk.PhotoImage(img)
	canvas.delete(imageOnCanvas)
	canvas.create_image(0,0,image=img,anchor="nw")
    def generate(self, diversities, iteration):
        start_idx = random.randint(0, self.data.shape[0] - 1)

        for diversity in diversities:
            print()
            print('Diversity:', diversity)

            generated = []
            seed = self.data[start_idx]
            seed = seed[None, None, :]

            for i in xrange(self.h):
                if (i + 1) % self.h == 0:
                    line = (i + 1) / self.h
                    print('Generating line {} of {}'.format(line, self.h))

                preds = self.model.predict(seed, verbose=0)[0]
                # next_idx = self.sample(preds, diversity)
                next_idx = np.argmax(preds)
                next_color = self.data[next_idx]

                seed = next_color[None, None, :]
                generated.append(next_color)

            generated = np.array(generated).astype(np.uint8)
            # add back 255
            generated = generated.reshape(self.n * self.w * self.h, 1)
            add = np.full((self.n * self.w * self.h, 1), 255, dtype = np.uint8)
            generated = np.concatenate((generated, add), axis=1)
            generated = generated.reshape(self.w, self.h, self.d)

            filename = 'output_{}_{}.png'.format(iteration, diversity)
            print('Saving output image as {}'.format(filename))
            Image.fromarray(generated).save(filename)
Example #25
0
def save_sample_pictures():
    for te_train, te_target in test_stream.get_epoch_iterator():
        break
    te_out, te_ta = ae_encode(input_transform(te_train), target_transform(te_target))
    te_reshape = inverse(te_out)
    te_target_reshape = inverse(te_ta)

    new_size = (128 * 6, 160 * 12)
    new_im = Image.new('RGB', new_size)
    r = np.random.choice(128, 24, replace=False).reshape(2,12)
    for i in range(2):
        for j in range(12):
            index =  r[i][j]
            a1 = np.concatenate((te_train[index],te_target_reshape[index]),axis=2)
            a1 = YUV2RGB(a1)
            a2 = np.concatenate((te_train[index],te_train[index]),axis=2)
            a2 = np.concatenate((a2,te_train[index]),axis=2)
            a3 = np.concatenate((te_train[index],te_reshape[index]),axis=2)
            a3 = YUV2RGB(a3)
            target_im = Image.fromarray(a1.astype(np.uint8))
            train_im = Image.fromarray(a2.astype(np.uint8))
            im = Image.fromarray(a3.astype(np.uint8))
            
            new_im.paste(target_im, (128 * i * 3, 160 * j))
            new_im.paste(train_im, (128 * (i * 3 + 1), 160 * j))
            new_im.paste(im, (128 * (i * 3 + 2), 160 * j))
    img_loc = "/data/chencj/Face/gen_images/%i.png" %int(time())     
    print "saving images to %s" %img_loc
    new_im.save(img_loc)
Example #26
0
 def slide(self, master, slave):
     n = self.n
     min_master = None
     min_slave = None
     min_sum = 9999999999999
     x, y = 0, 0
     for cr in range(self.imgs[master].size[1] / n):
         for cc in range(self.imgs[master].size[0] / n):
             # racuna dimenzije sliding prozora za poredjenje
             w = self.imgs[master].size[0] - cc * n
             # w = w if self.imgs[slave].size[0] < w else self.imgs[slave].size[0]
             h = self.imgs[master].size[1] - cr * n
             # h = h if self.imgs[slave].size[1] < h else self.imgs[slave].size[1]
             master_slice = self.flat_imgs_array[master][cr * n:cr * n + h, cc * n:cc * n + w]
             slave_slice = self.flat_imgs_array[slave][:h, :w]
             # Image.fromarray(master_slice - slave_slice).show()
             # print h, w, np.sum(master_slice - slave_slice)
             tmp_sum = np.sum(abs(master_slice - slave_slice)) / (h * w)
             if tmp_sum < min_sum:
                 min_sum = tmp_sum
                 min_master = master_slice
                 min_slave = slave_slice
                 x, y = cc * n, cr * n
     kolaz_y = self.imgs[master].size[1] + self.imgs[slave].size[1] - len(min_slave)
     kolaz_x = self.imgs[master].size[0] + self.imgs[slave].size[0] - len(min_slave[0])
     kolaz = np.zeros((kolaz_y, kolaz_x))
     kolaz[:self.imgs[master].size[1], :self.imgs[master].size[0]] = self.img_arrays[master]
     kolaz[y:, x:] = self.img_arrays[slave]
     Image.fromarray(kolaz).show()
Example #27
0
File: myutils.py Project: ghif/drcn
def show_images(Xo, padsize=1, padval=0, filename=None, title=None):
	# data format : channel_first
	X = np.copy(Xo)
	[n, c, d1, d2] = X.shape
	if c== 1:
		X = np.reshape(X, (n, d1, d2))

	n = int(np.ceil(np.sqrt(X.shape[0])))
	
	padding = ((0, n ** 2 - X.shape[0]), (0, padsize), (0, padsize)) + ((0, 0), ) * (X.ndim - 3)
	canvas = np.pad(X, padding, mode='constant', constant_values=(padval, padval))

	canvas = canvas.reshape((n, n) + canvas.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, canvas.ndim + 1)))
	canvas = canvas.reshape((n * canvas.shape[1], n * canvas.shape[3]) + canvas.shape[4:])

	if title is not None:
		title_canv = np.zeros((50, canvas.shape[1]))
		title_canv = title_canv.astype('uint8')
		canvas = np.vstack((title_canv, canvas)).astype('uint8')
		
		I = Image.fromarray(canvas)
		d = ImageDraw.Draw(I)
		fill = 255
		d.text((10, 10), title, fill=fill, font=fnt)
	else:
		canvas = canvas.astype('uint8')
		I = Image.fromarray(canvas)

	if filename is None:
		I.show()
	else:
		I.save(filename)

	return I
    def find_roi_normal(self):
        # self.mask = cv2.cvtColor(self.mask, cv2.CV_32SC1)
        hsv = cv2.cvtColor(self.rgb_image, cv2.COLOR_BGR2HSV)
        # [20, 20, 20]
        lower_red = np.array([30, 30, 30])
        # [255, 255, 255]
        upper_red = np.array([200, 200, 200])
        mask = cv2.inRange(hsv, lower_red, upper_red)
        res = cv2.bitwise_and(self.rgb_image, self.rgb_image, mask=mask)

        # (50, 50)
        close_kernel = np.ones((50, 50), dtype=np.uint8)
        close_kernel_tmp = np.ones((30, 30), dtype=np.uint8)
        image_close = Image.fromarray(cv2.morphologyEx(np.array(mask), cv2.MORPH_CLOSE, close_kernel))
        image_close_tmp = Image.fromarray(cv2.morphologyEx(np.array(mask), cv2.MORPH_CLOSE, close_kernel_tmp))
        # (30, 30)
        open_kernel = np.ones((30, 30), dtype=np.uint8)
        open_kernel_tmp = np.ones((30, 30), dtype=np.uint8)
        image_open = Image.fromarray(cv2.morphologyEx(np.array(image_close), cv2.MORPH_OPEN, open_kernel))
        image_open_tmp = Image.fromarray(cv2.morphologyEx(np.array(image_close_tmp), cv2.MORPH_OPEN, open_kernel_tmp))
        contour_rgb, bounding_boxes, contour_rgb_tmp = self.get_normal_image_contours(np.array(image_open),
                                                                                      self.rgb_image,
                                                                                      np.array(image_open_tmp))
        # self.draw_bbox(bounding_boxes)

        self.display(contour_rgb, contour_rgb_tmp)
Example #29
0
 def _repr_png_(self):
     """Returns an Image for display in an IPython console"""
     # Convert picture to in-memory PNG
     data = BytesIO()
     Image.fromarray(self._inflate(self._image)).save(data, format="png")
     data.seek(0)
     return data.read()
Example #30
0
def changeColor(i, indices, pixel, labels, color):
	global imageOnCanvas
	global canvas
	global img
	img = np.zeros((80,80,3), np.uint8)
	img[:,:] = color
	img = Image.fromarray(img)
	img = ImageTk.PhotoImage(img)
	labels[i].configure(image = img)
	labels[i].image = img

	img = cv2.imread('curr_img.jpg')
	height, width, channel = img.shape

	for j in range(len(coordinates[indices[i]])):
		img[coordinates[indices[i]][j][1],coordinates[indices[i]][j][0]] = color

	if(i != (len(indices)-1)):
		img_old = cv2.imread('curr_img.jpg')
		hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		h, s, v = cv2.split(hsv)
		hsv2 = cv2.cvtColor(img_old, cv2.COLOR_BGR2HSV)
		h1, s1, v1 = cv2.split(hsv2)
		final_hsv = cv2.merge((h, s, v1))
		img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)

	cv2.imwrite('curr_img.jpg', img)

	img = Image.fromarray(img)
	img = ImageTk.PhotoImage(img)

	canvas.delete(imageOnCanvas)
	canvas.create_image(0, 0, image=img, anchor="nw")
def make_face_df_save(face_landmarks, file_path, file_name, save_path, filenum, df):
    image_select = file_path + file_name
    
    # This function looks at one image, draws points and saves points to DF
    pts = []
   # filenum = 0   # need this to iterate through the dataframe to append rows
    face = 0

    """
    Initial face landmarks are now passed as an argument, so the process does not runs again
    
    image = face_recognition.load_image_file(image_select)
    face_landmarks_list = face_recognition.face_landmarks(image)
    """
    face_landmarks_list = face_landmarks

    for face_landmarks in face_landmarks_list:
        face += 1
        if face >1:    # this will only measure one face per image
            break
        else:
            # Print the location of each facial feature in this image
            facial_features = [
                'chin',
                'left_eyebrow',
                'right_eyebrow',
                'nose_bridge',
                'nose_tip',
                'left_eye',
                'right_eye',
                'top_lip',
                'bottom_lip'
                ]

            for facial_feature in facial_features:
                # put each point in a COLUMN
                for  point in  face_landmarks[facial_feature]:
                    for pix in point:
                        pts.append(pix)
               
        # pil_image = Image.fromarray(image)
        # d = ImageDraw.Draw(pil_image)
        
        eyes = []
        lex = pts[72]
        ley = pts[73]
        rex = pts[90]
        rey = pts[91]
        eyes.append(pts[72:74])
        eyes.append(pts[90:92])

        picture_service = services.PictureService()
        # picture_service.crop_picture(image_select)
        picture_service.crop_picture(file_path, file_name, save_path)
        # try:
        #     picture_service.crop_picture(image_select)
        # except:
        #     print("Error cropping the file")
        #     continue

        # -------- Original code --------
        # image =  Image.open(image_select)
        # crop_image = crop_face(image, eye_left=(lex, ley), eye_right=(rex, rey), offset_pct=(0.34,0.34), dest_sz=(300,300))
        # try:
        #     crop_image.save(str(image_select)+"_NEW_cropped.jpg")
        # except:
        #     continue
        #crop_image.show()
        # -------- end of Original code --------
        print(save_path+file_name+"_cropped.jpg")

        new_file_name = file_name.split('.')[0]
        new_file_extension = file_name.split('.')[1]

        neww = save_path + new_file_name + "_cropped." + new_file_extension
        print(neww)
        
        # nn = str(save_path + file_name)+"_cropped.jpg"
        nn = str(save_path + new_file_name + "_cropped." + new_file_extension)
        pts = []
        face = 0
        """
        The cropped and rotated image is read again to detect the face landmarks 
        """
        image = face_recognition.load_image_file(nn)
        face_landmarks_list = face_recognition.face_landmarks(image)

        for face_landmarks in face_landmarks_list:
            face += 1
            if face >1:    # this will only measure one face per image
                break
            else:
                # Print the location of each facial feature in this image
                facial_features2 = [
                    'chin',
                    'left_eyebrow',
                    'right_eyebrow',
                    'nose_bridge',
                    'nose_tip',
                    'left_eye',
                    'right_eye',
                    'top_lip',
                    'bottom_lip'
                    ]

                for facial_feature in facial_features2:
                    # put each point in a COLUMN
                    for  point in  face_landmarks[facial_feature]:
                        for pix in point:
                            pts.append(pix)

            i = 0
            for j in range(0,17):
                if i != 16:
                    if i != 17:
                        px = pts[i]
                        py = pts[i+1]
                        chin_x = pts[16]   # always the chin x
                        chin_y = pts[17]   # always the chin y

                        x_diff = float(px - chin_x)

                        if(py == chin_y): 
                            y_diff = 0.1
                        if(py < chin_y): 
                            y_diff = float(np.absolute(py-chin_y))
                        if(py > chin_y):
                            y_diff = 0.1
                            print("Error: facial feature is located below the chin.")

                        angle = np.absolute(math.degrees(math.atan(x_diff/y_diff)))

                        pts.append(angle)
                i += 2
        
            pil_image = Image.fromarray(image)
            d = ImageDraw.Draw(pil_image)

            for facial_feature in facial_features2:
                    #d.line(face_landmarks[facial_feature], width=5)
                    d.point(face_landmarks[facial_feature], fill=(255,255,255))

            """
            we can omit saving the picture with the points drawn to save processing time and disk space
            
            pil_image.save(str(save_path + file_name.split('.')[0]) + '_rotated_pts.jpg', 'JPEG', quality=100)
            """

            # take_measurements width & height measurements
        msmt = []
        a = pts[0]   ## point 1 x - left side of face 
        b = pts[1]   ## point 1 y
        c = pts[32]  ## point 17 x - right side of face
        d = pts[33]  ## point 17 y

        e = pts[16]  ## point 9 x - chin
        f = pts[17]  ## point 9 y - chin
        #Visual inspection indicates that point 29 is the middle of the face, 
        #so the height of the face is 2X the height between chin & point 29 which are coordinates 56 and 57     
        g = pts[56]  # point 29's x coordinate (mid-face point)
        h = pts[57]   # point 29's y coordinate
        
        i = pts[12]    # point 7 x   for jaw length 
        j = pts[13]    # point 7 y   for jaw length
        k = pts[20]    # point 11 x  for jaw length
        l = pts[21]    # point 11 y  for jaw length
             
        m = pts[8]     # point 5 x   for lower jaw length     
        n = pts[9]     # point 5 y
        o = pts[24]     # point 13 x
        p = pts[25]     # point 13 y

        face_width = np.sqrt(np.square(a - c) + np.square(b - d))
        #print(face_width)
        pts.append(face_width)
        face_height = np.sqrt(np.square(e - g) + np.square(f - h)) * 2   # double the height to the mid-point
        #print(face_height)
        pts.append(face_height)
        height_to_width = face_height/face_width
        
        pts.append(height_to_width)
        
        # JAW width (7-11)
        jaw_width = np.sqrt(np.square(i-k) + np.square(j-l))
        pts.append(jaw_width)
        jaw_width_to_face_width =  jaw_width/face_width
        pts.append(jaw_width_to_face_width)
        
        # mid-JAW width (5-13)
        mid_jaw_width = np.sqrt(np.square(m-o) + np.square(n-p))
        pts.append(mid_jaw_width)
        mid_jaw_width_to_jaw_width =  mid_jaw_width/jaw_width
        pts.append(mid_jaw_width_to_jaw_width)
        
        ### end of new ###

        df.loc[filenum] = np.array(pts)
Example #32
0
    def update_frame(self):
        global avg1, avg2, avg3, t0, t, p, x1, y1, x2, y2, a1, b1, a2, b2, c1, d1, c2, d2, inverted, tstart, samplenum, grower, sampletextset, cmap, background, backgroundset, scaled_w, scaled_h, runstopwatch, timing, timer_active, stopwatch_active
        #### Size and identity of camera frame ####
        self.window_width = self.parentFrame.frameSize().width()
        self.window_height = self.parentFrame.frameSize().height()
        if not q.empty():
            self.connectButton.setText('Connected')
            frame = q.get()
            img = frame["img"]
            if inverted:
                img = np.invert(img)
            if backgroundset:
                img = img - background
#            img = np.flipud(img)
            img_height, img_width = img.shape
            #### Scaling the image from the camera ####
            scale_w = float(self.window_width) / float(img_width)
            scale_h = float(self.window_height) / float(img_height)
            scale = min([scale_w, scale_h])
            if scale == 0:
                scale = 1
            self.scaled_w = int(scale * img_width)
            self.scaled_h = int(scale * img_height)
            scaled_w, scaled_h = self.scaled_w, self.scaled_h
            #### Resize camera canvas to proper size ####
            self.cameraCanvas.resize(self.scaled_w, self.scaled_h)
            self.drawCanvas.resize(self.scaled_w, self.scaled_h)
            #### Resize annotation frame to proper width ####
            self.annotationFrame.resize(self.scaled_w, self.annotationFrame.frameSize().height())
            img = cv2.resize(img, dsize=(self.scaled_w, self.scaled_h), interpolation=cv2.INTER_CUBIC)
            #### Apply colormap ####
            imc = Image.fromarray(np.uint8(cmap(img)*255)) # colormaps: cm.gray, FRHEEDcmap, cm.Greens
            #### Convert PIL image to QImage
            imc = ImageQt.ImageQt(imc)
            #### Adding the camera to the screen ####
            self.cameraCanvas.setPixmap(QtGui.QPixmap.fromImage(imc))
            #### Updating live data ####
            if liveplotting:
                avg_1 = img[y1:y2, x1:x2].mean()
                avg_1 = round(avg_1, 3)
                avg_2 = img[b1:b2, a1:a2].mean()
                avg_2 = round(avg_2, 3)
                avg_3 = img[d1:d2, c1:c2].mean()
                avg_3 = round(avg_3, 3)
                avg1.append(avg_1)
                avg2.append(avg_2)
                avg3.append(avg_3)
                timenow = time.time() - t0
                t.append(timenow)
                pen1 = pg.mkPen('r', width=1, style=QtCore.Qt.SolidLine)
                pen2 = pg.mkPen('g', width=1, style=QtCore.Qt.SolidLine)
                pen3 = pg.mkPen('b', width=1, style=QtCore.Qt.SolidLine)
                curve1 = self.plot1.plot(pen=pen1, clear = True)
                curve2 = self.plot1.plot(pen=pen2)
                curve3 = self.plot1.plot(pen=pen3)
                curve1.setData(t, avg1)
                curve2.setData(t, avg2)
                curve3.setData(t, avg3)
            if drawing:
                pixmap = QtGui.QPixmap(self.drawCanvas.frameGeometry().width(), self.drawCanvas.frameGeometry().height())
                pixmap.fill(QtGui.QColor("transparent"))
                qp = QtGui.QPainter(pixmap)
                qp.setPen(QtGui.QPen(QtCore.Qt.red, 2, QtCore.Qt.SolidLine))
                qp.drawRect(x1, y1, x2 - x1, y2 - y1)
                qp.setPen(QtGui.QPen(QtCore.Qt.green, 2, QtCore.Qt.SolidLine))
                qp.drawRect(a1, b1, a2 - a1, b2 - b1)
                qp.setPen(QtGui.QPen(QtCore.Qt.blue, 2, QtCore.Qt.SolidLine))
                qp.drawRect(c1, d1, c2 - c1, d2 - d1)
                qp.end()
                self.drawCanvas.setPixmap(pixmap)
        self.sampleLabel.setText('Current Sample: '+samplenum)
        self.growerLabel.setText('Current Grower: '+grower)
        self.annotateSampleName.setText('Sample: '+self.setSampleName.text())
        self.annotateOrientation.setText('Orientation: '+self.setOrientation.text())
        self.annotateLayer.setText('Growth layer: '+self.setGrowthLayer.text())
        self.annotateMisc.setText('Other notes: '+self.setMisc.text())
        #### Updating cursor position text labels ####
        self.cursorLiveData.setText(self.livecursor)
        self.cursorNewerData.setText(self.newercursor)
        self.cursorOlderData.setText(self.oldercursor)
        self.cursorFFTRed.setText(self.redcursor)
        self.cursorFFTGreen.setText(self.greencursor)
        self.cursorFFTBlue.setText(self.bluecursor)
        #### Updating manual RHEED oscillation calculations ####
        self.rheed1Label.setText(self.rheed1cal)
        self.rheed2Label.setText(self.rheed2cal)
        self.rheed3Label.setText(self.rheed3cal)
        if runstopwatch and stopwatch_active:
            self.timenow = round(float(time.time() - tstart + float(self.savedtime)), 2)
            self.tnow = "%.2f" % self.timenow
            self.stopwatchScreen.display(self.tnow)
        if not runstopwatch and stopwatch_active:
            _ = '%.2f' % self.savedtime
            self.stopwatchScreen.display(_)
        if timing and timer_active:
            if self.savedtime2 == 0.0:
                self.remaining = (self.timerstart + float(self.totaltime)) - time.time()
            else:
                self.remaining = (self.timerstart + float(self.savedtime2)) - time.time()
            hours, rem = divmod(self.remaining, 3600)
            minutes, seconds = divmod(rem, 60)
            self.formatted_time = str("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
            self.timerScreen.display(self.formatted_time)
            if self.remaining < 0:
                self.timerScreen.setStyleSheet('QLCDNumber {color:red}')
                if self.savedtime2 == 0.0:
                    self.remaining = (self.timerstart + float(self.totaltime)) - time.time()
                else:
                    self.remaining = (self.timerstart + float(self.savedtime2)) - time.time()
                hours, rem = divmod(self.remaining, 3600)
                minutes, seconds = divmod(rem, 60)
                self.formatted_time = str("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
                self.timerScreen.display(self.formatted_time)
                self.remaining = self.overtime
        if not timing and timer_active:
            hours, rem = divmod(self.remaining, 3600)
            minutes, seconds = divmod(rem, 60)
            self.formatted_time = str("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
            self.timerScreen.display(self.formatted_time)
        if self.numpeaksLive.value() < 3 or self.numpeaksNewer.value() < 3 or self.numpeaksOlder.value() < 3:
            QtWidgets.QMessageBox.warning(self, 'Alert', 'Your calibration is shit.')
            self.numpeaksLive.setValue(10)
            self.numpeaksNewer.setValue(10)
            self.numpeaksOlder.setValue(10)
        if not sampletextset and samplenum != "None":
            self.setSampleName.setText(samplenum)
            sampletextset = True
gen = patch_generator(bg_filenames, crop_size, factor)
print "Going in..."
#idcodes = []
for i, fn in enumerate(fg_filenames):

    print fn
    #fg_im = Image.open(fn)
    fg_im = gv.img.load_image(fn)

    # Resize
    fg_im2 = gv.img.resize(fg_im, size)

    # Crop
    fg_im3 = gv.img.crop(fg_im2, crop_size)

    fg_pil = Image.fromarray((fg_im3 * 255).astype(np.uint8))

    # Now superimpose it onto the dst_im
    try:
        dst_im = gen.next()
    except StopIteration:
        print "Breaking."
        break

    # Find the bounding box in the fg image
    #bnd_box = find_bounding_box(fg_im)
    #print bnd_box

    #index = i + mix_comps[i] * 1000

    #print index
def resize_n_merge_triplet(imgs, min_shape):
    # Merge the images using vstack and save the Vertially merged images
    new_imgs = []
    img_merge = np.vstack((np.asarray(i.resize(min_shape, Img.ANTIALIAS)) for i in imgs))
    img_merge = Img.fromarray(img_merge)
    return img_merge
Example #35
0
import cv2
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt

from PIL import Image, ImageTk

root = tk.Tk()

imagen = cv2.imread('perroSm.jpg',0)

figura = plt.figure(figsize=(100,100))
lienzo = figura.add_subplot(111)

im_plt = lienzo.imshow(imagen)

image1 = Image.fromarray(np.uint8( im_plt.get_cmap()(im_plt.get_array())*255))
im = ImageTk.PhotoImage('RGB', image1.size)

tk.Label(root, image=im).pack()

root.mainloop()
Example #36
0
def make_quiver_from_action_probs(action_probs,
                                  action_space_type,
                                  actions,
                                  title,
                                  bg_img=None):
    H, W = action_probs.shape[-2:]
    action_probs = action_probs * (action_probs > 0)
    _x, _y = np.meshgrid(np.arange(H) + 0.5, np.arange(W) + 0.5)
    _u_zero, _v_zero = np.meshgrid(np.zeros(H), np.zeros(W))

    if action_space_type == 'pov':
        # POV action space (processed)
        # _pad = np.zeros(action_probs.shape)
        # action_probs = np.concatenate([_pad[:, 0:1], action_probs], 1)
        '''
        NOTE: Turn-left, Turn-right, as well as Down, Up are inverted
        here because the environment grid being viewed in visdom has
        been transposed.
        '''
        _right = action_probs[0]
        _down = -1 * action_probs[3]
        _left = -1 * action_probs[2]
        _up = action_probs[1]
        _turn_left = action_probs[5]
        _turn_right = action_probs[4]
        _pickup = action_probs[6]
        _drop = action_probs[7]
        _toggle = action_probs[8]
        _stay = action_probs[9]
        _stay = _stay + _drop + _toggle + _pickup

        # Uncomment to visualize maximum size of markers
        # _toggle[0, 0] = 1.0
        # _pickup[0, 1] = 1.0
        # _drop[0, 2] = 1.0
        # _stay[0, 3] = 1.0
        # _right[0, 4] = 1.0
        # _left[0, 5] = -1.0
        # _up[0, 6] = 1.0
        # _down[0, 7] = -1.0

    elif action_space_type == 'cardinal':
        # Cardinal action space
        '''
        NOTE: Down and Up are inverted here because the environment
        grid being viewed in visdom has been transposed, so the up action
        actually corresponds to moving down in the visualized grid.
        '''
        _right = action_probs[actions.right]
        _down = -1 * action_probs[actions.up]
        _left = -1 * action_probs[actions.left]
        _up = action_probs[actions.down]
        _stay = action_probs[actions.done]
        # _u = _right - _left
        # _v = _up - _down

        # Uncomment to visualize maximum size of markers
        # _stay[0, 1] = 1.0
        # _right[0, 0] = 1.0
        # _left[0, 0] = -1.0
        # _up[0, 0] = 1.0
        # _down[0, 0] = -1.0

    else:
        raise ValueError("Cannot recognize action space {}"\
            .format(action_space_type))

    common_kwargs = {'scale': 0.5, 'arrow_scale': 0.5}
    quiver_1 = ff.create_quiver(_x,
                                _y,
                                _right,
                                _v_zero,
                                name='right',
                                **common_kwargs)
    quiver_2 = ff.create_quiver(_x,
                                _y,
                                _left,
                                _v_zero,
                                name='left',
                                **common_kwargs)
    quiver_3 = ff.create_quiver(_x,
                                _y,
                                _u_zero,
                                _up,
                                name='up',
                                **common_kwargs)
    quiver_4 = ff.create_quiver(_x,
                                _y,
                                _u_zero,
                                _down,
                                name='down',
                                **common_kwargs)
    quiver_1['data'][0]['marker'].update({'color': 'blue'})
    quiver_2['data'][0]['marker'].update({'color': 'orange'})
    quiver_3['data'][0]['marker'].update({'color': 'green'})
    quiver_4['data'][0]['marker'].update({'color': 'red'})

    def my_scatter(inp, title, symbol, fillcolor, linecolor):
        return go.Scatter(
            x=_x.reshape(-1),
            y=_y.reshape(-1),
            mode='markers',
            marker=dict(
                symbol=symbol,
                size=inp.reshape(-1),
                sizemode='area',
                opacity=0.4,
                sizeref=2.0 / ((16**2) * (25. / H)),
                line=dict(width=1, color=linecolor),
                # color = 'rgba(204, 0, 204, .9)',
                color=fillcolor,
            ),
            name=title)

    quiver_5 = my_scatter(_stay,
                          'stay',
                          symbol='octagon',
                          fillcolor='red',
                          linecolor='black')

    data = [
        *quiver_1['data'],
        *quiver_2['data'],
        *quiver_3['data'],
        *quiver_4['data'],
        quiver_5,
    ]

    if action_space_type == 'pov':
        # quiver_6 = my_scatter(_pickup, 'pickup',
        #     symbol='triangle-up', fillcolor='skyblue', linecolor='blue')
        # quiver_7 = my_scatter(_drop, 'drop',
        #     symbol='triangle-down', fillcolor='orange', linecolor='brown')
        # quiver_8 = my_scatter(_toggle, 'toggle',
        #     symbol='star', fillcolor='gold', linecolor='black')
        # data.extend([quiver_6, quiver_7, quiver_8])
        '''Left and right turns'''
        quiver_6 = my_scatter(_turn_left,
                              'turn_left',
                              symbol='triangle-left',
                              fillcolor='skyblue',
                              linecolor='blue')
        quiver_7 = my_scatter(_turn_right,
                              'turn_right',
                              symbol='triangle-right',
                              fillcolor='orange',
                              linecolor='brown')
        data.extend([quiver_6, quiver_7])
        pass

    if bg_img is None:
        fig = go.Figure()
    else:
        from PIL import Image
        # image_array = np.random.randint(0, 255, size=(100, 100)).astype('uint8')
        # image_tmp = Image.fromarray(image_array)
        bg_pil_img = Image.fromarray(np.rollaxis(bg_img, 0, 3))
        fig = go.Figure(layout=go.Layout(
            images=[
                go.layout.Image(
                    source=bg_pil_img,
                    xref="x",
                    yref="y",
                    x=0,
                    y=H,
                    sizex=W,
                    sizey=H,
                    sizing="stretch",
                    opacity=0.1,
                    layer="below",
                )
            ],
            xaxis=dict(
                showgrid=False,
                showline=False,
            ),
            yaxis=dict(
                showgrid=False,
                showline=False,
            ),
        ))
    fig['layout']['title'] = title
    fig.add_traces(data=data)
    return fig
Example #37
0
                #Check to make sure that the TF_Example has valid bounding boxes.
                #If there are no valid bounding boxes, then don't save the image to the TFRecord.
                #float_list_value = tf_example.features.feature['image/object/bbox/xmin'].float_list.value

                if (ind_chips < max_chips_per_res and tf_example):
                    tot_box += tf_example.count('\n')

                    if idx < split_ind:
                        test_chips += 1
                    else:
                        train_chips += 1

                    writer = open(
                        "labels/%s.txt" % (str(ind_chips).rjust(6, '0')), "w")
                    img_file = 'images/%s.png' % (str(ind_chips).rjust(6, '0'))
                    Image.fromarray(image).save(img_file)
                    images_list.append(os.path.join('/raid', img_file))

                    writer.write(tf_example)

                    ind_chips += 1

                    #Make augmentation probability proportional to chip size.  Lower chip size = less chance.
                    #This makes the chip-size imbalance less severe.
                    prob = np.random.randint(0, np.max(res))
                    #for 200x200: p(augment) = 200/500 ; for 300x300: p(augment) = 300/500 ...

                    if AUGMENT and prob < it[0]:

                        for extra in range(3):
                            center = np.array([
Example #38
0
    args = parser.parse_args()

    os.makedirs(args.out_dir, exist_ok=True)

    root_mnist = '../datas/MNIST'
    root_fashionmnist = '../datas/FashionMNIST'

    if args.dataset == 'OmniMNIST':
        dataset = OmniMNIST(root=root_mnist,
                            fov=args.fov,
                            flip=args.flip,
                            h_rotate=args.h_rotate,
                            v_rotate=args.v_rotate,
                            fix_aug=args.fix_aug)
    elif args.dataset == 'OmniFashionMNIST':
        dataset = OmniFashionMNIST(root=root_fashionmnist,
                                   fov=args.fov,
                                   flip=args.flip,
                                   h_rotate=args.h_rotate,
                                   v_rotate=args.v_rotate,
                                   fix_aug=args.fix_aug)
    print(args.idx)

    for idx in args.idx:
        idx = int(idx)
        path = os.path.join(args.out_dir, '%d.png' % idx)
        x, label = dataset[idx]

        print(path, label)
        Image.fromarray(x.numpy().astype(np.uint8)).save(path)
Example #39
0
        saver.restore(sess, full_path)

        for i in range(total_batch_val):

            val_y = get_train_batch(i)
            hazy_y = get_test_batch(i)

            val_result = sess.run(output, feed_dict={input_x: hazy_y})

            for j in range(35):

                array = np.reshape(val_result[j], newshape=[INPUT_HEIGHT, INPUT_WIDTH, 3])
                array = array * 255
                array = tf.saturate_cast(array, dtype=tf.uint8)
                arr1 = sess.run(array)
                image = Image.fromarray(arr1, 'RGB')
                # if image.mode != 'L':
                #     image = image.convert('L')
                image.save('./pred3_25_test/' + str(epoch + 1) + '_' + str(i) + '_' + str(j) + '.jpg', 'jpeg')

                array_truth = np.reshape(val_y[j], newshape=[INPUT_HEIGHT, INPUT_WIDTH, 3])
                array_truth = array_truth * 255
                array_truth = tf.saturate_cast(array_truth, dtype=tf.uint8)
                arr3 = sess.run(array_truth)
                image_truth = Image.fromarray(arr3, 'RGB')
                # if image.mode != 'L':
                #     image = image.convert('L')
                image_truth.save('./pred3_25_test/' + str(epoch + 1) + '_' + str(i) + '_' + str(j) + '_truth.jpg', 'jpeg')

                psnr_val = tools.cal_psnr(arr1, arr3)
                print('epoch: %04d\tper psnr: %.9f' % (epoch + 1, psnr_val))
Example #40
0
    assert os.path.isdir(lidar_dir)
    assert os.path.isdir(calib_dir)
    assert os.path.isdir(image_dir)

    if not os.path.isdir(depth_dir):
        os.makedirs(depth_dir)

    lidar_files = [x for x in os.listdir(lidar_dir) if x[-3:] == 'bin']
    lidar_files = sorted(lidar_files)

    assert os.path.isfile(args.split_file)
    with open(args.split_file, 'r') as f:
        file_names = [x.strip() for x in f.readlines()]

    for fn in lidar_files:
        predix = fn[:-4]
        if predix not in file_names:
            continue
        calib_file = '{}/{}.txt'.format(calib_dir, predix)
        calib = kitti_util.Calibration(calib_file)
        # load point cloud
        lidar = np.fromfile(lidar_dir + '/' + fn, dtype=np.float32).reshape((-1, 4))[:, :3]
        image_file = '{}/{}.png'.format(image_dir, predix)
        image = imageio.imread(image_file)
        height, width = image.shape[:2]
        depth = generate_depth_from_velo(lidar, height, width, calib)
        img = (depth*256).astype('uint16')
        img = Image.fromarray(img)
        img.save(depth_dir  + '/' + predix+'.png')
        print('Finish Depth {}'.format(predix))
        assert self.loss_value is not None
        grad_values = np.copy(self.grad_values)
        self.loss_value = None
        self.grad_values = None
        return grad_values

evaluator = Evaluator()

x = np.random.uniform(0, 255, (1, height, width, 3)) - 128.

iterations = 10

for i in range(iterations):
    print('Start of iteration', i)
    start_time = time.time()
    x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(),
                                     fprime=evaluator.grads, maxfun=20)
    print('Current loss value:', min_val)
    end_time = time.time()
    print('Iteration %d completed in %ds' % (i, end_time - start_time))

x = x.reshape((height, width, 3))
x = x[:, :, ::-1]
x[:, :, 0] += 103.939
x[:, :, 1] += 116.779
x[:, :, 2] += 123.68
x = np.clip(x, 0, 255).astype('uint8')

Image.fromarray(x)

Example #42
0
#!/usr/bin/env python3
from PIL import Image
import numpy as np

imgSource = Image.open('_source.jpg')
imgSecret = Image.open('_secret.png')
dataSource = np.array(imgSource)
dataSecret = np.array(imgSecret)

dataSource[...] = dataSource & 0xF8
dataSecret[...] = (dataSecret & 0xE0) >> 5
dataSource = dataSource + dataSecret

out = Image.fromarray(dataSource, 'RGB')
out.save('final2.png')
Example #43
0
def histeq(im, nbr_bins=256):
    """对一幅灰度图像进行直方图均衡化"""
    #计算图像的直方图
    #在numpy中,也提供了一个计算直方图的函数histogram(),第一个返回的是直方图的统计量,第二个为每个bins的中间值
    imhist, bins = histogram(im.flatten(), nbr_bins, normed=True)
    cdf = imhist.cumsum()  #
    cdf = 255.0 * cdf / cdf[-1]  #累 计函数归一化(由0~1变换至0~255)
    #使用累积分布函数的线性插值,计算新的像素值
    im2 = interp(im.flatten(), bins[:-1], cdf)
    #interp(x,xp,yp) 输入原函数的一系列点(xp,yp),使用线性插值方法模拟函数并根据这个函数计算f(x)
    return im2.reshape(im.shape), cdf


pil_im = Image.open('/home/gzb/PycharmProjects/homework/data/a.png')  #打开原图
pil_im_gray = pil_im.convert('L')  #转化为灰度图像
pil_im_gray.show()  #显示灰度图像

im = array(
    Image.open('/home/gzb/PycharmProjects/homework/data/a.png').convert('L'))
figure()
hist(im.flatten(), 256)  # 原图

im2, cdf = histeq(im)
figure()
hist(im2.flatten(), 256)
show()  #增强后的

im2 = Image.fromarray(uint8(im2))
im2.show()
im2.save("/home/gzb/PycharmProjects/homework/res/1_res.png")  # 保存增强后的图
Example #44
0
 def getImage(self):
     return Image.fromarray(np.transpose(self.canv_data.astype(np.uint8), (1, 0, 2)))
Example #45
0
def realtime_augmented_data_gen_pos(
        params,
        num_chunks=None,
        chunk_size=CHUNK_SIZE,
        augmentation_params=default_augmentation_params,  # keep
        ds_transforms=ds_transforms_default,
        target_sizes=None,
        processor_class=LoadAndProcessSource,
        processor_class2=LoadAndProcessLens,
        normalize=True,
        resize=False,
        resize_shape=(60, 60),
        range_min=0.02,
        range_max=0.5,
):
    """
    new version, using Pool.imap instead of Pool.map, to avoid the data structure conversion
    from lists to numpy arrays afterwards.
    """
    ds_transforms = ds_transforms_default
    if target_sizes is None:
        target_sizes = [(53, 53) for _ in range(len(ds_transforms))]
    n = 0
    while True:
        start_time = time.time()
        if num_chunks is not None and n >= num_chunks:
            break
        selected_indices_sources = select_indices(num_sources, chunk_size)
        selected_indices_lenses = select_indices(num_lenses, chunk_size)

        labels = np.ones(chunk_size)

        process_func = processor_class(params, ds_transforms,
                                       augmentation_params,
                                       target_sizes)  # SOURCE
        process_func2 = processor_class2(params, ds_transforms,
                                         augmentation_params,
                                         target_sizes)  # LENS

        target_arrays_pos = [
            np.empty((chunk_size, size_x, size_y, params.nbands),
                     dtype="float32") for size_x, size_y in target_sizes
        ]

        pool1 = mp.Pool(NUM_PROCESSES)
        gen = pool1.imap(process_func,
                         selected_indices_sources,
                         chunksize=loadsize)

        pool2 = mp.Pool(NUM_PROCESSES)
        gen2 = pool2.imap(process_func2,
                          selected_indices_lenses,
                          chunksize=loadsize)

        k = 0
        for source, lens in zip(gen, gen2):  #gen=source, gen2=lens
            source = np.array(source)
            lens = np.array(lens)
            imageData = lens + source / np.amax(source) * np.amax(
                lens) * np.random.uniform(range_min, range_max)
            scale_min = 0
            scale_max = imageData.max()
            imageData.clip(min=scale_min, max=scale_max)
            indices = np.where(imageData < 0)
            imageData[indices] = 0.0
            new_img = np.sqrt(imageData)
            if normalize:
                new_img = new_img / new_img.max() * 255.0
            if resize:
                new_img = np.squeeze(new_img.astype(np.uint8), axis=0)
                new_img = Image.fromarray(new_img)
                new_img = new_img.resize(resize_shape, resample=Image.LANCZOS)
            target_arrays_pos[0][k] = new_img
            k += 1

        pool1.close()
        pool1.join()
        pool2.close()
        pool2.join()
        target_arrays_pos.append(labels.astype(np.int32))
        print("\nchunk creation took:{0:.3f}".format(time.time() - start_time))
        yield target_arrays_pos, chunk_size
        n += 1
Example #46
0

def sobel(src):
    img = numpy.asarray(src, dtype="int32")
    img2 = numpy.asarray(src, dtype="int32")
    src = numpy.asarray(src, dtype="int32")
    kernal = numpy.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
    kernalt = kernal.T
    img = mul(img, src, kernal)
    img2 = mul(img2, src, kernalt)

    return img, img2


img = cv2.imread("img.jpg")
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#1
lap = laplacian(imgray)

#2
sob = sobel(imgray)

Image.fromarray(lap).show()
Image.fromarray(sob[0]).show()
Image.fromarray(sob[1]).show()

cv2.imwrite("laplacian.jpg", lap)
cv2.imwrite("sobel1.jpg", sob[0])
cv2.imwrite("sobel2.jpg", sob[1])
'''
加载图像进行人脸检测(cnn模型)
'''

from PIL import Image
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("biden.jpg")

# Find all the faces in the image using a pre-trained convolutional neural network.
# This method is more accurate than the default HOG model, but it's slower
# unless you have an nvidia GPU and dlib compiled with CUDA extensions. But if you do,
# this will use GPU acceleration and perform well.
# See also: find_faces_in_picture.py
#使用cnn进行人脸检测
face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:

    # Print the location of each face in this image
    top, right, bottom, left = face_location
    print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    # You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()
def fit_single_frame(img,
                     keypoints,
                     body_model,
                     camera,
                     joint_weights,
                     body_pose_prior,
                     jaw_prior,
                     left_hand_prior,
                     right_hand_prior,
                     shape_prior,
                     expr_prior,
                     angle_prior,
                     result_fn='out.pkl',
                     mesh_fn='out.obj',
                     out_img_fn='overlay.png',
                     loss_type='smplify',
                     use_cuda=True,
                     init_joints_idxs=(9, 12, 2, 5),
                     use_face=True,
                     use_hands=True,
                     data_weights=None,
                     body_pose_prior_weights=None,
                     hand_pose_prior_weights=None,
                     jaw_pose_prior_weights=None,
                     shape_weights=None,
                     expr_weights=None,
                     hand_joints_weights=None,
                     face_joints_weights=None,
                     depth_loss_weight=1e2,
                     interpenetration=True,
                     coll_loss_weights=None,
                     df_cone_height=0.5,
                     penalize_outside=True,
                     max_collisions=8,
                     point2plane=False,
                     part_segm_fn='',
                     focal_length=5000.,
                     side_view_thsh=25.,
                     rho=100,
                     vposer_latent_dim=32,
                     vposer_ckpt='',
                     use_joints_conf=False,
                     interactive=True,
                     visualize=True,
                     save_meshes=True,
                     degrees=None,
                     batch_size=1,
                     dtype=torch.float32,
                     ign_part_pairs=None,
                     left_shoulder_idx=2,
                     right_shoulder_idx=5,
                     **kwargs):
    assert batch_size == 1, 'PyTorch L-BFGS only supports batch_size == 1'

    device = torch.device('cuda') if use_cuda else torch.device('cpu')

    if degrees is None:
        degrees = [0, 90, 180, 270]

    if data_weights is None:
        data_weights = [1, ] * 5

    if body_pose_prior_weights is None:
        body_pose_prior_weights = [4.04 * 1e2, 4.04 * 1e2, 57.4, 4.78]

    msg = (
        'Number of Body pose prior weights {}'.format(
            len(body_pose_prior_weights)) +
        ' does not match the number of data term weights {}'.format(
            len(data_weights)))
    assert (len(data_weights) ==
            len(body_pose_prior_weights)), msg

    if use_hands:
        if hand_pose_prior_weights is None:
            hand_pose_prior_weights = [1e2, 5 * 1e1, 1e1, .5 * 1e1]
        msg = ('Number of Body pose prior weights does not match the' +
               ' number of hand pose prior weights')
        assert (len(hand_pose_prior_weights) ==
                len(body_pose_prior_weights)), msg
        if hand_joints_weights is None:
            hand_joints_weights = [0.0, 0.0, 0.0, 1.0]
            msg = ('Number of Body pose prior weights does not match the' +
                   ' number of hand joint distance weights')
            assert (len(hand_joints_weights) ==
                    len(body_pose_prior_weights)), msg

    if shape_weights is None:
        shape_weights = [1e2, 5 * 1e1, 1e1, .5 * 1e1]
    msg = ('Number of Body pose prior weights = {} does not match the' +
           ' number of Shape prior weights = {}')
    assert (len(shape_weights) ==
            len(body_pose_prior_weights)), msg.format(
                len(shape_weights),
                len(body_pose_prior_weights))

    if use_face:
        if jaw_pose_prior_weights is None:
            jaw_pose_prior_weights = [[x] * 3 for x in shape_weights]
        else:
            jaw_pose_prior_weights = map(lambda x: map(float, x.split(',')),
                                         jaw_pose_prior_weights)
            jaw_pose_prior_weights = [list(w) for w in jaw_pose_prior_weights]
        msg = ('Number of Body pose prior weights does not match the' +
               ' number of jaw pose prior weights')
        assert (len(jaw_pose_prior_weights) ==
                len(body_pose_prior_weights)), msg

        if expr_weights is None:
            expr_weights = [1e2, 5 * 1e1, 1e1, .5 * 1e1]
        msg = ('Number of Body pose prior weights = {} does not match the' +
               ' number of Expression prior weights = {}')
        assert (len(expr_weights) ==
                len(body_pose_prior_weights)), msg.format(
                    len(body_pose_prior_weights),
                    len(expr_weights))

        if face_joints_weights is None:
            face_joints_weights = [0.0, 0.0, 0.0, 1.0]
        msg = ('Number of Body pose prior weights does not match the' +
               ' number of face joint distance weights')
        assert (len(face_joints_weights) ==
                len(body_pose_prior_weights)), msg

    if coll_loss_weights is None:
        coll_loss_weights = [0.0] * len(body_pose_prior_weights)
    msg = ('Number of Body pose prior weights does not match the' +
           ' number of collision loss weights')
    assert (len(coll_loss_weights) ==
            len(body_pose_prior_weights)), msg

    use_vposer = kwargs.get('use_vposer', True)
    vposer, pose_embedding = [None, ] * 2
    if use_vposer:
        pose_embedding = torch.zeros([batch_size, 32],
                                     dtype=dtype, device=device,
                                     requires_grad=True)

        vposer_ckpt = osp.expandvars(vposer_ckpt)
        vposer, _ = load_vposer(vposer_ckpt, vp_model='snapshot')
        vposer = vposer.to(device=device)
        vposer.eval()

    if use_vposer:
        body_mean_pose = torch.zeros([batch_size, vposer_latent_dim],
                                     dtype=dtype)
    else:
        body_mean_pose = body_pose_prior.get_mean().detach().cpu()

    keypoint_data = torch.tensor(keypoints, dtype=dtype)
    gt_joints = keypoint_data[:, :, :2]
    if use_joints_conf:
        joints_conf = keypoint_data[:, :, 2].reshape(1, -1)

    # Transfer the data to the correct device
    gt_joints = gt_joints.to(device=device, dtype=dtype)
    if use_joints_conf:
        joints_conf = joints_conf.to(device=device, dtype=dtype)

    # Create the search tree
    search_tree = None
    pen_distance = None
    filter_faces = None
    if interpenetration:
        from mesh_intersection.bvh_search_tree import BVH
        import mesh_intersection.loss as collisions_loss
        from mesh_intersection.filter_faces import FilterFaces

        assert use_cuda, 'Interpenetration term can only be used with CUDA'
        assert torch.cuda.is_available(), \
            'No CUDA Device! Interpenetration term can only be used' + \
            ' with CUDA'

        search_tree = BVH(max_collisions=max_collisions)

        pen_distance = \
            collisions_loss.DistanceFieldPenetrationLoss(
                sigma=df_cone_height, point2plane=point2plane,
                vectorized=True, penalize_outside=penalize_outside)

        if part_segm_fn:
            # Read the part segmentation
            part_segm_fn = os.path.expandvars(part_segm_fn)
            with open(part_segm_fn, 'rb') as faces_parents_file:
                face_segm_data = pickle.load(faces_parents_file,
                                             encoding='latin1')
            faces_segm = face_segm_data['segm']
            faces_parents = face_segm_data['parents']
            # Create the module used to filter invalid collision pairs
            filter_faces = FilterFaces(
                faces_segm=faces_segm, faces_parents=faces_parents,
                ign_part_pairs=ign_part_pairs).to(device=device)

    # Weights used for the pose prior and the shape prior
    opt_weights_dict = {'data_weight': data_weights,
                        'body_pose_weight': body_pose_prior_weights,
                        'shape_weight': shape_weights}
    if use_face:
        opt_weights_dict['face_weight'] = face_joints_weights
        opt_weights_dict['expr_prior_weight'] = expr_weights
        opt_weights_dict['jaw_prior_weight'] = jaw_pose_prior_weights
    if use_hands:
        opt_weights_dict['hand_weight'] = hand_joints_weights
        opt_weights_dict['hand_prior_weight'] = hand_pose_prior_weights
    if interpenetration:
        opt_weights_dict['coll_loss_weight'] = coll_loss_weights

    keys = opt_weights_dict.keys()
    opt_weights = [dict(zip(keys, vals)) for vals in
                   zip(*(opt_weights_dict[k] for k in keys
                         if opt_weights_dict[k] is not None))]
    for weight_list in opt_weights:
        for key in weight_list:
            weight_list[key] = torch.tensor(weight_list[key],
                                            device=device,
                                            dtype=dtype)

    # The indices of the joints used for the initialization of the camera
    init_joints_idxs = torch.tensor(init_joints_idxs, device=device)

    edge_indices = kwargs.get('body_tri_idxs')
    init_t = fitting.guess_init(body_model, gt_joints, edge_indices,
                                use_vposer=use_vposer, vposer=vposer,
                                pose_embedding=pose_embedding,
                                model_type=kwargs.get('model_type', 'smpl'),
                                focal_length=focal_length, dtype=dtype)

    camera_loss = fitting.create_loss('camera_init',
                                      trans_estimation=init_t,
                                      init_joints_idxs=init_joints_idxs,
                                      depth_loss_weight=depth_loss_weight,
                                      dtype=dtype).to(device=device)
    camera_loss.trans_estimation[:] = init_t

    loss = fitting.create_loss(loss_type=loss_type,
                               joint_weights=joint_weights,
                               rho=rho,
                               use_joints_conf=use_joints_conf,
                               use_face=use_face, use_hands=use_hands,
                               vposer=vposer,
                               pose_embedding=pose_embedding,
                               body_pose_prior=body_pose_prior,
                               shape_prior=shape_prior,
                               angle_prior=angle_prior,
                               expr_prior=expr_prior,
                               left_hand_prior=left_hand_prior,
                               right_hand_prior=right_hand_prior,
                               jaw_prior=jaw_prior,
                               interpenetration=interpenetration,
                               pen_distance=pen_distance,
                               search_tree=search_tree,
                               tri_filtering_module=filter_faces,
                               dtype=dtype,
                               **kwargs)
    loss = loss.to(device=device)

    print("tipe img")
    print(type(img))
    with fitting.FittingMonitor(
            batch_size=batch_size, visualize=visualize, background_image = img, camera = camera, **kwargs) as monitor:

        img = torch.tensor(img, dtype=dtype)

        H, W, _ = img.shape

        data_weight = 1000 / H
        # The closure passed to the optimizer
        camera_loss.reset_loss_weights({'data_weight': data_weight})

        # Reset the parameters to estimate the initial translation of the
        # body model
        body_model.reset_params(body_pose=body_mean_pose)

        # If the distance between the 2D shoulders is smaller than a
        # predefined threshold then try 2 fits, the initial one and a 180
        # degree rotation
        shoulder_dist = torch.dist(gt_joints[:, left_shoulder_idx],
                                   gt_joints[:, right_shoulder_idx])
        try_both_orient = shoulder_dist.item() < side_view_thsh

        # Update the value of the translation of the camera as well as
        # the image center.
        with torch.no_grad():
            camera.translation[:] = init_t.view_as(camera.translation)
            camera.center[:] = torch.tensor([W, H], dtype=dtype) * 0.5

        # Re-enable gradient calculation for the camera translation
        camera.translation.requires_grad = True

        camera_opt_params = [camera.translation, body_model.global_orient]

        camera_optimizer, camera_create_graph = optim_factory.create_optimizer(
            camera_opt_params,
            **kwargs)

        # The closure passed to the optimizer
        fit_camera = monitor.create_fitting_closure(
            camera_optimizer, body_model, camera, gt_joints,
            camera_loss, create_graph=camera_create_graph,
            use_vposer=use_vposer, vposer=vposer,
            pose_embedding=pose_embedding,
            return_full_pose=False, return_verts=False)

        # Step 1: Optimize over the torso joints the camera translation
        # Initialize the computational graph by feeding the initial translation
        # of the camera and the initial pose of the body model.
        camera_init_start = time.time()
        cam_init_loss_val = monitor.run_fitting(camera_optimizer,
                                                fit_camera,
                                                camera_opt_params, body_model,
                                                use_vposer=use_vposer,
                                                pose_embedding=pose_embedding,
                                                vposer=vposer)
        if interactive:
            if use_cuda and torch.cuda.is_available():
                torch.cuda.synchronize()
            tqdm.write('Camera initialization done after {:.4f}'.format(
                time.time() - camera_init_start))
            tqdm.write('Camera initialization final loss {:.4f}'.format(
                cam_init_loss_val))

        # If the 2D detections/positions of the shoulder joints are too
        # close the rotate the body by 180 degrees and also fit to that
        # orientation
        if try_both_orient:
            body_orient = body_model.global_orient.detach().cpu().numpy()
            flipped_orient = cv2.Rodrigues(body_orient)[0].dot(
                cv2.Rodrigues(np.array([0., np.pi, 0]))[0])
            flipped_orient = cv2.Rodrigues(flipped_orient)[0].ravel()

            flipped_orient = torch.tensor(flipped_orient,
                                          dtype=dtype,
                                          device=device).unsqueeze(dim=0)
            orientations = [body_orient, flipped_orient]
        else:
            orientations = [body_model.global_orient.detach().cpu().numpy()]

        # store here the final error for both orientations,
        # and pick the orientation resulting in the lowest error
        results = []

        # Step 2: Optimize the full model
        final_loss_val = 0
        for or_idx, orient in enumerate(tqdm(orientations, desc='Orientation')):
            opt_start = time.time()

            new_params = defaultdict(global_orient=orient,
                                     body_pose=body_mean_pose)
            body_model.reset_params(**new_params)
            if use_vposer:
                with torch.no_grad():
                    pose_embedding.fill_(0)

            for opt_idx, curr_weights in enumerate(tqdm(opt_weights, desc='Stage')):

                body_params = list(body_model.parameters())

                final_params = list(
                    filter(lambda x: x.requires_grad, body_params))

                if use_vposer:
                    final_params.append(pose_embedding)

                body_optimizer, body_create_graph = optim_factory.create_optimizer(
                    final_params,
                    **kwargs)
                body_optimizer.zero_grad()

                curr_weights['data_weight'] = data_weight
                curr_weights['bending_prior_weight'] = (
                    3.17 * curr_weights['body_pose_weight'])
                if use_hands:
                    joint_weights[:, 25:76] = curr_weights['hand_weight']
                if use_face:
                    joint_weights[:, 76:] = curr_weights['face_weight']
                loss.reset_loss_weights(curr_weights)

                closure = monitor.create_fitting_closure(
                    body_optimizer, body_model,
                    camera=camera, gt_joints=gt_joints,
                    joints_conf=joints_conf,
                    joint_weights=joint_weights,
                    loss=loss, create_graph=body_create_graph,
                    use_vposer=use_vposer, vposer=vposer,
                    pose_embedding=pose_embedding,
                    return_verts=True, return_full_pose=True)

                if interactive:
                    if use_cuda and torch.cuda.is_available():
                        torch.cuda.synchronize()
                    stage_start = time.time()
                final_loss_val = monitor.run_fitting(
                    body_optimizer,
                    closure, final_params,
                    body_model,
                    pose_embedding=pose_embedding, vposer=vposer,
                    use_vposer=use_vposer)

                if interactive:
                    if use_cuda and torch.cuda.is_available():
                        torch.cuda.synchronize()
                    elapsed = time.time() - stage_start
                    if interactive:
                        tqdm.write('Stage {:03d} done after {:.4f} seconds'.format(
                            opt_idx, elapsed))

            if interactive:
                if use_cuda and torch.cuda.is_available():
                    torch.cuda.synchronize()
                elapsed = time.time() - opt_start
                tqdm.write(
                    'Body fitting Orientation {} done after {:.4f} seconds'.format(
                        or_idx, elapsed))
                tqdm.write('Body final loss val = {:.5f}'.format(
                    final_loss_val))

            # Get the result of the fitting process
            # Store in it the errors list in order to compare multiple
            # orientations, if they exist
            result = {'camera_' + str(key): val.detach().cpu().numpy()
                      for key, val in camera.named_parameters()}
            result.update({key: val.detach().cpu().numpy()
                           for key, val in body_model.named_parameters()})
            if use_vposer:
                result['body_pose'] = pose_embedding.detach().cpu().numpy()

            results.append({'loss': final_loss_val,
                            'result': result})

        with open(result_fn, 'wb') as result_file:
            if len(results) > 1:
                min_idx = (0 if results[0]['loss'] < results[1]['loss']
                           else 1)
            else:
                min_idx = 0
            pickle.dump(results[min_idx]['result'], result_file, protocol=2)

    if save_meshes or visualize:
        body_pose = vposer.decode(
            pose_embedding,
            output_type='aa').view(1, -1) if use_vposer else None

        model_type = kwargs.get('model_type', 'smpl')
        append_wrists = model_type == 'smpl' and use_vposer
        if append_wrists:
                wrist_pose = torch.zeros([body_pose.shape[0], 6],
                                         dtype=body_pose.dtype,
                                         device=body_pose.device)
                body_pose = torch.cat([body_pose, wrist_pose], dim=1)

        model_output = body_model(return_verts=True, body_pose=body_pose)
        vertices = model_output.vertices.detach().cpu().numpy().squeeze()

        import trimesh

        out_mesh = trimesh.Trimesh(vertices, body_model.faces)
        rot = trimesh.transformations.rotation_matrix(
            np.radians(180), [1, 0, 0])
        out_mesh.apply_transform(rot)
        out_mesh.export(mesh_fn)
    visualize=True
    if visualize:
        import pyrender

        material = pyrender.MetallicRoughnessMaterial(
            metallicFactor=0.0,
            alphaMode='OPAQUE',
            baseColorFactor=(1.0, 1.0, 0.9, 1.0))
        mesh = pyrender.Mesh.from_trimesh(
            out_mesh,
            material=material)

        scene = pyrender.Scene(bg_color=[0.0, 0.0, 0.0, 0.0],
                               ambient_light=(0.3, 0.3, 0.3))
        scene.add(mesh, 'mesh')

        camera_center = camera.center.detach().cpu().numpy().squeeze()
        camera_transl = camera.translation.detach().cpu().numpy().squeeze()
        # Equivalent to 180 degrees around the y-axis. Transforms the fit to
        # OpenGL compatible coordinate system.
        camera_transl[0] *= -1.0

        camera_pose = np.eye(4)
        camera_pose[:3, 3] = camera_transl

        camera = pyrender.camera.IntrinsicsCamera(
            fx=focal_length, fy=focal_length,
            cx=camera_center[0], cy=camera_center[1])
        scene.add(camera, pose=camera_pose)

        # # Get the lights from the viewer
        light_nodes = create_raymond_lights()
        for node in light_nodes:
            scene.add_node(node)

        r = pyrender.OffscreenRenderer(viewport_width=W,
                                       viewport_height=H,
                                       point_size=1.0)
        color, _ = r.render(scene, flags=pyrender.RenderFlags.RGBA)
        color = color.astype(np.float32) / 255.0

        valid_mask = (color[:, :, -1] > 0)[:, :, np.newaxis]
        input_img = img.detach().cpu().numpy()
        output_img = (color[:, :, :-1] * valid_mask +
                      (1 - valid_mask) * input_img)

        img = pil_img.fromarray((output_img * 255).astype(np.uint8))
        print(out_img_fn)
        print(img)
        img.save(out_img_fn)
Example #49
0
 def resize_one(i, j):
     out[i, j] = np.array(
         Image.fromarray(tensor_cpu[i, j]).resize(
             (width, height), Image.BILINEAR))
Example #50
0
 def resize_channel(j):
     for i in range(tensor.size(0)):
         out[i, j] = np.array(
             Image.fromarray(tensor_cpu[i, j]).resize(
                 (width, height), Image.BILINEAR))
Example #51
0
 def save_to_PNG(self, path):
     Image.fromarray(self.get_FMC()).save(path,
                                          "PNG",
                                          compress_level=0,
                                          bits=16)
def run_surveillance(model_path, labelmap_path, show_video_steam, label_to_look_for, output_directory, threshold):
    # Load model
    graph, category_index = load_model(model_path, labelmap_path)
    # Initialize Video Capture
    cap = cv2.VideoCapture(0)
    # Create output directory if not already created
    os.makedirs(output_directory, exist_ok=True)
    os.makedirs(output_directory+'/images', exist_ok=True)

    if os.path.exists(output_directory+'/counts.csv'):
        df = pd.read_csv(output_directory+'/counts.csv')
    else:
        df = pd.DataFrame(columns=['year','month','day','hour','minute','second', 'orange','apple','banana'])
    # Open detection graph

    # Open detection graph
    with graph.as_default():
        with tf.Session() as sess:
            # Get handles to input and output tensors
            ops = tf.get_default_graph().get_operations()
            all_tensor_names = {output.name for op in ops for output in op.outputs}
            tensor_dict = {}
            for key in [
                'num_detections', 'detection_boxes', 'detection_scores',
                'detection_classes', 'detection_masks'
            ]:
                tensor_name = key + ':0'
                if tensor_name in all_tensor_names:
                    tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)
            while True:
                try:
                    # Read frame from video
                    ret, image_np = cap.read()

                    # Copy image for later
                    image_show = np.copy(image_np)

                    image_height, image_width, _ = image_np.shape
                    # Actual detection.
                    output_dict = run_inference_for_single_image(image_np, sess, tensor_dict)

                    if show_video_steam:
                        # Visualize results
                        vis_util.visualize_boxes_and_labels_on_image_array(
                            image_np,
                            output_dict['detection_boxes'],
                            output_dict['detection_classes'],
                            output_dict['detection_scores'],
                            category_index,
                            instance_masks=output_dict.get('detection_masks'),
                            use_normalized_coordinates=True,
                            line_thickness=8)
                        cv2.imshow('object_detection', cv2.resize(image_np, (800, 600)))
                        if cv2.waitKey(25) & 0xFF == ord('q'):
                            cap.release()
                            cv2.destroyAllWindows()
                            break

                    # Get data(label, xmin, ymin, xmax, ymax)
                    output = []
                    labelsOut = []
                    for index, score in enumerate(output_dict['detection_scores']):
                            if score < threshold:
                                continue
                            label = category_index[output_dict['detection_classes'][index]]['name']
                            ymin, xmin, ymax, xmax = output_dict['detection_boxes'][index]
                            output.append((label, int(xmin * image_width), int(ymin * image_height), int(xmax * image_width), int(ymax * image_height)))
                            labelsOut.append(label)
                    count0 = labelsOut.count('orange')
                    count1 = labelsOut.count('apple')
                    count2 = labelsOut.count('banana')
                    # Save incident (could be extended to send a email or something)
                    for l, x_min, y_min, x_max, y_max in output:
                        #if l == label_to_look_for:
                        array = cv2.cvtColor(np.array(image_show), cv2.COLOR_RGB2BGR)
                        image = Image.fromarray(array)
                        cropped_img = image.crop((x_min, y_min, x_max, y_max))
                        file_path = output_directory+'/images/'+str(len(df))+'.jpg'
                        #cropped_img.save(file_path, "JPEG", icc_profile=cropped_img.info.get('icc_profile'))
                        #df.loc[len(df)] = [datetime.datetime.now(), file_path]
                        #df.to_csv(output_directory+'/results.csv', index=None)
                        df.loc[len(df)] = [int(datetime.datetime.now().year),int(datetime.datetime.now().month),
                        int(datetime.datetime.now().day),int(datetime.datetime.now().hour),
                        int(datetime.datetime.now().minute),int(datetime.datetime.now().second),count0,count1,count2]
                        df.to_csv(output_directory+'/counts.csv', index=None)
                except Exception as e:
                    print(e)
                    cap.release()
                    cv2.destroyAllWindows()
                    break
    def __getitem__(self, idx):
        """
        Args: idx (int): Index in list to load image
        """
        # Load img, gt, x_coordinates, y_coordinates, line_type
        assert self.rgb_lst[idx].split('.')[0] == self.gt_lst[idx].split(
            '.')[0]
        img_name = os.path.join(self.image_dir, self.rgb_lst[idx])
        gt_name = os.path.join(self.gt_dir, self.gt_lst[idx])
        with open(img_name, 'rb') as f:
            image = (Image.open(f).convert('RGB'))
        with open(gt_name, 'rb') as f:
            gt = (Image.open(f).convert('P'))
        idx = int(self.rgb_lst[idx].split('.')[0]) - 1
        lanes_lst = self.ordered_lanes[idx]["lanes"]
        h_samples = self.ordered_lanes[idx]["h_samples"]
        line_lst = self.line_file[idx]["lines"]

        # Crop and resize images
        w, h = image.size
        image, gt = F.crop(image, h - 640, 0, 640,
                           w), F.crop(gt, h - 640, 0, 640, w)
        image = F.resize(image,
                         size=(self.resize, 2 * self.resize),
                         interpolation=Image.BILINEAR)
        gt = F.resize(gt,
                      size=(self.resize, 2 * self.resize),
                      interpolation=Image.NEAREST)

        # Adjust size of lanes matrix
        # lanes = np.array(lanes_lst)[:, -self.num_points:]
        lanes = np.array(lanes_lst)
        to_add = np.full((4, 56 - lanes.shape[1]), -2)
        lanes = np.hstack((to_add, lanes))

        # Get valid coordinates from lanes matrix
        valid_points = np.int32(lanes > 0)
        valid_points[:, :8] = 0  # start from h-samples = 210

        # Resize coordinates
        lanes = lanes / 2.5
        track = lanes < 0
        h_samples = np.array(h_samples) / 2.5 - 32
        lanes[track] = -2

        # Compute horizon for resized img
        horizon_lanes = []
        for lane in lanes:
            horizon_lanes.append(
                min([
                    y_cord for (x_cord, y_cord) in zip(lane, h_samples)
                    if x_cord != -2
                ] or [self.resize]))
        y_val = min(horizon_lanes)
        horizon = torch.zeros(gt.size[1])
        horizon[0:int(np.floor(y_val))] = 1

        # Compute line type in image
        # line_lst = np.prod(lanes == -2, axis=1) # 1 when line is not present

        gt = np.array(gt)
        idx3 = np.isin(gt, 3)
        idx4 = np.isin(gt, 4)
        if self.nclasses < 3:
            gt[idx3] = 0
            gt[idx4] = 0
        # Flip ground truth ramdomly
        hflip_input = np.random.uniform(0.0, 1.0) > 0.5 and self.flip_on
        if idx not in self.valid_idx and hflip_input:
            image, gt = F.hflip(image), np.flip(gt, axis=1)
            idx1 = np.isin(gt, 1)
            idx2 = np.isin(gt, 2)
            gt[idx1] = 2
            gt[idx2] = 1
            gt[idx3] = 4
            gt[idx4] = 3
            lanes = (2 * self.resize - 1) - lanes
            lanes[track] = -2
            lanes = lanes[[1, 0, 3, 2]]
            # line_lst = np.prod(lanes == -2, axis=1)
            line_lst = mirror_list(line_lst)

        # Get Tensors
        gt = Image.fromarray(gt)
        image, gt = self.totensor(image).float(), (self.totensor(gt) *
                                                   255).long()

        # Cast to correct types
        line_lst = np.array(line_lst[3:7])
        line_lst = torch.from_numpy(np.array(line_lst + 1)).clamp(0, 1).float()
        valid_points = torch.from_numpy(valid_points).double()

        lanes = torch.from_numpy(lanes).double()
        horizon = horizon.float()

        if idx in self.valid_idx:
            index = self.valid_idx.index(idx)
            return image, gt, lanes, idx, line_lst, horizon, index, valid_points
        return image, gt, lanes, idx, line_lst, horizon, valid_points
Example #54
0
def main(args):
    # get device (GPU or CPU)
    if torch.cuda.is_available():
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
        torch.backends.cudnn.benchmark = True
        torch.backends.cudnn.enabled = True
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")

    # load model (MASKRCNN)
    print("... loading model")
    model = get_instance_segmentation_model(num_classes=2)
    model.to(device)
    model.load_state_dict(torch.load(args.ckp_path, map_location=torch.device('cpu')))
    model.eval()
    thres = float(args.thres)

    # load transform 
    transform = T.Compose([T.ToTensor(),
                           T.Normalize(
                            mean=[0.485, 0.456, 0.406],
                            std=[0.229, 0.224, 0.225]),
                        ])
    print("... loading", end=' ')

    # load camera
    cam_id = args.cam_id
    cam = cv2.VideoCapture(cam_id)
    assert cam.isOpened(), 'Cannot capture source'

    # inference 
    print("+++ Start inference !")
    while cam.isOpened():
        start_time = time.time()
        # load and transform image
        start_time = time.time()
        ret, img_arr = cam.read()
        IMG_H, IMG_W, IMG_C = img_arr.shape
        img_data = Image.fromarray(img_arr).convert("RGB")
        img_tensor = transform(img_data)
        img_tensor = img_tensor.unsqueeze(0).to(device)

        # forward and post-process results
        pred_result = model(img_tensor, None)[0]
        pred_mask = pred_result['masks'].cpu().detach().numpy().transpose(0, 2, 3, 1)
        pred_mask[pred_mask >= 0.5] = 1
        pred_mask[pred_mask < 0.5] = 0
        pred_mask = np.repeat(pred_mask, 3, 3)
        pred_scores = pred_result['scores'].cpu().detach().numpy()
        pred_boxes = pred_result['boxes'].cpu().detach().numpy()
        # pred_labels = pred_result['labels']

        # draw predictions
        ids = np.where(pred_scores > thres)[0]
        colors = np.random.randint(0, 255, (len(ids), 3))
        # set colors considering location and size of bbox 
        colors = []
        for (x1, y1, x2, y2) in pred_boxes: 
            w = max(x1, x2) - min(x1, x2)
            h = max(y1, y2) - min(y1, y2)
            x = (x1 + x2) / 2
            y = (y1 + y2) / 2
            ratio_x, ratio_y = x / IMG_W, y / IMG_H
            ratio_s = min(w, h) / max(w, h)
            ratio_s = 1 + ratio_s if ratio_s < 0 else ratio_s
            ratio_x, ratio_y, ratio_s = int(ratio_x*255), int(ratio_y*255), int(ratio_s*255)
            colors.append([ratio_x, ratio_y, ratio_s])

        for color_i, pred_i in enumerate(ids):
            color = tuple(map(int, colors[color_i]))
            # draw segmentation
            mask = pred_mask[pred_i] 
            mask = mask * color
            img_arr = cv2.addWeighted(img_arr, 1, mask.astype(np.uint8), 0.5, 0)
            # draw bbox 
            x1, y1, x2, y2 = map(int, pred_boxes[pred_i])
            cv2.rectangle(img_arr, (x1, y1), (x2, y2), color, 2)
            # put text
            vis_text = "FOOD({:.2f})".format(pred_scores[pred_i])
            cv2.putText(img_arr, vis_text, (x1+5, y1+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [255, 255, 255], 2)
            cv2.putText(img_arr, vis_text, (x1+5, y1+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)

        cv2.imshow('frame', img_arr)
        key = cv2.waitKey(1)
        if key == 27:
            break

        print("FPS is {:.2f} | Image Size:{}\t\t".format(
              1/(time.time()-start_time), img_arr.shape), end='\r')
    cam.release()
    cv2.destroyAllWindows()
def resize_binary_mask(array, new_size):
    image = Image.fromarray(array.astype(np.uint8)*255)
    image = image.resize(new_size)
    return np.asarray(image).astype(np.bool_)
def save_image(image_numpy, image_path):
    image_pil = Image.fromarray(image_numpy)
    image_pil.save(image_path)
Example #57
0
def generate_random_image(path: str):
    imarray = numpy.random.rand(100, 100, 3) * 255
    im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
    im.save(path)
                        scene_seg[:, :, 0] += scene_seg_hot[:, :, layer] * 255
                    if layer == 9:  #vegetation
                        scene_seg[:, :, 0] += scene_seg_hot[:, :, layer] * 255
                    if layer == 10:  #vehicles
                        scene_seg[:, :, 1] += scene_seg_hot[:, :, layer] * 255
                    if layer == 11:  #wall
                        scene_seg[:, :, 0] += scene_seg_hot[:, :, layer] * 255
                    if layer == 12:  #traffic sign
                        scene_seg[:, :, 0] += scene_seg_hot[:, :, layer] * 255

            #rand = random.random()
            #if (outdim == 3):
            #Image.fromarray(segs_center[i]).convert('RGB').save("seg_" + str(i) + ".png")
            '''
      if (outdim == 1):
	if h_num < h5_last*0.2:
	  Image.fromarray(rgb).save(testPath + name + "_rgb_" + str(i+h_num*200) + "_W8.png")
      	  Image.fromarray(scene_seg).save(testAnnotPath + name + "_seg_" + str(i+h_num*200) + "_W8.png") 
	elif h_num < h5_last*0.4:
	  Image.fromarray(rgb).save(valPath + name + "_rgb_" + str(i+h_num*200) + "_W8.png")
      	  Image.fromarray(scene_seg).save(valAnnotPath + name + "_seg_" + str(i+h_num*200) + "_W8.png") 
	else:
	  Image.fromarray(rgb).save(trainPath + name + "_rgb_" + str(i+h_num*200) + "_W8.png")
      	  Image.fromarray(scene_seg).save(trainAnnotPath + name + "_seg_" + str(i+h_num*200) + "_W8.png") 
      '''
            Image.fromarray(rgb).save(trainPath + name + "_rgb_" +
                                      str(i + h_num * 200) + ".png")
            #Image.fromarray(scene_seg).save(trainAnnotPath + name + "_seg_" + str(i+h_num*200) + ".png")
            #Image.fromarray(rgb).save(valPath + name + "_rgb_" + str(i+h_num*200) + ".png")
            #Image.fromarray(scene_seg).save(valAnnotPath + name + "_seg_" + str(i+h_num*200) + ".png")
Example #59
0
 def work(self, data, filename):
     Image.fromarray(data).save(filename, optimize=True, quality=100)
Example #60
0
BOARD_WIDTH = 100

distrib = torch.distributions.Bernoulli(0.7)

weights = torch.tensor([[1,1,1],[1,10,1],[1,1,1]]).view(1,1,3,3)
board = distrib.sample((BOARD_HEIGHT,BOARD_WIDTH)).view(1,1,BOARD_HEIGHT,BOARD_WIDTH)
board = board.to(torch.int64)

cv2.namedWindow("game", cv2.WINDOW_NORMAL)

if False:
    data = torch.load('train.data')
    data.append(board)
else:
    data = [board]

while True:
    newboard = F.conv2d(board, weights, padding=1).view(BOARD_HEIGHT,BOARD_WIDTH)
    newboard = (newboard==12) | (newboard==3) | (newboard==13)
    newboard_array = np.int8(newboard) * 255
    img = Image.fromarray(newboard_array).convert('RGB')
    img = np.array(img)
    cv2.imshow("game", img)
    q = cv2.waitKey(100)
    if q == 113: # 'q'
        cv2.destroyAllWindows()
        break
    board = torch.tensor(newboard_array/255, dtype=torch.int64).view(1,1,BOARD_HEIGHT,BOARD_WIDTH)
    data.append(board)

torch.save(data, 'train.data')