def testRigidTransformationMultiscale(dTheta, displacement, level):
    inImg=misc.imread('T2sample.png')[...,0]
    left=ndimage.rotate(inImg, -0.5*dTheta)#Rotate symmetricaly to ensure both are still the same size
    right=ndimage.rotate(inImg, 0.5*dTheta)#Rotate symmetricaly to ensure both are still the same size
    right=ndimage.affine_transform(right, np.eye(2), offset=-1*displacement)
    rightPyramid=[i for i in transform.pyramid_gaussian(right, level)]
    leftPyramid=[i for i in transform.pyramid_gaussian(left, level)]
    rcommon.plotPyramids(leftPyramid, rightPyramid)
    beta=estimateRigidTransformationMultiscale(leftPyramid, rightPyramid)
    print 180.0*beta[0]/np.pi, beta[1:3]
    return beta
def testRigidTransformEstimation(inImg, level, dTheta, displacement, thr):
    left=ndimage.rotate(inImg, dTheta)
    right=ndimage.rotate(inImg, -dTheta)
    left=ndimage.affine_transform(left , np.eye(2), offset=-1*displacement)
    right=ndimage.affine_transform(right, np.eye(2), offset=displacement)
    
    rightPyramid=[i for i in transform.pyramid_gaussian(right, level)]
    leftPyramid=[i for i in transform.pyramid_gaussian(left, level)]
    sel=level
    beta=estimateRigidTransformation(leftPyramid[sel], rightPyramid[sel], 2.0*dTheta, thr)
    return beta
def test_build_gaussian_pyramid():
    rows, cols, dim = image.shape
    pyramid = pyramid_gaussian(image, downscale=2)

    for layer, out in enumerate(pyramid):
        layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim)
        assert_array_equal(out.shape, layer_shape)
  def get_face(self, do_rot=True, do_scale=True):
    frame = self.ig.getFrame()
    frame_pyramid = list(tf.pyramid_gaussian(frame, max_layer=NUM_PYR, downscale=2))

    scale_ssds = {}
    for i, face_pyramid in enumerate(self.scaled_face_pyramids):
      if not do_scale and i != 1:
          continue
      res = self.determine_best_shift(face_pyramid, frame_pyramid)
      best_i, best_j, best_ssd = res
      scale_ssds[i] = (1.0 / (best_ssd * self.scaled_weights[i]), best_i, best_j, np.array(face_pyramid[0].shape))
    if len(scale_ssds) == 3 or not do_scale:
      best_i, best_j = scale_ssds[1][1], scale_ssds[1][2]
    else:
      best_i, best_j = scale_ssds[0][1], scale_ssds[0][2]
    total = sum([v[0] for v in scale_ssds.values()])
    interp_shape = sum([v[0] / total * v[3] for v in scale_ssds.values()])

    rot_ssds = {}
    for i, face_pyramid in enumerate(self.rotated_face_pyramids):
      if not do_rot and i != 1:
          continue
      res = self.determine_best_shift(face_pyramid, frame_pyramid)
      rot_best_i, rot_best_j, best_ssd = res
      rot_ssds[i] = (1.0 / best_ssd, rot_best_i, rot_best_j, np.array(face_pyramid[0].shape))
    total = sum([v[0] for v in rot_ssds.values()])
    interp_rot = sum([v[0] / total * ROT_AMTS[k] for k, v in rot_ssds.items()])

    return best_i, best_j, frame, interp_shape, interp_rot
def save_pyramid () :
    global temp_line
    global pyramids
    global patchNum
    global total_patch
    global total_pyramid

    org_img = Image.open("%s/%s.jpg" %(base_path, temp_line), 'r' )
    
    org_img_name = "%s " %(temp_line)        # original image name
    # describ_file.write ( "%s\n" %org_img_name )  # original image name
    
    pyramids = list( pyramid_gaussian(org_img, downscale=math.sqrt(2) ) )
    for i in range(len(pyramids) ):
        if min( pyramids[i].shape[0], pyramids[i].shape[1] ) < 30 :
            del pyramids[i:]
            break
    
    for i in range( len (pyramids) ) :
        row = pyramids[i].shape[0]
        col = pyramids[i].shape[1]
        im_matrix = np.zeros([row, col, 3]).astype('uint8')
    
        for k in range(row):
            for j in range(col):
                im_matrix[k,j] = pyramids[i][k,j] * 255
    
        new_img = Image.fromarray(im_matrix)
          # new_img.save("%s/pyramid-%s.jpg" %(patch_path, i+total_patch) )
        new_img.save("%s/pyramid-%s.jpg" %(patch_path, i+total_pyramid) )
        # new_img.show()
    
        patchNum[i] = (row-30+1) * (col-30+1)                  # the number of patches
    total_pyramid = total_pyramid + len(pyramids)
    total_patch = total_patch + sum(patchNum)
Exemple #6
0
    def gaussian_pyramid(self, n_levels=3, downscale=2, sigma=None, order=1, mode="reflect", cval=0):
        r"""
        Return the gaussian pyramid of this image. The first image of the
        pyramid will be the original, unmodified, image.

        Parameters
        ----------
        n_levels : int
            Number of levels in the pyramid. When set to -1 the maximum
            number of levels will be build.

            Default: 3

        downscale : float, optional
            Downscale factor.

            Default: 2

        sigma : float, optional
            Sigma for gaussian filter. Default is `2 * downscale / 6.0` which
            corresponds to a filter mask twice the size of the scale factor
            that covers more than 99% of the gaussian distribution.

            Default: None

        order : int, optional
            Order of splines used in interpolation of downsampling. See
            `scipy.ndimage.map_coordinates` for detail.

            Default: 1

        mode :  {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
            The mode parameter determines how the array borders are handled,
            where cval is the value when mode is equal to 'constant'.

            Default: 'reflect'

        cval : float, optional
            Value to fill past edges of input if mode is 'constant'.

            Default: 0

        Returns
        -------
        image_pyramid:
            Generator yielding pyramid layers as menpo image objects.
        """
        max_layer = n_levels - 1
        pyramid = pyramid_gaussian(
            self.pixels, max_layer=max_layer, downscale=downscale, sigma=sigma, order=order, mode=mode, cval=cval
        )

        for j, image_data in enumerate(pyramid):
            image = self.__class__(image_data)

            # rescale and reassign existent landmark
            image.landmarks = self.landmarks
            transform = UniformScale(downscale ** j, self.n_dims)
            transform.pseudoinverse.apply_inplace(image.landmarks)
            yield image
Exemple #7
0
def main():
    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True, help="Path to the image")
    ap.add_argument("-s", "--scale", type=float, default=1.5, help="scale factor size")
    args = vars(ap.parse_args())

    # load the image
    image = cv2.imread(args["image"])

    # METHOD #1: No smooth, just scaling.
    # loop over the image pyramid
    for (i, resized) in enumerate(pyramid(image, scale=args["scale"])):
        # show the resized image
        cv2.imshow("Layer {}".format(i + 1), resized)
        cv2.waitKey(0)

    # close all windows
    cv2.destroyAllWindows()

    # METHOD #2: Resizing + Gaussian smoothing.
    for (i, resized) in enumerate(pyramid_gaussian(image, downscale=2)):
        # if the image is too small, break from the loop
        if resized.shape[0] < 30 or resized.shape[1] < 30:
            break

        # show the resized image
        cv2.imshow("Layer {}".format(i + 1), resized)
        cv2.waitKey(0)
  def calibrate(self):
    self.ig = WebcamImageGetter()
    self.ig.start()
    self.init_interp_shape = None
    print "Place face 1 ft from camera. When face is visible, press Enter to continue."
    while True:
      frame = self.ig.getFrame()
      if frame is None:
        continue
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")
      faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
      for x, y, w, h in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), color=(255, 0, 0), thickness=2)
      cv2.imshow("calibration", frame)
      if cv2.waitKey(1) & 0xFF == 10:
        cv2.destroyWindow("calibration")
        if len(faces) > 0:
          break
        else:
          print "No face detected."

    x, y, w, h = faces[0]
    num_pix = float(w*h)
    face_roi = frame[y:y+h, x:x+w]
    rotated_faces = [tf.rotate(face_roi, angle=rot_ang) for rot_ang in ROT_AMTS]
    self.rotated_face_pyramids = [list(tf.pyramid_gaussian(face, max_layer=NUM_PYR, downscale=2))
                                  for face in rotated_faces]
    scaled_faces = [tf.rescale(face_roi, scale=sc) for sc in RESCALING_FACTORS]
    self.scaled_face_pyramids = [list(tf.pyramid_gaussian(face, max_layer=NUM_PYR, downscale=2))
                                 for face in scaled_faces]
    # scaled_weights are used for scaled_faces
    self.scaled_weights = [num_pix / (sf.shape[0]*sf.shape[1]) for sf in scaled_faces]
    # we observed that the small detector is too strong, so we penalize it more
    self.scaled_weights[0] *= 1.5
    # w = f*Y/Z  -->  f = wZ/Y
    self.camera_f = w * START_FACE_DIST/AVERAGE_FACE_WIDTH
    self.start_center = np.array((x + w/2.0, y+h/2.0))
    self.w = w; self.h = h
    cv2.destroyWindow("calibration")
    cv2.waitKey(1)
    cv2.destroyWindow("calibration")
    cv2.waitKey(1)
    print "Tracking face...press Enter to quit."
    print "Red: close, green: far, blue: in between."
Exemple #9
0
def getpyramidImage(image, d, fname):
	
	path = fname + "/t%03d"%d + "_GaussianPyramidLevel"

	for (i, resized) in enumerate(pyramid_gaussian(image, downscale=2)):
		#if resized.shape[0] < 30 or resized.shape[1] < 30:
		if i > 4 :
		 	break
		#imsave("t000_GaussianPyramidLevel%i.tif"%i,resized)
		vigra.impex.writeVolume(convert(resized),path +"%i.tif"%i,'')
def gaussian_downsample(frames, pyramid_levels=4):
    nt = frames.shape[0]
    for ii, frame in enumerate(frames):
        pyr = transform.pyramid_gaussian(frame.astype(np.float))
        for jj in xrange(pyramid_levels + 1):
            ds = pyr.next()
        if ii == 0:
            out = np.empty((nt,) + ds.shape, dtype=np.float)
        out[ii] = ds
    return out
def create_image_pyramid(img, downscale_dim=2, pyramid_layer=3):
    # I put in some automatic values for the definition call. I need to check that they actually work.
    """ Create image pyramid"""


    pyramid = tuple(pyramid_gaussian(pyramid_in, downscale=downscale_dim))

    """ Check pyramid results """
    #Also need to put some test cases that check that downscale_dim and pyramid_layer are correct values.

    return(pyramid)
def augment_data(img):
  rotate_angles = [0, 45, 90, 135, 180, 225, 270, 315]
  scales = 4 # number of downsampling scales
  flip_flags = [True, False]
  cnt = 0
  output_imgs, output_filenames = {}, {}

  for f in flip_flags:
    if f:
      arr_img = util.PIL2array(img)
      # plt.imshow(arr_img)
      f_img = flip_image(arr_img)
      # plt.imshow(f_img)
      f_img = util.array2PIL(f_img)

      """
      # Optional: using affine transformation
      # shear by 180 degrees is equivalent to rotation by 180 degrees + flip.
      # So after that we rotate it another 180 degrees to get just the flip.
      shear = 180
      rotation = 180
      tform_augment = transform.AffineTransform(scale=(1, 1), rotation=np.deg2rad(rotation),
                                               shear=np.deg2rad(shear), translation=(0, 0))
      f_img = transform.warp(arr_img, tform_augment, mode='constant', order=3)
      plt.imshow(f_img)

      """
    else:
      f_img = img

    pyramid = tuple(transform.pyramid_gaussian(f_img, downscale=2))
    for p in xrange(scales):
      H, W, chs = pyramid[p].shape
      p_img = util.array2PIL(pyramid[p])
      #plt.imshow(pyramid[p])
      #p_img.show()
      for angle in rotate_angles:
        output = p_img.rotate(angle, expand=True)
        output = output.resize((58, 58))
        output_imgs[cnt] = output
        # output.show()
        """
        if f:
          output.save('samples/' + 'flipped'+ '_p' + str(p+1) + '_r' + str(angle) + '.jpg')
        else:
          output.save('samples/' + 'p' + str(p + 1) + '_r' + str(angle) + '.jpg')
        """
        if f:
          output_filenames[cnt] = 'flipped' + '_p' + str(p + 1) + '_r' + str(angle) + '.jpg'
        else:
          output_filenames[cnt] = 'p' + str(p + 1) + '_r' + str(angle) + '.jpg'

        cnt += 1
  return output_imgs, output_filenames
def getGaussianPyramidOfList(imageList,amountOfLayers):
	listOfGaussiansPyramids = list()
	for i in range(1,amountOfLayers+1):
		currentLayer = list()
		for currentImage in imageList:
			gaussianImage = tuple(transform.pyramid_gaussian(currentImage,max_layer=i))
			currentLayer.append(gaussianImage[-1])
		listOfGaussiansPyramids.append(currentLayer)

	return listOfGaussiansPyramids
		
def testEstimateRotationMultiscale(dTheta, level):
    #inImg=misc.imread('stinkbug.png')[...,0]
    inImg=misc.imread('T2sample.png')[...,0]
    left=ndimage.rotate(inImg, -dTheta/2.0)
    right=ndimage.rotate(inImg, dTheta/2.0)
    rightPyramid=[i for i in transform.pyramid_gaussian(right, level)]
    leftPyramid=[i for i in transform.pyramid_gaussian(left, level)]
    angles=[]
    theta=estimateRotationMultiscale(leftPyramid, rightPyramid, 0, angles)
    angles=180*np.array(angles).reshape(len(angles),1)/np.pi
    xticks=[str(i) for i in range(level+1)[::-1]]
    plt.figure()
    plt.plot(angles)
    plt.xlabel('Scale')
    plt.ylabel('Estimated angle')
    plt.title('Global rotation [GT/Est.]: '+str(dTheta)+'/'+str(180*theta/np.pi)+' deg. Levels: '+str(level+1))
    plt.xticks(range(level+1), xticks)
    plt.grid()
    #printPyramids(leftPyramid, rightPyramid)
    print 'Estimated:\n', angles,' degrees'
    return 180*theta/np.pi
    def image_pyramid_down(image, downscale=1.5, min_size=(30, 30)):
        """
        Method to downscale images and yield scaled images down to the provided min_size
        :param image: Image to downscale
        :param downscale: Downscale factor
        :param min_size: Minimum image size
        :return: Generator with scaled images
        """
        for i, resized_image in enumerate(pyramid_gaussian(image, downscale=downscale)):
            if resized_image.shape[0] < min_size[1] or resized_image.shape[1] < min_size[0]:
                break

            yield i, resized_image
Exemple #16
0
def test_collection_viewer():

    img = data.astronaut()
    img_collection = tuple(pyramid_gaussian(img))

    view = CollectionViewer(img_collection)
    make_key_event(48)

    view.update_index('', 2),
    assert_equal(view.image, img_collection[2])
    view.keyPressEvent(make_key_event(53))
    assert_equal(view.image, img_collection[5])
    view._format_coord(10, 10)
Exemple #17
0
def kMeans(img):
	t0 = time.time()
	# apply kMeans, fit data, get histogram, get color bar
	org_img = img
	img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
	z = img.reshape((-1, 3))
	#print(z.shape)

	# image resize just for silhouetteCoeff
	# Crops images to 300x300, but loses accuracy
	# Try pyrDown (downsampling the images)
	"""ysize, xsize, chan = img.shape
	if ysize and xsize > 300:
		xoff = (xsize - 300) // 2
		yoff = (ysize - 300) // 2
		y = img[yoff:-yoff, xoff:-xoff]
	else:
		y = img
	y = y.reshape((-1, 3))
	print(y.shape)"""

	# downnsample images with gaussian smoothing
	if (img.shape[0] > 250 or img.shape[1] > 250):
		for (i, resized) in enumerate(pyramid_gaussian(org_img, downscale=2)):
			if resized.shape[0] < 100 or resized.shape[1] < 100:
				break
			org_img = resized
		#cv2.imshow("Layer {}".format(i + 1), resized)
		#print(org_img.shape)

	org_img = org_img.reshape((-1, 3))
	org_img = scale(org_img)
	#print(org_img.shape)
	# kmeans
	clt = KMeans(n_clusters = silhouetteCoeff(org_img), random_state = 42)
	clt.fit(z)
	#print(clt.cluster_centers_)

	hist = centroidHistogram(clt)
	bar = plotColors(hist, clt.cluster_centers_)
	print("Time including KMeans: ", time.time() - t0)
	#print("unique labels: ", np.unique(np.array(clt.labels_), axis=0))

	plt.figure(1)
	plt.axis("off")
	plt.subplot(211)
	plt.imshow(img)
	plt.subplot(212)
	plt.imshow(bar)
	plt.show()
def compute_gaussian_pyramid(img, min_size):
    h, w = img.shape[0:2]
    curr_size = np.min([h, w])
    levels = 0

    while curr_size > min_size:
        curr_size = np.floor(curr_size / 2.0)
        levels += 1

    img_pyr = list(pyramid_gaussian(img, max_layer=levels))

    img_pyr.reverse()  # smallest to largest

    assert np.min(img_pyr[1].shape[:2]) > min_size
    assert np.min(img_pyr[1].shape[:2]) <= np.min(img_pyr[-1].shape[:2])

    return img_pyr
def create_ns (tmp_imgpath, cnt_ns ) :
	global pyramids

	tmp_img = Image.open("%s/%s" %(coco_path, tmp_imgpath), 'r' )
	pyramids = list( pyramid_gaussian( tmp_img, downscale=math.sqrt(2) ) )

	for i in range ( len(pyramids) ):
		if min( pyramids[i].shape[0], pyramids[i].shape[1] ) < MinFace :
			del pyramids[i:]
			break

	# for j in range(4) :
	for j in range(36) :
		# creating random index
		img_index = random.randint(0, len(pyramids)-1 )
		tmp_patch_num = ( pyramids[img_index].shape[0] - 12 + 1) * ( pyramids[img_index].shape[1] - 12 + 1)
		rand_index = random.randint(0, tmp_patch_num)

		# x, y position decoding
		row_max = pyramids[img_index].shape[0]
		col_max = pyramids[img_index].shape[1]
		row = 0
		col = rand_index
		
		while ( col >= col_max - 12 +1 ) :
			row = row + 1
			col = col - (col_max-12+1)

		flag = 0
		# Rejecting Black and White image
		tmp_ns = pyramids[img_index][row:row+12, col:col+12]
		if not len(tmp_ns.shape)==3 :
			print " Gray Image. Skip "
			return 0

		# Rejecting Positive Samples
		scale_factor = math.sqrt(2)**img_index

		tmp_ns = pyramids[img_index][row:row+12, col:col+12]
		tmp_ns = Image.fromarray((tmp_ns*255.0).astype(np.uint8) )
		# tmp_ns = tmp_ns.resize( (12,12), Image.BICUBIC )
		tmp_ns = tmp_ns.resize( (12,12), Image.BILINEAR )
		tmp_ns.save("%s/ns-%s.jpg" %(ns_path, cnt_ns+j) )

	return 1
def scale_pyramid(im_path):

    detections = []
    downscale = 1.5    
    
    # The current scale of the image
    scale = 0

    im = imread(im_path, as_grey=True)
    	
    top_2 = []

    # Downscale the image and iterate
    for im_scaled in pyramid_gaussian(im, downscale=downscale):

        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[1] < min_wdw_sz[0]:
            break

        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz, step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[1] != min_wdw_sz[0]:
                continue

            # Calculate the HOG features
            fd = hog(im_window, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
            pred = clf.predict(fd)
            dec_score = clf.decision_function(fd)

            if(dec_score[0][pred - 1] > 0.5):
            	new_tuple = (x, y, dec_score[0][pred - 1], int(min_wdw_sz[0]*(downscale**scale)),
                	int(min_wdw_sz[1]*(downscale**scale)), pred)

            	if len(top_2) < 2:
            		top_2.append(new_tuple)
            	else:
            		if new_tuple[2] > top_2[0][2] and pred != top_2[0][5]:
            			top_2[0] = new_tuple
            		elif new_tuple[2] > top_2[1][2] and pred != top_2[0][5]:
            			top_2[1] = new_tuple
            		else:
            			continue
            
        scale+=1.25

    return top_2
    def test_pyramid_process(self):
        image = mh.imread(
            '/home/zo/PycharmProjects/cal-app/person/zj/learn/python-ml/vision4op/image/true/line_rect.jpg')  # IMG_1962.JPG')  # treegroup_1.JPG')

        #image = image[:, :, 0]
        rows, cols, dim = image.shape  #获取图片的行数,列数和通道数
        print rows, cols, dim
        pyramid = tuple(transform.pyramid_gaussian(image, downscale=1.1))  #产生高斯金字塔图像
        #共生成了log(512)=9幅金字塔图像,加上原始图像共10幅,pyramid[0]-pyramid[1]

        i_row = 0
        for p in pyramid[1:]:
            rows, cols, dim = p.shape  # 获取图片的行数,列数和通道数
            print rows, cols, dim
            print p
            #p = p[:,:,0]
            #thred = p.mean()
            #p = (p > thred)
            plt.imshow(p)
            plt.show()
"""
Displays an image pyramid
"""

from skimage import data
from skimage.util import img_as_ubyte
from skimage.color import rgb2gray
from skimage.transform import pyramid_gaussian
import napari
import numpy as np

# create pyramid from astronaut image
astronaut = data.astronaut()
base = np.tile(astronaut, (3, 3, 1))
pyramid = list(
    pyramid_gaussian(base, downscale=2, max_layer=3, multichannel=True))
pyramid = [
    np.array([p * (abs(3 - i) + 1) / 4 for i in range(6)]) for p in pyramid
]
print('pyramid level shapes: ', [p.shape for p in pyramid])

with napari.gui_qt():
    # create the viewer
    viewer = napari.Viewer()

    # add image pyramid
    viewer.add_pyramid(pyramid, contrast_limits=[0, 255])
Exemple #23
0
The ``pyramid_gaussian`` function takes an image and yields successive images
shrunk by a constant scale factor. Image pyramids are often used, e.g., to
implement algorithms for denoising, texture discrimination, and scale-
invariant detection.

"""
import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.transform import pyramid_gaussian

image = data.astronaut()
rows, cols, dim = image.shape
pyramid = tuple(pyramid_gaussian(image, downscale=2))

composite_image = np.zeros((rows, cols + cols / 2, 3), dtype=np.double)

composite_image[:rows, :cols, :] = pyramid[0]

i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p
    i_row += n_rows

fig, ax = plt.subplots()
ax.imshow(composite_image)
plt.show()
Exemple #24
0
 def _build_pyramid(self, image):
     image = _prepare_grayscale_input_2D(image)
     return list(pyramid_gaussian(image, self.n_scales - 1, self.downscale))
Exemple #25
0
implement algorithms for denoising, texture discrimination, and scale-invariant
detection.

"""
import math

import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.transform import pyramid_gaussian


image = data.astronaut()
rows, cols, dim = image.shape
pyramid = tuple(pyramid_gaussian(image, downscale=2, channel_axis=-1))

#####################################################################
# Generate a composite image for visualization
# ============================================
#
# For visualization, we generate a composite image with the same number of rows
# as the source image but with ``cols + pyramid[1].shape[1]`` columns. We then
# have space to stack all of the dowsampled images to the right of the
# original.
#
# Note: The sum of the number of rows in all dowsampled images in the pyramid
# may sometimes exceed the original image size in cases when image.shape[0] is
# not a power of two. We expand the number of rows in the composite slightly as
# necessary to account for this. Expansion beyond the number of rows in the
# original will also be necessary to cover cases where downscale < 2.
Exemple #26
0
def test_classifier(args=object):
    for im_path in [
            os.path.join(args.DIR_PATHS["TEST_IMG_DIR_PH"], i)
            for i in os.listdir(args.DIR_PATHS["TEST_IMG_DIR_PH"])
            if not i.startswith('.')
    ]:
        # Read the Image
        im = Image.open(im_path).convert('L')
        im = np.array(resize_by_short(im))

        clf = joblib.load(args.MODEL_PH)  # Load the classifier
        detections = []  # List to store the detections
        scale = 0  # The current scale of the image

        # Downscale the image and iterate
        for im_scaled in pyramid_gaussian(im, downscale=args.DOWNSCALE):
            cd = []  # This list contains detections at the current scale
            # If the width or height of the scaled image is less than
            # the width or height of the window, then end the iterations.
            if im_scaled.shape[0] < args.MIN_WDW_SIZE[1] or im_scaled.shape[
                    1] < args.MIN_WDW_SIZE[0]:
                break
            for (x, y, im_window) in sliding_window(im_scaled,
                                                    args.MIN_WDW_SIZE,
                                                    args.STEP_SIZE):
                if im_window.shape[0] != args.MIN_WDW_SIZE[
                        1] or im_window.shape[1] != args.MIN_WDW_SIZE[0]:
                    continue
                # Calculate the HOG features
                fd = feature_extraction.process_image(im_window,
                                                      args).reshape([1, -1])
                pred = clf.predict(fd)
                if pred == 1:
                    if args.IF_PRINT:
                        print("==> Detection:: Location -> ({}, {})".format(
                            x, y))
                    if args.CLF_TYPE is "LIN_SVM":
                        if args.IF_PRINT:
                            print("==> Scale ->  {} Confidence Score {} \n".
                                  format(scale, clf.decision_function(fd)))
                        detections.append((x, y, clf.decision_function(fd),
                                           int(args.MIN_WDW_SIZE[0] *
                                               (args.DOWNSCALE**scale)),
                                           int(args.MIN_WDW_SIZE[1] *
                                               (args.DOWNSCALE**scale))))
                    elif args.CLF_TYPE is "MLP":
                        if args.IF_PRINT:
                            print("==> Scale ->  {} Confidence Score {} \n".
                                  format(scale,
                                         clf.predict_proba(fd)[0]
                                         [1]))  #clf.decision_function(fd)))
                        detections.append((x, y, clf.predict_proba(fd)[0][1],
                                           int(args.MIN_WDW_SIZE[0] *
                                               (args.DOWNSCALE**scale)),
                                           int(args.MIN_WDW_SIZE[1] *
                                               (args.DOWNSCALE**scale))))
                    cd.append(detections[-1])

                # If visualize is set to true, display the working of the sliding window
                if args.VISUALIZE:
                    clone = im_scaled.copy()
                    for x1, y1, _, _, _ in cd:
                        # Draw the detections at this scale
                        cv2.rectangle(
                            clone, (x1, y1),
                            (x1 + im_window.shape[1], y1 + im_window.shape[0]),
                            (0, 0, 0),
                            thickness=2)
                    cv2.rectangle(
                        clone, (x, y),
                        (x + im_window.shape[1], y + im_window.shape[0]),
                        (255, 255, 255),
                        thickness=2)
                    cv2.imshow("Sliding Window in Progress", clone)
                    cv2.waitKey(30)

            # Move the the next scale
            scale += 1

        # Display the results before performing NMS
        clone = im.copy()

        # Draw the detections
        for (x_tl, y_tl, _, w, h) in detections:
            cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 0, 0),
                          thickness=2)

        detections_fin = nms(detections,
                             args.THRESHOLD)  # Perform Non Maxima Suppression

        # Display the results after performing NMS
        for (x_tl, y_tl, _, w, h) in detections_fin:
            # Draw the detections
            cv2.rectangle(clone, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 0, 0),
                          thickness=2)

        if args.VISUALIZE:
            cv2.imshow("Final Detections after applying NMS", clone)

        # print(os.path.split(im_path))
        print(
            os.path.join(args.DIR_PATHS['PRED_SAVE_PH'],
                         os.path.split(im_path)[1]))
        cv2.imwrite(
            os.path.join(args.DIR_PATHS['PRED_SAVE_PH'],
                         os.path.split(im_path)[1]), clone)
    # Read the image
    im = imread(args["image"], as_grey=False)
    min_wdw_sz = (100, 40)
    step_size = (10, 10)
    downscale = args['downscale']
    visualize_det = args['visualize']

    # Load the classifier
    clf = joblib.load(model_path)

    # List to store the detections
    detections = []
    # The current scale of the image
    scale = 0
    # Downscale the image and iterate
    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        # This list contains detections at the current scale
        cd = []
        # If the width or height of the scaled image is less than
        # the width or height of the window, then end the iterations.
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz, step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[1] != min_wdw_sz[0]:
                continue
            # Calculate the HOG features
            fd = hog(im_window, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
            pred = clf.predict(fd)
            if pred == 1:
                print  "Detection:: Location -> ({}, {})".format(x, y)
                print "Scale ->  {} | Confidence Score {} \n".format(scale,clf.decision_function(fd))
Exemple #28
0
    saver_calib_12.restore(sess,etc.save_dir+"12-net_calib.ckpt")
    saver_calib_24.restore(sess,etc.save_dir+"24-net_calib.ckpt")

   
    if cascade_lv == 2:
        print "Training finished"
        break

       
    print "Negative sample mining"
    neg_db = [0 for _ in xrange(len(neg_img))]
    neg_db_num = 0 
    for nid, img in enumerate(neg_img):
        print "cas_lv ", cascade_lv, " ", nid+1,"/",len(neg_img), "th image...", "neg_db size: ", neg_db_num, "thr_12: ", thr_12, "thr_24: ", thr_24
        pyramid = tuple(pyramid_gaussian(img, downscale = etc.downscale))
        neg_db_for_scale = [0 for _ in xrange(len(pyramid))]
        for scale in xrange(len(pyramid)): 
            
            X = pyramid[scale]
            img_row = np.shape(X)[0]
            img_col = np.shape(X)[1]
            
            if img_row < etc.img_size_12 or img_col < etc.img_size_12:
                break
            if etc.img_size_12 * (etc.downscale ** scale) < etc.face_minimum:
                continue
            
            win_list = view_as_windows(X, (etc.img_size_12, etc.img_size_12, etc.input_channel), etc.window_stride) 
            win_col = np.shape(win_list)[1]
            win_list = np.reshape(win_list, (-1, etc.input_channel * etc.img_size_12 * etc.img_size_12))
Exemple #29
0
    def extract_features(image):

        pyramid = pyramid_gaussian(image,
                                   max_layer=args.pyramid_levels,
                                   downscale=args.scale_factor_levels)

        score_maps = {}
        for (j, resized) in enumerate(pyramid):
            im = resized.reshape(1, resized.shape[0], resized.shape[1], 1)

            feed_dict = {
                input_network:
                im,
                phase_train:
                False,
                dimension_image:
                np.array([1, im.shape[1], im.shape[2]], dtype=np.int32),
            }

            im_scores = sess.run(maps, feed_dict=feed_dict)

            im_scores = geo_tools.remove_borders(im_scores,
                                                 borders=args.border_size)
            score_maps['map_' +
                       str(j + 1 + args.upsampled_levels)] = im_scores[0, :, :,
                                                                       0]

        if args.upsampled_levels:
            for j in range(args.upsampled_levels):
                factor = args.scale_factor_levels**(args.upsampled_levels - j)
                up_image = cv2.resize(image, (0, 0), fx=factor, fy=factor)

                im = np.reshape(up_image,
                                (1, up_image.shape[0], up_image.shape[1], 1))

                feed_dict = {
                    input_network:
                    im,
                    phase_train:
                    False,
                    dimension_image:
                    np.array([1, im.shape[1], im.shape[2]], dtype=np.int32),
                }

                im_scores = sess.run(maps, feed_dict=feed_dict)

                im_scores = geo_tools.remove_borders(im_scores,
                                                     borders=args.border_size)
                score_maps['map_' + str(j + 1)] = im_scores[0, :, :, 0]

        im_pts = []
        for idx_level in range(levels):

            scale_value = (args.scale_factor_levels**(idx_level -
                                                      args.upsampled_levels))
            scale_factor = 1. / scale_value

            h_scale = np.asarray([[scale_factor, 0., 0.],
                                  [0., scale_factor, 0.], [0., 0., 1.]])
            h_scale_inv = np.linalg.inv(h_scale)
            h_scale_inv = h_scale_inv / h_scale_inv[2, 2]

            num_points_level = point_level[idx_level]
            if idx_level > 0:
                res_points = int(
                    np.asarray(
                        [point_level[a]
                         for a in range(0, idx_level + 1)]).sum() -
                    len(im_pts))
                num_points_level = res_points

            im_scores = rep_tools.apply_nms(
                score_maps['map_' + str(idx_level + 1)], args.nms_size)
            im_pts_tmp = geo_tools.get_point_coordinates(
                im_scores, num_points=num_points_level, order_coord='xysr')

            im_pts_tmp = geo_tools.apply_homography_to_points(
                im_pts_tmp, h_scale_inv)

            if not idx_level:
                im_pts = im_pts_tmp
            else:
                im_pts = np.concatenate((im_pts, im_pts_tmp), axis=0)

        if args.order_coord == 'yxsr':
            im_pts = np.asarray(
                list(map(lambda x: [x[1], x[0], x[2], x[3]], im_pts)))

        im_pts = im_pts[(-1 * im_pts[:, 3]).argsort()]
        im_pts = im_pts[:args.num_points]

        # Extract descriptor from features
        descriptors = []
        im = image.reshape(1, image.shape[0], image.shape[1], 1)
        for idx_desc_batch in range(int(len(im_pts) / 250 + 1)):
            points_batch = im_pts[idx_desc_batch * 250:(idx_desc_batch + 1) *
                                  250]

            if not len(points_batch):
                break

            feed_dict = {
                input_network:
                im,
                phase_train:
                False,
                kpts_coord:
                points_batch[:, :2],
                kpts_scale:
                args.scale_factor * points_batch[:, 2],
                kpts_batch:
                np.zeros(len(points_batch)),
                dimension_image:
                np.array([1, im.shape[1], im.shape[2]], dtype=np.int32),
            }

            patch_batch = sess.run(input_patches, feed_dict=feed_dict)
            patch_batch = np.reshape(patch_batch,
                                     (patch_batch.shape[0], 1, 32, 32))
            data_a = torch.from_numpy(patch_batch)
            data_a = data_a.cuda()
            data_a = Variable(data_a)
            with torch.no_grad():
                out_a = model(data_a)
            desc_batch = out_a.data.cpu().numpy().reshape(-1, 128)
            if idx_desc_batch == 0:
                descriptors = desc_batch
            else:
                descriptors = np.concatenate([descriptors, desc_batch], axis=0)

        return im_pts, descriptors
Exemple #30
0
The ``pyramid_gaussian`` function takes an image and yields successive images
shrunk by a constant scale factor. Image pyramids are often used, e.g., to
implement algorithms for denoising, texture discrimination, and scale-invariant
detection.

"""
import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.transform import pyramid_gaussian


image = data.astronaut()
rows, cols, dim = image.shape
pyramid = tuple(pyramid_gaussian(image, downscale=2, multichannel=True))

composite_image = np.zeros((rows, cols + cols // 2, 3), dtype=np.double)

composite_image[:rows, :cols, :] = pyramid[0]

i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p
    i_row += n_rows

fig, ax = plt.subplots()
ax.imshow(composite_image)
plt.show()
def classityImage(img=None):
    _start = time.time()
    # img = cv2.imread('/home/gongxijun/data/object-detector/11/6442798_215617200193_2.jpg')
    global detections
    detections = []
    # print os.path.join('/home/gongxijun/data/object-detector/object-detector/webs/image', filename)
    # img = cv2.imread(os.path.join('/home/gongxijun/data/object-detector/object-detector/webs/image', filename));
    # print img
    scale_size = (max(img.shape[0], img.shape[1]) / 400.)  # 最大允许1000*1000的图像
    # # scale_size = 1 if scale_size < 1 else scale_size
    # if scale_size < 0.2:
    #     scale_size = 0.5;
    img = transform.rescale(img, 1.0 / scale_size)
    imt = color.rgb2gray(img)
    # job_manager = thread_pool.JobTaskManager(job_num=30, thread_num=cpu_count() << 1)
    # The current scale of the image
    scale = 0
    # Downscale the image and iterate
    # create thread and make thread num equal cpu kernel
    _step_size = step_size
    futures = []
    for im_scaled in pyramid_gaussian(imt, downscale=downscale):
        # This list contains detections at the current scale
        # im = imt
        # If the width or height of the scaled image is less than
        # the width or height of the window, then end the iterations.
        _step_size = (_step_size[0] / downscale, _step_size[1] / downscale)
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[1] < min_wdw_sz[0]:
            break
        for y in xrange(0, im_scaled.shape[0], step_size[1]):
            if y + min_wdw_sz[1] > im_scaled.shape[0]:
                break;
            for x in xrange(0, im_scaled.shape[1], step_size[0]):
                if x + min_wdw_sz[0] > im_scaled.shape[1]:
                    break;
                ##开启多线程.
                futures.append(job_manager.submit(get_similarIamgeRect,
                                                  (im_scaled, x, y, min_wdw_sz, downscale, scale)));

        # Move the the next scale
        scale += 1
    wait(futures)
    # print(wait(futures))
    # wait(futures)
    # job_manager._join_all();
    # del job_manager
    # job_manager.join()
    # while (job_manager.getthreadStatus()):
    #    time.sleep(0.1)
    font = cv2.FONT_HERSHEY_SIMPLEX  # 使用默认字体
    # Perform Non Maxima Suppression
    print "-----------------------------------------------------------"
    _num_status = True;
    while _num_status and len(detections) > 0:
        detections, _num_status = nms(detections, threshold)
    print "-----------------------------------------------------------"
    print "-----------------------------------------------------------"
    clone = img
    # Display the results after performing NMS
    for (x_tl, y_tl, _prod, w, h) in detections:
        cv2.rectangle(clone, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 255, 0), thickness=2)
        cv2.putText(clone, str(_prod), (x_tl, y_tl + 12), font, 0.5, (255, 0, 0), 1)
    _end = time.time()
    print   _end - _start
    return clone[:, :, ::-1] * 255
def generateData(train_dir, dataset_dir, scene, method_name):
    assert method_name in ['FgSegNet_M',
                           'FgSegNet_S'], 'method_name is incorrect'

    void_label = -1.

    # Given ground-truths, load training frames
    # ground-truths end with '*.png'
    # training frames end with '*.jpg'

    # given ground-truths, load inputs
    Y_list = glob.glob(os.path.join(train_dir, '*.png'))
    X_list = glob.glob(os.path.join(dataset_dir, 'input', '*.jpg'))

    if len(Y_list) <= 0 or len(X_list) <= 0:
        raise ValueError(
            'System cannot find the dataset path or ground-truth path. Please give the correct path.'
        )

    X_list_temp = []
    for i in range(len(Y_list)):
        Y_name = os.path.basename(Y_list[i])
        Y_name = Y_name.split('.')[0]
        Y_name = Y_name.split('gt')[1]
        for j in range(len(X_list)):
            X_name = os.path.basename(X_list[j])
            X_name = X_name.split('.')[0]
            X_name = X_name.split('in')[1]
            if (Y_name == X_name):
                X_list_temp.append(X_list[j])
                break

    X_list = X_list_temp

    if len(X_list) != len(Y_list):
        raise ValueError('The number of X_list and Y_list must be equal.')

    # X must be corresponded to Y
    X_list = sorted(X_list)
    Y_list = sorted(Y_list)

    # load training data
    X = []
    Y = []
    for i in range(len(X_list)):
        x = kImage.load_img(X_list[i])
        x = kImage.img_to_array(x)
        X.append(x)

        x = kImage.load_img(Y_list[i], grayscale=True)
        x = kImage.img_to_array(x)
        shape = x.shape
        x /= 255.0
        x = x.reshape(-1)
        idx = np.where(np.logical_and(x > 0.25, x < 0.8))[0]  # find non-ROI
        if (len(idx) > 0):
            x[idx] = void_label
        x = x.reshape(shape)
        x = np.floor(x)
        Y.append(x)

    X = np.asarray(X)
    Y = np.asarray(Y)

    # We do not consider temporal data
    idx = list(range(X.shape[0]))
    np.random.shuffle(idx)
    np.random.shuffle(idx)
    X = X[idx]
    Y = Y[idx]

    if method_name == 'FgSegNet_M':
        # Image Pyramid
        scale2 = []
        scale3 = []
        for i in range(0, X.shape[0]):
            pyramid = tuple(
                pyramid_gaussian(X[i] / 255., max_layer=2, downscale=2))
            scale2.append(pyramid[1] * 255.)  # 2nd scale
            scale3.append(pyramid[2] * 255.)  # 3rd scale
            del pyramid

        scale2 = np.asarray(scale2)
        scale3 = np.asarray(scale3)

    # compute class weights
    cls_weight_list = []
    for i in range(Y.shape[0]):
        y = Y[i].reshape(-1)
        idx = np.where(y != void_label)[0]
        if (len(idx) > 0):
            y = y[idx]
        lb = np.unique(y)  #  0., 1
        cls_weight = compute_class_weight('balanced', lb, y)
        class_0 = cls_weight[0]
        class_1 = cls_weight[1] if len(lb) > 1 else 1.0

        cls_weight_dict = {0: class_0, 1: class_1}
        cls_weight_list.append(cls_weight_dict)

    cls_weight_list = np.asarray(cls_weight_list)

    if method_name == 'FgSegNet_M':
        return [X, scale2, scale3, Y, cls_weight_list]
    else:
        return [X, Y, cls_weight_list]
def kMeans(img):
    t0 = time.time()
    # apply kMeans, fit data, get histogram, get color bar
    org_img = img
    print(img.shape[0], img.shape[1])
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    z = img.reshape((-1, 3))
    print(z.shape)

    # image resize just for silhouetteCoeff
    # Crops images to 300x300, but loses accuracy
    # Try pyrDown (downsampling the images)
    """ysize, xsize, chan = img.shape
	if ysize and xsize > 300:
		xoff = (xsize - 300) // 2
		yoff = (ysize - 300) // 2
		y = img[yoff:-yoff, xoff:-xoff]
	else:
		y = img
	y = y.reshape((-1, 3))
	print(y.shape)"""

    # downnsample images with gaussian smoothing
    if (img.shape[0] > 250 or img.shape[1] > 250):
        for (i, resized) in enumerate(pyramid_gaussian(org_img, downscale=2)):
            if resized.shape[0] < 100 or resized.shape[1] < 100:
                print(resized.shape)
                break
            org_img = resized
            #cv2.imshow("Layer {}".format(i + 1), resized)

    org_img = org_img.reshape((-1, 3))
    org_img = scale(org_img)

    #print(org_img)

    # calculate sse score for each k value
    """Ks = range(1, 10)
	km = [KMeans(n_clusters=i) for i in Ks]
	score = [km[i].fit(org_img).score(org_img) for i in range(len(km))]
	plt.plot(Ks, score)
	plt.show()"""

    # manual version of calculating bss to measure best value of k
    """kMeansVar = [KMeans(n_clusters = k).fit(org_img) for k in range(1, 10)]
	centroids = [X.cluster_centers_ for X in kMeansVar]
	k_euclid = [cdist(org_img, cent) for cent in centroids]
	dist = [np.min(ke, axis=1) for ke in k_euclid]
	wcss = [sum(d**2) for d in dist]
	tss = sum(pdist(org_img)**2/org_img.shape[0])
	bss = tss - wcss
	plt.plot(bss)
	plt.show()"""
    """t0 = time.time()
	ks = range(1, 10)
	km = [KMeans(n_clusters = i, init="k-means++").fit(org_img) for i in ks]
	BIC = [compute_bic(kmeansi, org_img) for kmeansi in km]
	#print(BIC)
	print("Time for BIC: ", time.time() - t0)"""

    # kmeans
    clt = MiniBatchKMeans(n_clusters=16, random_state=42)
    clt.fit(z)

    lab = clt.labels_
    centers = clt.cluster_centers_
    inertia = clt.inertia_

    lab = pd.DataFrame(lab, columns=['pix'])
    centers = pd.DataFrame(centers)
    print(pd.value_counts(lab['pix'].values, sort=True))
    print(centers)
    print(dist(centers.loc[9, :], centers.loc[10, :]))
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.scatter(centers.loc[:, 0], centers.loc[:, 1], centers.loc[:, 2])
    plt.show()
    exit(0)
    hist = centroidHistogram(clt)
    bar = plotColors(hist, clt.cluster_centers_)
    print("Time including KMeans: ", time.time() - t0)
    #print("unique labels: ", np.unique(np.array(clt.labels_), axis=0))

    plt.figure(1)
    plt.axis("off")
    plt.subplot(211)
    plt.imshow(img)
    plt.subplot(212)
    plt.imshow(bar)
    plt.show()
Exemple #34
0
def generateData(train_dir, dataset_dir, scene):

    void_label = -1.
    X_list = []
    Y_list = []

    # Given ground-truths, load training frames
    # ground-truths end with '*.png'
    # training frames end with '*.jpg'

    # scan over FgSegNet_dataset for groundtruths
    for root, _, _ in os.walk(train_dir):
        gtlist = glob.glob(os.path.join(root, '*.png'))
        if gtlist:
            Y_list = gtlist

    # scan over CDnet2014_dataset for .jpg files
    for root, _, _ in os.walk(dataset_dir):
        inlist = glob.glob(os.path.join(root, '*.jpg'))
        if inlist:
            X_list = inlist

    # filter matched files
    X_list_temp = []
    for i in range(len(Y_list)):
        Y_name = os.path.basename(Y_list[i])
        Y_name = Y_name.split('.')[0]
        Y_name = Y_name.split('gt')[1]
        for j in range(len(X_list)):
            X_name = os.path.basename(X_list[j])
            X_name = X_name.split('.')[0]
            X_name = X_name.split('in')[1]
            if (Y_name == X_name):
                X_list_temp.append(X_list[j])
                break
    X_list = X_list_temp
    del X_list_temp, inlist, gtlist

    # process training images
    X = []
    Y = []
    for i in range(0, len(X_list)):
        x = kImage.load_img(X_list[i])
        x = kImage.img_to_array(x)
        X.append(x)

        x = kImage.load_img(Y_list[i], grayscale=True)
        x = kImage.img_to_array(x)
        shape = x.shape
        x /= 255.0
        x = x.reshape(-1)
        idx = np.where(np.logical_and(x > 0.25, x < 0.8))[0]  # find non-ROI
        if (len(idx) > 0):
            x[idx] = void_label
        x = x.reshape(shape)
        x = np.floor(x)
        Y.append(x)
    del Y_list, X_list, x, idx
    X = np.asarray(X)
    Y = np.asarray(Y)

    # Shuffle the training data
    idx = list(range(X.shape[0]))
    np.random.shuffle(idx)
    np.random.shuffle(idx)
    X = X[idx]
    Y = Y[idx]
    del idx

    # Image Pyramid
    scale1 = X
    del X
    scale2 = []
    scale3 = []
    for i in range(0, scale1.shape[0]):
        pyramid = tuple(
            pyramid_gaussian(scale1[i] / 255., max_layer=2, downscale=2))
        scale2.append(pyramid[1])  # 2nd scale
        scale3.append(pyramid[2])  # 3rd scale
        del pyramid
    scale2 = np.asarray(scale2)
    scale3 = np.asarray(scale3)
    print(scale1.shape, scale2.shape, scale3.shape)

    # compute class weights
    cls_weight_list = []
    for i in range(Y.shape[0]):
        y = Y[i].reshape(-1)
        idx = np.where(y != void_label)[0]
        if (len(idx) > 0):
            y = y[idx]
        lb = np.unique(y)  #  0., 1
        cls_weight = compute_class_weight('balanced', lb, y)
        class_0 = cls_weight[0]
        class_1 = cls_weight[1] if len(lb) > 1 else 1.0

        cls_weight_dict = {0: class_0, 1: class_1}
        cls_weight_list.append(cls_weight_dict)
    del y, idx
    cls_weight_list = np.asarray(cls_weight_list)
    return [scale1, scale2, scale3, Y, cls_weight_list]
Exemple #35
0
def slidingW_Test(img, thr_12, x_12, h_fc2_12):

    pyramid = tuple(pyramid_gaussian(img, downscale=downscale))
    detected_list = [0 for _ in xrange(len(pyramid))]
    for scale in xrange(pyramid_num):
        X = pyramid[scale]

        resized = Image.fromarray(np.uint8(X * 255)).resize(
            (int(np.shape(X)[1] * float(img_size_12) / float(face_minimum)),
             int(np.shape(X)[0] * float(img_size_12) / float(face_minimum))))
        X = np.asarray(resized).astype(np.float32) / 255

        img_row = np.shape(X)[0]
        img_col = np.shape(X)[1]

        if img_row < img_size_12 or img_col < img_size_12:
            break

        if img_row % 2 == 1:
            img_row -= 1
            X = X[:img_row, :]
        if img_col % 2 == 1:
            img_col -= 1
            X = X[:, :img_col]

        windows = view_as_windows(X, (img_size_12, img_size_12, input_channel),
                                  4)
        feature_col = np.shape(windows)[1]
        result = h_fc2_12.eval(
            feed_dict={
                x_12:
                np.reshape(windows, (-1, img_size_12 * img_size_12 *
                                     input_channel))
            })

        result_id = np.where(result > thr_12)[0]

        detected_list_scale = np.zeros((len(result_id), 5), np.float32)

        detected_list_scale[:, 0] = (result_id % feature_col) * 4
        detected_list_scale[:, 1] = (result_id / feature_col) * 4
        detected_list_scale[:, 2] = detected_list_scale[:, 0] + img_size_12 - 1
        detected_list_scale[:, 3] = detected_list_scale[:, 1] + img_size_12 - 1

        detected_list_scale[:,
                            0] = detected_list_scale[:,
                                                     0] / img_col * img.size[0]
        detected_list_scale[:,
                            1] = detected_list_scale[:,
                                                     1] / img_row * img.size[1]
        detected_list_scale[:,
                            2] = detected_list_scale[:,
                                                     2] / img_col * img.size[0]
        detected_list_scale[:,
                            3] = detected_list_scale[:,
                                                     3] / img_row * img.size[1]
        detected_list_scale[:, 4] = np.reshape(result[result_id], (-1))

        detected_list_scale = detected_list_scale.tolist()

        detected_list_scale = [
            elem + [
                img.crop((int(elem[0]), int(elem[1]), int(elem[2]), int(
                    elem[3]))), scale, False
            ] for id_, elem in enumerate(detected_list_scale)
        ]

        if len(detected_list_scale) > 0:
            detected_list[scale] = detected_list_scale

    detected_list = [elem for elem in detected_list if type(elem) != int]
    result_box = [
        detected_list[i][j] for i in xrange(len(detected_list))
        for j in xrange(len(detected_list[i]))
    ]

    return result_box
Exemple #36
0
from hammiu import pyramid
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the image")
ap.add_argument("-s",
                "--scale",
                type=float,
                default=2,
                help="scale factor size")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])

# Cách 1: Tạo image pyramid sử dụng OpenCV
for (i, resized) in enumerate(pyramid(image, scale=args["scale"])):
    cv2.imshow("Layer {}".format(i + 1), resized)
    cv2.waitKey(0)

# Cách 2: Tạo image pyramid sử dụng scikit image
# Chú ý phần này ngoài resize còn áp dụng thêm Gaussian smoothiing
for (i, resized) in enumerate(
        pyramid_gaussian(image, downscale=2, multichannel=True)):
    # nếu chiều nào của ảnh nhỏ hơn min thì thoát khỏi vòng lặp
    if resized.shape[0] < 30 or resized.shape[1] < 30:
        break

    # hiển thị các ảnh đã resize
    cv2.imshow("Layer {}".format(i + 1), resized)
    cv2.waitKey(0)
Exemple #37
0
"""
=====================
CollectionViewer demo
=====================

Demo of CollectionViewer for viewing collections of images. This demo uses
the different layers of the gaussian pyramid as image collection.

You can scroll through images with the slider, or you can interact with the
viewer using your keyboard:

left/right arrows
    Previous/next image in collection.
number keys, 0--9
    0% to 90% of collection. For example, "5" goes to the image in the
    middle (i.e. 50%) of the collection.
home/end keys
    First/last image in collection.

"""
import numpy as np
from skimage import data
from skimage.viewer import CollectionViewer
from skimage.transform import pyramid_gaussian

img = data.lena()
img_collection = tuple(pyramid_gaussian(img))

view = CollectionViewer(img_collection)
view.show()
Exemple #38
0
def testImage(imagePath,
              decisionThreshold=cfg.decision_threshold,
              applyNMS=True):

    file = open(cfg.modelPath)
    svc = pickle.load(file)

    image = io.imread(imagePath, as_grey=True)
    image = util.img_as_ubyte(
        image)  #Read the image as bytes (pixels with values 0-255)

    rows, cols = image.shape
    pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor))

    scale = 0
    boxes = None
    scores = None

    for p in pyramid[0:]:
        #We now have the subsampled image in p

        #Add padding to the image, using reflection to avoid border effects
        if cfg.padding > 0:
            p = pad(p, cfg.padding, 'reflect')

        try:
            views = view_as_windows(p, cfg.window_shape, step=cfg.window_step)
        except ValueError:
            #block shape is bigger than image
            break

        num_rows, num_cols, width, height = views.shape
        for row in range(0, num_rows):
            for col in range(0, num_cols):

                #Get current window
                subImage = views[row, col]
                #Extract features
                feats = feature_extractor.extractFeatures(subImage)
                #Obtain prediction score
                decision_func = svc.decision_function(
                    np.array(feats).reshape(1, -1))

                if decision_func > decisionThreshold:
                    # Pedestrian found!
                    h, w = cfg.window_shape
                    scaleMult = math.pow(cfg.downScaleFactor, scale)

                    x1 = int(scaleMult * (col * cfg.window_step - cfg.padding +
                                          cfg.window_margin))
                    y1 = int(scaleMult * (row * cfg.window_step - cfg.padding +
                                          cfg.window_margin))
                    x2 = int(x1 + scaleMult * (w - 2 * cfg.window_margin))
                    y2 = int(y1 + scaleMult * (h - 2 * cfg.window_margin))

                    bbox = (x1, y1, x2, y2)
                    score = decision_func[0]

                    if boxes is not None:
                        boxes = np.vstack((bbox, boxes))
                        scores = np.hstack((score, scores))
                    else:
                        boxes = np.array([bbox])
                        scores = np.array([score])
        scale += 1

    if applyNMS:
        #From all the bounding boxes that are overlapping, take those with maximum score.
        boxes, scores = nms.non_max_suppression_fast(boxes, scores,
                                                     cfg.nmsOverlapThresh)

    return boxes, scores
Exemple #39
0
            output_list = []
            if file.endswith(".ppm") or file.endswith(".pgm") or file.endswith(
                    ".png") or file.endswith(".jpg"):
                image_name = load_dir + '/' + subset + '/test/image_color/' + file
                print(image_name)
                save_file = file[0:-4] + '.mat'
                save_name = save_dir + '/' + subset + '/' + save_file

                #read image
                img, ratio = read_image_from_name(image_name)
                if img.shape[2] == 1:
                    img = np.repeat(img, 3, axis=2)

                #build image pyramid
                pyramid = pyramid_gaussian(img,
                                           max_layer=4,
                                           downscale=np.sqrt(2))

                #predict transformation
                for (j, resized) in enumerate(pyramid):
                    fetch = {"o1": cnn_model.o1}

                    resized = np.asarray(resized)
                    resized = (resized - mean) / std
                    resized = resized.reshape(
                        (1, resized.shape[0], resized.shape[1],
                         resized.shape[2]))

                    result = sess.run(fetch,
                                      feed_dict={cnn_model.patch: resized})
                    result_mat = result["o1"].reshape(
Exemple #40
0
def main():
    createFolder()
    trainData = get_all_labels(DATA_DIR + "/rgb_train/train.yaml")
    print("trainData length: " + str(len(trainData)))
    # don't shuffle for this (so we can easily run multiple programs at the same time)
    indices = [i for i in range(len(trainData))]
    #indices = indices[1500:2000] # did this for each process in batches of 500
    random.shuffle(indices)
    print("LOOKING FOR FALSE POSITIVES")
    #indices = indices[:1000] # TODO: number of images to sample
    fileCount = 0
    imageCount = 0

    #####################################
    # Read the image
    min_wdw_sz = (24, 48)
    step_size = (5, 5)
    downscale = 1.25
    # Load the classifier
    # TODO: update to desired model
    # model4 is from run2 (all images, and 15ish "other" per image, but train was limited to 20,000 from "other")
    clf = joblib.load("../myData/traffic.model5")
    #####################################

    #fds = []  # all features (need to include ones that are not traffic lights as well)
    # extract patches from images and save them to labeled folders
    for i in indices:
        i = 137
        curCount = 0 # number of patches from this image so far
        loc = i
        imageCount += 1
        info = trainData[i]
        im = cv2.imread(DATA_DIR + "/rgb_train/train/" + info['path'], cv2.IMREAD_GRAYSCALE)
        if fileCount % 100 == 0:
            print("patches saved so far: " + str(fileCount))
            print("images so far: " + str(imageCount))

        imageCopy = cv2.cvtColor(im.copy(),cv2.COLOR_GRAY2RGB) # copy to draw rectangles on
        #####################################
        for im_scaled in pyramid_gaussian(im, downscale=downscale):
            # This list contains detections at the current scale
            cd = []
            # If the width or height of the scaled image is less than
            # the width or height of the window, then end the iterations.
            if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[1] < min_wdw_sz[0]:
                break
            for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz, step_size):
                if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[1] != min_wdw_sz[0]:
                    continue
                # Calculate the HOG features
                # TODO!!!: is it a problem that im_window is grayscale floats?  will that mess with my SVM which was trained with grayscale images from 0-255?
                fd = hog(im_window, orientations, pixels_per_cell, cells_per_block, block_norm="L1", visualise=False, transform_sqrt=True, feature_vector=True)
                fd = [fd] # clf wants a list of samples
                # TODO: it's faster to pass all the samples to clf at once to predict on
                #pred = clf.predict(fd)
                probs = list(clf.predict_proba(fd)[0])
                #print(pred)
                pred = probs.index(max(probs)) # index of max
                if pred != 0 and probs[pred] > 0.95:
                    # check if this is a false positive (need to find its rect)
                    # calculate (approximate) rect for this region (24x48)
                    scaleF = im.shape[0] / im_scaled.shape[0]
                    scaleF2 = im.shape[1] / im_scaled.shape[1]
                    if scaleF2 > scaleF: # take the bigger one
                        scaleF = scaleF2
                    # TODO: fix problem where some correct traffic lights are ending up in the falsePositives (I'm pretty sure of this but it's a little hard to tell with some of them)
                    rect = Rectangle(y, y+int(48/scaleF), x, x+int(25/scaleF))
                    print("\n" + str(loc) + ": " + str(rect))
                    #test = getRegion(im, rect)
                    # TODO: does this function not work??? (see above problem TODO)
                    if not isALight(rect, info['boxes']):
                        isLight = "NOT LIGHT"
                        cv2.rectangle(imageCopy, (int(rect.xmin),int(rect.ymin)+1), (int(rect.xmax),int(rect.ymax)+1), [255,0,0], 2)
                        saveImage(im_window, None, "fp-" + str(loc) + "-" + str(curCount), "falsePositives", convert=True)
                        # im_window is float (values from 0 to 1) so convert:
                    else:
                        isLight = "LIGHT"
                        cv2.rectangle(imageCopy, (int(rect.xmin),int(rect.ymin)+1), (int(rect.xmax),int(rect.ymax)+1), [0,255,0], 2)
                        # store positive matches as well for reference:
                        saveImage(im_window, None, "p-" + str(loc) + "-" + str(curCount), "positives", convert=True)
                    fileCount += 1
                    curCount += 1
                    # TODO: there is a definite bug here where some patches are incorrectly believed to
                    # be a light or not a light (might be a problem with how rect is created)
                    # once instance of the problem is apprent in image 137, patch 11
                    saveImage(imageCopy, None, "typ-summary-" + str(loc) + "summaries", "summaries")
                    if curCount == 11:
                        print("" + str(curCount) + ": " + str(rect))
                        plt.imshow(im_window)
                        plt.title(isLight)
                        plt.show()
                        copy2 = cv2.cvtColor(im.copy(),cv2.COLOR_GRAY2RGB) # copy to draw rectangles on
                        cv2.rectangle(copy2, (int(rect.xmin),int(rect.ymin)+1), (int(rect.xmax),int(rect.ymax)+1), [255,0,0], 2)
                        plt.imshow(copy2)
                        plt.title(isLight)
                        plt.show()
            #####################################
        saveImage(imageCopy, None, "summary-" + str(loc) + "summaries", "summaries")
        return

    print("*** Completed looking for false positiives ***")
    print("TOTAL patches saved: " + str(fileCount))
Exemple #41
0
def detector(filename, i, model):
    im = cv2.imread(filename)
    im = imutils.resize(im, width=min(400, im.shape[1]))
    min_wdw_sz = (64, 128)
    step_size = (10, 10)
    downscale = 1.25

    clf = model

    #List to store the detections
    detections = []
    #The current scale of the image
    scale = 0

    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        #The list contains detections at the current scale
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[
                1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz,
                                                step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[
                    1] != min_wdw_sz[0]:
                continue
            im_window = color.rgb2gray(im_window)
            fd = hog(im_window, orientations, pixels_per_cell, cells_per_block)

            fd = fd.reshape(1, -1)
            pred = clf.predict(fd)
            #print pred
            if pred == 1:

                #if clf.decision_function(fd) > 0.5:
                detections.append(
                    (int(x * (downscale**scale)), int(y * (downscale**scale)),
                     pred, int(min_wdw_sz[0] * (downscale**scale)),
                     int(min_wdw_sz[1] * (downscale**scale))))

        scale += 1

    clone = im.copy()

    for (x_tl, y_tl, _, w, h) in detections:
        cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 255, 0),
                      thickness=2)

    rects = np.array([[x, y, x + w, y + h] for (x, y, _, w, h) in detections])
    sc = [score[0] for (x, y, score, w, h) in detections]
    #print "sc: ", sc
    sc = np.array(sc)
    pick = non_max_suppression(rects, probs=sc, overlapThresh=0.25)
    #print "shape, ", pick.shape

    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(clone, (xA, yA), (xB, yB), (0, 255, 0), 2)
    '''
    plt.axis("off")
    plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    plt.title("Raw Detection")
    plt.show()

    plt.axis("off")
    plt.imshow(cv2.cvtColor(clone, cv2.COLOR_BGR2RGB))
    plt.title("Final Detections")
    plt.show()
    '''

    image = cv2.cvtColor(clone, cv2.COLOR_BGR2RGB)
    cv2.imwrite(("../output/" + "%d" + sys.argv[1] + ".jpg") % i, image)
Exemple #42
0
def testImage(imagePath, decisionThreshold, applyNMS):

    fileList = os.listdir(cfg.modelRootPath)

    # Filter all model files
    modelsList = filter(lambda element: '.model' in element, fileList)

    # Filter our specific feature method
    currentModel = cfg.model + '_' + cfg.modelFeatures
    currentModelsList = filter(lambda element: currentModel in element,
                               modelsList)

    models = []
    subImages = []  #To save backgorund crops

    for modelname in currentModelsList:

        file = open(cfg.modelRootPath + modelname, 'r')
        svc = pickle.load(file)
        models.append(svc)

        file.close()

    image = io.imread(imagePath, as_grey=True)
    image = util.img_as_ubyte(
        image)  #Read the image as bytes (pixels with values 0-255)

    rows, cols = image.shape
    pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor))

    scale = 0
    boxes = None
    scores = None
    indices = None

    for p in pyramid[0:]:
        #We now have the subsampled image in p
        window_shape = (32, 32)

        #Add padding to the image, using reflection to avoid border effects
        if cfg.padding > 0:
            p = pad(p, cfg.padding, 'reflect')

        try:
            views = view_as_windows(p, window_shape, step=cfg.window_step)
        except ValueError:
            #block shape is bigger than image
            break

        num_rows, num_cols, width, height = views.shape

        for row in range(0, num_rows):
            for col in range(0, num_cols):
                #Get current window
                subImage = views[row, col]
                # subImages.append(subImage)   #To save backgorund crops: Accumulate them in an array
                #Extract features
                feats = feature_extractor.extractFeatures(subImage)

                #Obtain prediction score for each model
                for model in models:

                    decision_func = model.decision_function(feats)
                    idx = models.index(model)
                    decision_func += cfg.compensate[idx]
                    if decision_func > decisionThreshold:
                        # if decision_func > -0.2:  #For bootstrapping
                        # Signal found!
                        h, w = window_shape
                        scaleMult = math.pow(cfg.downScaleFactor, scale)

                        x1 = int(scaleMult * (col * cfg.window_step -
                                              cfg.padding + cfg.window_margin))
                        y1 = int(scaleMult * (row * cfg.window_step -
                                              cfg.padding + cfg.window_margin))
                        x2 = int(x1 + scaleMult * (w - 2 * cfg.window_margin))
                        y2 = int(y1 + scaleMult * (h - 2 * cfg.window_margin))

                        if (y1 > 0) and (y2 > 0):
                            if y2 - y1 > 330:
                                continue

                        #bootstrapping: Save image (if positive)
                        # print(decision_func)
                        # subImages.append(subImage)

                        bbox = (x1, y1, x2, y2)
                        score = decision_func[0]

                        if boxes is not None:
                            boxes = np.vstack((bbox, boxes))
                            scores = np.hstack((score, scores))
                            indices = np.hstack((idx, indices))
                        else:
                            boxes = np.array([bbox])
                            scores = np.array([score])
                            indices = np.array([idx])
                        break

        scale += 1

    # To save backgorund crops
    # numSubImages = len(subImages)
    # for x in range(0,10): #Save 10 crops for each background image
    #     randomIndex = random.randint(1,numSubImages-1) #Get a random window index
    #     imageName = imagePath.split('/')  #Working on the crop name...
    #     imageName = imageName[len(imageName)-1]
    #     filename = (imageName[:-4]+'-'+str(x)+'.jpg')
    #     io.imsave('Results/'+filename, subImages[randomIndex])  #Save the crop
    #end To save backgorund crops

    # To save bootstrapping windows
    # numSubImages = len(subImages)
    # length = min(10, len(subImages))
    # if length > 0:
    #     for x in range(0,length) : #Save windows with detections (max 10)
    #         if numSubImages == 1:
    #             randomIndex = 0
    #         else:
    #             randomIndex = random.randint(1, numSubImages-1) #Get a random window index
    #         imageName = imagePath.split('/')  #Working on the crop name...
    #         imageName = imageName[len(imageName)-1]
    #         filename = (imageName[:-4]+'-'+str(x)+'.jpg')
    #         io.imsave('Bootstrapping/'+filename, subImages[randomIndex])  #Save the crop
    #end To save bootstrapping windows

    if applyNMS:
        #From all the bounding boxes that are overlapping, take those with maximum score.
        boxes, scores = nms.non_max_suppression_fast(boxes, scores,
                                                     cfg.nmsOverlapThresh)

    #Color boostraping
    # save_windows(boxes, imagePath)

    return boxes, scores, indices
		cond_newimg = (temp_line[0:3] == '200') & (temp_line[3]!=' ') & (temp_line[3]!='.')
	else :
		cond_newimg = 0

	if cond_eof :	   # end of file
		print "1 set done!!"
		break
	
	elif cond_numface : # the number of face in the image
		pass
	
	elif cond_newimg :  # new image name
		org_img_file = Image.open("%s/../fddb/%s.jpg" %(base_path, temp_line) )
		
		start_time = time.time()
		pyramids = list( pyramid_gaussian(org_img_file, downscale=math.sqrt(2) ) )
		
		for i in range(len(pyramids) ):
			if min( pyramids[i].shape[0], pyramids[i].shape[1] ) < MinFace :
			# if min( pyramids[i].shape[0], pyramids[i].shape[1] ) < BigKernelSize :
				del pyramids[i:]
				break
			
		cnt_face = 0
		
		contents = []
		# contents.append( test_file_name )
		contents.append( temp_line )
		
		batch = np.zeros([BigKernelSize,BigKernelSize,3])
from skimage.transform import pyramid_gaussian

img_name = str(sys.argv[1])
if os.path.exists('./result/'):
    shutil.rmtree('./result/')
    os.makedirs('./result/')
else:
    os.makedirs('./result/')
## Process the origin image to be an tensorflow tensor

#image  = skimage.io.imread("./test_data/"+img_name)
image = utils.load_image("./test_data/" + img_name)  # [0,1)

#image = data.astronaut()
rows, cols, dim = image.shape
pyramid = tuple(pyramid_gaussian(image, max_layer=4, downscale=2))

composite_image = np.zeros((rows, cols + cols // 2, 3), dtype=np.double)

composite_image[:rows, :cols, :] = pyramid[0]

i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p
    i_row += n_rows

fig, ax = plt.subplots()
ax.imshow(composite_image)
plt.show()
    path = os.path.join('crater_data', 'tiles')
    img = cv.imread(os.path.join(path, tile_img + '.pgm'), 0)
    img = cv.normalize(img, img, 0, 255, cv.NORM_MINMAX) / 255.0

    # Task. Creat new script and Apply results of segmentation phase (FCN) to remove the non-crater areas of crater image.

    # task: get the threshold of the image

    crater_list_cnn = []
    #crater_list_nn = []

    winS = param.dmin
    # loop over the image pyramid

    for (i, resized) in enumerate(pyramid_gaussian(img, downscale=1.5)):
        if resized.shape[0] < 31:
            break

        print("Resized shape: %d, Window size: %d, i: %d" %
              (resized.shape[0], winS, i))

        # loop over the sliding window for each layer of the pyramid
        # this process takes about 7 hours. To do quick test, we may try stepSize
        # to be large (60) and see if code runs OK
        for (x, y, window) in sliding_window(resized,
                                             stepSize=8,
                                             windowSize=(winS, winS)):

            # apply a circular mask ot the window here. Think about where you should apply this mask. Before, resizing or after it.
            crop_img = cv.resize(window, (50, 50))
        period: The period of the event (in days).
        num_bins: The number of intervals to divide the time axis into.
        bin_width_factor: Width of the bins, as a fraction of period.

      Returns:
        1D NumPy array containing the Gaussian representation of the 
        phase-folded lightcurve.
      """
  view = generate_view(time,
                       flux,
                       num_bins=num_bins,
                       bin_width=period * bin_width_factor,
                       t_min=-period / 2,
                       t_max=period / 2)

  pyramid = tuple(pyramid_gaussian(view, downscale=2, multichannel=False))
  position = np.int((len(pyramid)/2)-1)
  global_view = pyramid[position]

  return global_view


def local_view(time,
               flux,
               period,
               duration,
               num_bins=201,
               bin_width_factor=0.16,
               num_durations=4):
  """Generates a 'local view' of a phase folded light curve.
Exemple #47
0
def kMeans(img):
    t0 = time.time()
    # apply kMeans, fit data, get histogram, get color bar
    org_img = img
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    z = img.reshape((-1, 3))
    #print(z.shape)

    # image resize just for silhouetteCoeff
    # Crops images to 300x300, but loses accuracy
    # Try pyrDown (downsampling the images)
    """ysize, xsize, chan = img.shape
	if ysize and xsize > 300:
		xoff = (xsize - 300) // 2
		yoff = (ysize - 300) // 2
		y = img[yoff:-yoff, xoff:-xoff]
	else:
		y = img
	y = y.reshape((-1, 3))
	print(y.shape)"""

    # downnsample images with gaussian smoothing
    if (img.shape[0] > 250 or img.shape[1] > 250):
        for (i, resized) in enumerate(pyramid_gaussian(org_img, downscale=2)):
            if resized.shape[0] < 100 or resized.shape[1] < 100:
                break
            org_img = resized
        #cv2.imshow("Layer {}".format(i + 1), resized)
        #print(org_img.shape)

    org_img = org_img.reshape((-1, 3))
    org_img = scale(org_img)
    #print(org_img.shape)
    # kmeans
    clt = MiniBatchKMeans(n_clusters=16, random_state=42)
    clt.fit(z)
    c_centers = clt.cluster_centers_

    klt = MiniBatchKMeans(n_clusters=silhouetteCoeff(org_img), random_state=42)
    klt.fit(z)
    k_centers = klt.cluster_centers_

    print("k_centers(b): ", k_centers)
    print("c_centers(b): ", c_centers)
    print("c_centers shape: ", c_centers.shape)
    print("k_centers shape: ", k_centers.shape)

    for (idx, i) in enumerate(k_centers):
        d = 999999
        max_jdx = 999
        for (jdx, j) in enumerate(c_centers):
            if dist(i, j) <= d:
                d = dist(i, j)
                #k_centers[idx, :] = c_centers[jdx, :]
                max_jdx = jdx
        k_centers[idx, :] = c_centers[max_jdx, :]
        print(max_jdx)
        c_centers = np.delete(c_centers, max_jdx, axis=0)

    print("c_centers(a): ", c_centers)
    print("k_centers(a): ", k_centers)
    hist = centroidHistogram(clt)
    bar = plotColors(hist, clt.cluster_centers_, False)
    print("Time including KMeans: ", time.time() - t0)
    #print("unique labels: ", np.unique(np.array(clt.labels_), axis=0))

    hist2 = centroidHistogram(klt)
    bar2 = plotColors(hist2, klt.cluster_centers_, True)

    plt.figure(1)
    plt.axis("off")
    plt.subplot(1, 2, 1)
    plt.imshow(img)
    plt.subplot(2, 2, 2)
    plt.imshow(bar)
    plt.subplot(2, 2, 4)
    plt.imshow(bar2)
    plt.show()
Exemple #48
0
    saver_cal_48 = tf.train.Saver(cal_48_vars)

    sess = tf.Session()
    sess.run(tf.initialize_all_variables())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    saver_net_12.restore(sess, 'model/model_net_12-400000')
    saver_cal_12.restore(sess, 'model/model_cal_12-20000')
    saver_net_24.restore(sess, 'model/model_net_24-210000')
    saver_cal_24.restore(sess, 'model/model_cal_24-80000')
    saver_net_48.restore(sess, 'model/model_net_48-40000')
    saver_cal_48.restore(sess, 'model/model_cal_48-10000')

    # net-12
    pyramid = tuple(pyramid_gaussian(np.array(image), downscale=1.2))
    image_array = pyramid[0]
    window_after_24 = []
    for i, img in enumerate(pyramid):
        slide_return = slide_window(img, 40, 12, 4)
        if slide_return is None:
            break
        img_12 = slide_return[0]
        window_net_12 = slide_return[1]
        w_12 = img_12.shape[1]
        h_12 = img_12.shape[0]

        patch_net_12 = []
        for box in window_net_12:
            patch = img_12[box[0]:box[2], box[1]:box[3], :]
            patch_net_12.append(patch)
Exemple #49
0
 def _build_pyramid(self, image):
     image = _prepare_grayscale_input_2D(image)
     return list(pyramid_gaussian(image, self.n_scales - 1, self.downscale))
Exemple #50
0
    # Read the image
    im = imread(args["image"], as_grey=False)
    min_wdw_sz = (100, 40)
    step_size = (10, 10)
    downscale = args['downscale']
    visualize_det = args['visualize']

    # Load the classifier
    clf = joblib.load(config.model_path)

    # List to store the detections
    detections = []
    # The current scale of the image
    scale = 0
    # Downscale the image and iterate
    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        # This list contains detections at the current scale
        cd = []
        # If the width or height of the scaled image is less than
        # the width or height of the window, then end the iterations.
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[
                1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz,
                                                step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[
                    1] != min_wdw_sz[0]:
                continue
            # Calculate the HOG features
            fd = hog(im_window, config.orientations, config.pixels_per_cell,
                     config.cells_per_block)  # , visualize, normalize
Exemple #51
0
def testBodyDetector():
    im = cv2.imread('1.jpg')
    im = imutils.resize(im, width=min(400, im.shape[1]))
    min_wdw_sz = (64, 128)
    step_size = (10, 10)
    downscale = 1.25

    clf = joblib.load('svm.model')

    # List to store the detections
    detections = []
    # The current scale of the image
    scale = 0

    def sliding_window(image, window_size, step_size):
        '''
        This function returns a patch of the input 'image' of size
        equal to 'window_size'. The first image returned top-left
        co-ordinate (0, 0) and are increment in both x and y directions
        by the 'step_size' supplied.

        So, the input parameters are-
        image - Input image
        window_size - Size of Sliding Window
        step_size - incremented Size of Window

        The function returns a tuple -
        (x, y, im_window)
        '''
        for y in xrange(0, image.shape[0], step_size[1]):
            for x in xrange(0, image.shape[1], step_size[0]):
                yield (x, y, image[y:y + window_size[1], x:x + window_size[0]])

    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        # The list contains detections at the current scale
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[
                1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz,
                                                step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[
                    1] != min_wdw_sz[0]:
                continue
            im_window = color.rgb2gray(im_window)
            fd = hog(im_window, 9, [6, 6], [2, 2])

            fd = fd.reshape(1, -1)
            pred = clf.predict(fd)

            if pred == 1:

                if clf.decision_function(fd) > 0.5:
                    detections.append(
                        (int(x * (downscale**scale)),
                         int(y * (downscale**scale)),
                         clf.decision_function(fd),
                         int(min_wdw_sz[0] * (downscale**scale)),
                         int(min_wdw_sz[1] * (downscale**scale))))

        scale += 1

    clone = im.copy()

    for (x_tl, y_tl, _, w, h) in detections:
        cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 255, 0),
                      thickness=2)

    rects = np.array([[x, y, x + w, y + h] for (x, y, _, w, h) in detections])
    sc = [score[0] for (x, y, score, w, h) in detections]
    print "sc: ", sc
    sc = np.array(sc)
    pick = non_max_suppression(rects, probs=sc, overlapThresh=0.3)
    print "shape, ", pick.shape

    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(clone, (xA, yA), (xB, yB), (0, 255, 0), 2)

    plt.axis("off")
    plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    plt.title("Raw Detection before NMS")
    plt.show()

    plt.axis("off")
    plt.imshow(cv2.cvtColor(clone, cv2.COLOR_BGR2RGB))
    plt.title("Final Detections after applying NMS")
    plt.show()
Exemple #52
0
def detector(filename, esitmator):
    im = cv2.imread(filename)
    im = imutils.resize(im, width=min(400, im.shape[1]))
    #min_wdw_sz = (64, 128)
    min_wdw_sz = (50, 50)
    step_size = (10, 10)
    downscale = 1.25

    clf = joblib.load(model_path)

    #List to store the detections
    detections = []
    #The current scale of the image
    scale = 0

    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        #The list contains detections at the current scale
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[
                1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz,
                                                step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[
                    1] != min_wdw_sz[0]:
                continue
            im_window = color.rgb2gray(im_window)
            if des_type == 'HOG':
                fd = hog(im_window,
                         orientations,
                         pixels_per_cell,
                         cells_per_block,
                         visualise=visualize,
                         transform_sqrt=normalize)
            if des_type == 'HOG+PCA':
                fd = hog(im_window,
                         orientations,
                         pixels_per_cell,
                         cells_per_block,
                         visualise=visualize,
                         transform_sqrt=normalize)
                fd = fd.reshape(1, -1)
                fd = esitmator.transform(fd)

            if des_type == 'LBP':
                fd = local_binary_pattern(im_window, n_point, radius,
                                          'default')
                fd = np.reshape(fd, (2500, ))
            if des_type == "BOTH":
                fd = hog(im_window,
                         orientations,
                         pixels_per_cell,
                         cells_per_block,
                         visualise=visualize,
                         transform_sqrt=normalize)
                lbp = local_binary_pattern(im_window, n_point, radius,
                                           'default')
                lbp = np.reshape(lbp, (2500, ))
                fd = np.concatenate((fd, lbp), axis=0)
            if des_type == "BOTH+PCA":
                fd = hog(im_window,
                         orientations,
                         pixels_per_cell,
                         cells_per_block,
                         visualise=visualize,
                         transform_sqrt=normalize)
                lbp = local_binary_pattern(im_window, n_point, radius,
                                           'default')
                lbp = np.reshape(lbp, (2500, ))
                fd = np.concatenate((fd, lbp), axis=0)
                fd = fd.reshape(1, -1)
                fd = esitmator.transform(fd)

            fd = fd.reshape(1, -1)
            pred = clf.predict(fd)

            if pred == 1:

                if clf.decision_function(fd) > 0.5:
                    detections.append(
                        (int(x * (downscale**scale)),
                         int(y * (downscale**scale)),
                         clf.decision_function(fd),
                         int(min_wdw_sz[0] * (downscale**scale)),
                         int(min_wdw_sz[1] * (downscale**scale))))

        scale += 1

    clone = im.copy()

    for (x_tl, y_tl, score, w, h) in detections:
        a = float(score)

        print(type(a))
        cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 255, 0),
                      thickness=2)

    rects = np.array([[x, y, x + w, y + h]
                      for (x, y, score, w, h) in detections])
    sc = [score[0] for (x, y, score, w, h) in detections]
    print "sc: ", sc
    sc = np.array(sc)
    pick = non_max_suppression(rects, probs=sc, overlapThresh=0.3)
    #print "shape, ", pick.shape

    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(clone, (xA, yA), (xB, yB), (0, 255, 0), 2)

    plt.axis("off")
    plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    plt.title("Raw Detection before NMS")
    plt.show()

    plt.axis("off")
    plt.imshow(cv2.cvtColor(clone, cv2.COLOR_BGR2RGB))
    plt.title("Final Detections after applying NMS")
    plt.show()
def create_ns (tmp_imgpath, cnt_ns, network_list, threshold1) :
	
	tmp_img = Image.open("%s/%s" %(aflw_path, tmp_imgpath), 'r' )
	org_img = Image.open("%s/%s" %(aflw_path, tmp_imgpath), 'r' )

	size = tmp_img.size
	if max(size[0], size[1]) > 1000 :
		return 0

	resize_ratio = float(MinFace)/ 12.
	try :
		tmp_img = tmp_img.resize( (int(size[0]/resize_ratio), int(size[1]/resize_ratio)), Image.BILINEAR )
	except IOError :
		return 0

	scale_factor = 1.414
	pyra = list( pyramid_gaussian(tmp_img, downscale=scale_factor ) )
	max_pyra = image_search.remove_small_pyra( pyra )

	pos_list = []
	# Detection 12 net
	start_scale = 0
	for i in xrange( start_scale, max_pyra + 1 ) :
		try :
			pos_list.append( image_search.scan_12net(network_list[0], pyra[i], threshold1, i, scale_factor, 1) )
		except ValueError :
			pos_list.append( [ [-1,-1,-1,-1,-1] ])

	# Calibration 12 net
	for i in xrange(start_scale, max_pyra + 1 ) :
		scale_up = start_scale
		pos_list[i-start_scale] = image_search.apply_calib( network_list[1], pos_list[i-start_scale], pyra[i-scale_up], '12net', scale_factor, scale_up )

	# In-Scale NMS
	thre_cal1 = 0.5
	for i in xrange( start_scale, max_pyra + 1 ) :
		if type(pos_list[i-start_scale]) == list :
			pos_list[i-start_scale] = image_search.apply_nms( pos_list[i-start_scale], thre_cal1, 'inscale' )
		else :
			pos_list[i-start_scale] = image_search.apply_nms( pos_list[i-start_scale].tolist(), thre_cal1, 'inscale' )

	if net_name == '48net' :
		# Detection 24 net
		if len(network_list) >= 3 :
			for i in xrange(start_scale, max_pyra + 1 ) :
				scale_up = i
				pos_list[i] = image_search.apply_det( network_list, pos_list[i-start_scale], pyra[i-scale_up], threshold2, '24net', scale_factor, scale_up)

	# ADJUST SCALE and MERGE
	for i in xrange( start_scale, max_pyra + 1 ) :
		pos_list[i-start_scale] = np.array(pos_list[i-start_scale])
		pos_list[i-start_scale][:,0:4] = np.multiply( pos_list[i-start_scale][:,0:4], resize_ratio * scale_factor**i )

	final_list = []
	for i in xrange( start_scale, max_pyra + 1 ) :
		if pos_list[i-start_scale][0][2] > 0 :		# means there's face in this pyramid
			if len(final_list) == 0 :
				final_list = pos_list[i-start_scale]
			else :
				final_list = np.concatenate( [ final_list, pos_list[i-start_scale] ], axis = 0 )

	cnt_save = 0
	for i in xrange(len(final_list) ) :
		iou_thre = 0.1
		if image_search.check_iou(final_list[i], tmp_annot_list, iou_thre, 1) == 0 :
			if net_name == '24net' :
				path = "%s/NS_aflw24/ns-%d.jpg" %(base_path, cnt_ns+cnt_save)
			elif net_name == '48net' :
				path = "%s/NS_aflw48_1/ns-%d.jpg" %(base_path, cnt_ns+cnt_save)
			org_img = org_img.convert('RGB')
			image_search.save_patch(org_img, final_list[i], path, net_name)
			cnt_save = cnt_save + 1

	return cnt_save
def detector(filename):
    im = cv2.imread(filename)
    im = imutils.resize(im, width=min(400, im.shape[1]))
    min_wdw_sz = (64, 128)
    step_size = (10, 10)
    downscale = 1.6
    #with open('svm.model', 'rb') as f:
    #    loaded = pickle.load(f)

    clf = joblib.load(os.path.join(model_path, 'svm.model'))

    #List to store the detections
    detections = []
    #The current scale of the image
    scale = 0

    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        #The list contains detections at the current scale
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[
                1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz,
                                                step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[
                    1] != min_wdw_sz[0]:
                continue
            im_window = color.rgb2gray(im_window)
            fd = hog(im_window,
                     orientations=9,
                     pixels_per_cell=(6, 6),
                     cells_per_block=(2, 2),
                     block_norm='L1',
                     visualise=False,
                     transform_sqrt=False,
                     feature_vector=True,
                     normalise=None)
            fd = fd.reshape(1, -1)
            pred = clf.predict(fd)

            if pred == 1:

                if clf.decision_function(fd) > 0.5:
                    detections.append(
                        (int(x * (downscale**scale)),
                         int(y * (downscale**scale)),
                         clf.decision_function(fd),
                         int(min_wdw_sz[0] * (downscale**scale)),
                         int(min_wdw_sz[1] * (downscale**scale))))

        scale += 1

    clone = im.copy()

    rects = np.array([[x, y, x + w, y + h] for (x, y, _, w, h) in detections])
    sc = [score[0] for (x, y, score, w, h) in detections]
    print("sc: ", sc)
    sc = np.array(sc)
    pick = non_max_suppression(rects, probs=sc, overlapThresh=0.3)
    #print ("shape, ", pick.shape)
    if sc.any() < 0.5:
        clone = cv2.putText(clone,
                            "Human not Detected", (100, 180),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 0, 255),
                            lineType=cv2.LINE_AA)
        im = cv2.putText(im,
                         "Human not Detected", (100, 180),
                         cv2.FONT_HERSHEY_SIMPLEX,
                         0.5, (0, 0, 255),
                         lineType=cv2.LINE_AA)
    else:
        clone = cv2.putText(clone,
                            "Human Detected", (100, 180),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 0, 255),
                            lineType=cv2.LINE_AA)
        im = cv2.putText(im,
                         "Human Detected", (100, 180),
                         cv2.FONT_HERSHEY_SIMPLEX,
                         0.5, (0, 0, 255),
                         lineType=cv2.LINE_AA)

        for (x_tl, y_tl, _, w, h) in detections:
            cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 255, 0),
                          thickness=2)
        for (xA, yA, xB, yB) in pick:
            cv2.rectangle(clone, (xA, yA), (xB, yB), (0, 255, 0), 2)

    plt.axis("off")
    plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    plt.title("before NMS")
    plt.show()

    plt.axis("off")
    plt.imshow(cv2.cvtColor(clone, cv2.COLOR_BGR2RGB))
    plt.title("after NMS")
    plt.show()
The `pyramid_gaussian` function takes an image and yields successive images
shrunk by a constant scale factor. Image pyramids are often used, e.g., to
implement algorithms for denoising, texture discrimination, and scale-
invariant detection.

"""
import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.transform import pyramid_gaussian


image = data.astronaut()
rows, cols, dim = image.shape
pyramid = tuple(pyramid_gaussian(image, downscale=2))

composite_image = np.zeros((rows, cols + cols / 2, 3), dtype=np.double)

composite_image[:rows, :cols, :] = pyramid[0]

i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p
    i_row += n_rows

fig, ax = plt.subplots()
ax.imshow(composite_image)
plt.show()
Exemple #56
0
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)


I = io.imread('../fixed.bmp').astype(np.float32)
J = io.imread('../moving.bmp').astype(np.float32)

fig1 = plt.figure()
plt.subplot(1,2,1)
plt.imshow(I)
plt.subplot(1,2,2)
plt.imshow(J)


pyramid_I = tuple(pyramid_gaussian(I, downscale=2, multichannel=False))
pyramid_J = tuple(pyramid_gaussian(J, downscale=2, multichannel=False))

fig2 = plt.figure()
fig2.add_subplot(1,2,1)
plt.imshow(pyramid_I[7])
plt.title("Fixed Image")
fig2.add_subplot(1,2,2)
plt.imshow(pyramid_J[7])
plt.title("Moving Image")

# plt.show()


class Mine(nn.Module):
    def __init__(self, input_size=2, hidden_size=100):
"""
=====================
CollectionViewer demo
=====================

Demo of CollectionViewer for viewing collections of images. This demo uses
the different layers of the gaussian pyramid as image collection.

You can scroll through images with the slider, or you can interact with the
viewer using your keyboard:

left/right arrows
    Previous/next image in collection.
number keys, 0--9
    0% to 90% of collection. For example, "5" goes to the image in the
    middle (i.e. 50%) of the collection.
home/end keys
    First/last image in collection.

"""
from skimage import data
from skimage.viewer import CollectionViewer
from skimage.transform import pyramid_gaussian


img = data.astronaut()
img_collection = tuple(pyramid_gaussian(img))

view = CollectionViewer(img_collection)
view.show()
Exemple #58
0
def testImage(imagePath, decisionThreshold = cfg.decision_threshold, applyNMS=True):

    fileList = os.listdir(cfg.modelRootPath)

    # Filter all model files
    modelsList = filter(lambda element: '.model' in element, fileList)

    # Filter our specific feature method
    currentModel = cfg.model+'_'+cfg.modelFeatures
    currentModelsList = filter(lambda element: currentModel in element, modelsList)

    models = []
    subImages = [] #To save backgorund crops

    for modelname in currentModelsList:

        file = open(cfg.modelRootPath + modelname, 'r')
        svc = pickle.load(file)
        models.append(svc)

        file.close()

    image = io.imread(imagePath, as_grey=True)
    image = util.img_as_ubyte(image) #Read the image as bytes (pixels with values 0-255)

    rows, cols = image.shape
    pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor))

    scale = 0
    boxes = None
    scores = None


    for p in pyramid[0:]:
        #We now have the subsampled image in p
        window_shape = (32,32)

        #Add padding to the image, using reflection to avoid border effects
        if cfg.padding > 0:
            p = pad(p,cfg.padding,'reflect')

        try:
            views = view_as_windows(p, window_shape, step=cfg.window_step)
        except ValueError:
            #block shape is bigger than image
            break

        num_rows, num_cols, width, height = views.shape

        for row in range(0, num_rows):
            for col in range(0, num_cols):
                #Get current window
                subImage = views[row, col]
                # subImages.append(subImage)   #To save backgorund crops: Accumulate them in an array
                #Extract features
                feats = feature_extractor.extractFeatures(subImage)

                #Obtain prediction score for each model
                for model in models:

                    decision_func = model.decision_function(feats)

                    # if decision_func > decisionThreshold:
                    if decision_func > 0.2:  #For bootstrapping
                        # Signal found!
                        h, w = window_shape
                        scaleMult = math.pow(cfg.downScaleFactor, scale)

                        x1 = int(scaleMult * (col*cfg.window_step - cfg.padding + cfg.window_margin))
                        y1 = int(scaleMult * (row*cfg.window_step - cfg.padding + cfg.window_margin))
                        x2 = int(x1 + scaleMult*(w - 2*cfg.window_margin))
                        y2 = int(y1 + scaleMult*(h - 2*cfg.window_margin))

                        #bootstrapping: Save image (if positive)
                        subImages.append(subImage)

                        bbox = (x1, y1, x2, y2)
                        score = decision_func[0]

                        if boxes is not None:
                            boxes = np.vstack((bbox, boxes))
                            scores = np.hstack((score, scores))
                        else:
                            boxes = np.array([bbox])
                            scores = np.array([score])
                        break

        scale += 1


    # To save backgorund crops
    # numSubImages = len(subImages)
    # for x in range(0,10): #Save 10 crops for each background image
    #     randomIndex = random.randint(1,numSubImages-1) #Get a random window index
    #     imageName = imagePath.split('/')  #Working on the crop name...
    #     imageName = imageName[len(imageName)-1]
    #     filename = (imageName[:-4]+'-'+str(x)+'.jpg')
    #     io.imsave('Results/'+filename, subImages[randomIndex])  #Save the crop
    #end To save backgorund crops

    # To save bootstrapping windows
    numSubImages = len(subImages)
    length = min(10, len(subImages))
    for x in range(0,length) : #Save windows with detections (max 10)
        if numSubImages == 1:
            randomIndex = 0
        else:
            randomIndex = random.randint(1, numSubImages-1) #Get a random window index
        imageName = imagePath.split('/')  #Working on the crop name...
        imageName = imageName[len(imageName)-1]
        filename = (imageName[:-4]+'-'+str(x)+'.jpg')
        io.imsave('Bootstrapping/'+filename, subImages[randomIndex])  #Save the crop
    #end To save bootstrapping windows


    if applyNMS:
        #From all the bounding boxes that are overlapping, take those with maximum score.
        boxes, scores = nms.non_max_suppression_fast(boxes, scores, cfg.nmsOverlapThresh)

    return boxes, scores
Exemple #59
0
def detector(filename):
    im = cv2.imread(filename)
    if filename.split('.')[-1] == 'png':
        os.rename(filename, 'test_image/area_' + filename.split('_')[-1])
        im = cv2.imread('test_image/area_' + filename.split('_')[-1])
    im = imutils.resize(im, width=min(400, im.shape[1]))
    min_wdw_sz = (64, 128)
    step_size = (10, 10)
    downscale = 1.25

    clf = joblib.load(os.path.join(model_path, 'svm.model'))

    #List to store the detections
    detections = []
    #The current scale of the image
    scale = 0

    for im_scaled in pyramid_gaussian(im, downscale=downscale):
        #The list contains detections at the current scale
        if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[
                1] < min_wdw_sz[0]:
            break
        for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz,
                                                step_size):
            if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[
                    1] != min_wdw_sz[0]:
                continue
            im_window = color.rgb2gray(im_window)
            fd = hog(im_window, orientations, pixels_per_cell, cells_per_block,
                     'L1', visualize)

            fd = fd.reshape(1, -1)
            pred = clf.predict(fd)

            if pred == 1:

                if clf.decision_function(fd) > 0.5:
                    detections.append(
                        (int(x * (downscale**scale)),
                         int(y * (downscale**scale)),
                         clf.decision_function(fd),
                         int(min_wdw_sz[0] * (downscale**scale)),
                         int(min_wdw_sz[1] * (downscale**scale))))

        scale += 1

    clone = im.copy()

    for (x_tl, y_tl, _, w, h) in detections:
        cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 255, 0),
                      thickness=2)

    rects = np.array([[x, y, x + w, y + h] for (x, y, _, w, h) in detections])
    sc = [score[0] for (x, y, score, w, h) in detections]
    print("sc: ", sc)
    sc = np.array(sc)
    pick = non_max_suppression(rects, probs=sc, overlapThresh=0.3)
    print("shape, ", pick.shape)

    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(clone, (xA, yA), (xB, yB), (0, 255, 0), 2)

    plt.axis("off")
    plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    plt.title("Raw Detection before NMS")
    plt.show()

    plt.axis("off")
    plt.imshow(cv2.cvtColor(clone, cv2.COLOR_BGR2RGB))
    plt.title("Final Detections after applying NMS")
    plt.show()