Python cv2.resize() Examples

The following are code examples for showing how to use cv2.resize(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: AVSR-Deep-Speech   Author: pandeydivesh15   File: data_preprocessing_autoencoder.py    (GNU General Public License v2.0) View Source Project 13 votes vote down vote up
def crop_and_store(frame, mouth_coordinates, name):
	"""
	Args:
		1. frame:				The frame which has to be cropped.
		2. mouth_coordinates:	The coordinates which help in deciding which region is to be cropped.
		3. name:				The path name to be used for storing the cropped image.
	"""

	# Find bounding rectangle for mouth coordinates
	x, y, w, h = cv2.boundingRect(mouth_coordinates)

	mouth_roi = frame[y:y + h, x:x + w]

	h, w, channels = mouth_roi.shape
	# If the cropped region is very small, ignore this case.
	if h < 10 or w < 10:
		return
	
	resized = resize(mouth_roi, 32, 32)
	cv2.imwrite(name, resized) 
Example 2
Project: facial_emotion_recognition   Author: adamaulia   File: image_test.py    (license) View Source Project 11 votes vote down vote up
def test_image(addr):
    target = ['angry','disgust','fear','happy','sad','surprise','neutral']
    font = cv2.FONT_HERSHEY_SIMPLEX
    
    im = cv2.imread(addr)
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1)
    
    for (x, y, w, h) in faces:
            cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 2,5)
            face_crop = im[y:y+h,x:x+w]
            face_crop = cv2.resize(face_crop,(48,48))
            face_crop = cv2.cvtColor(face_crop, cv2.COLOR_BGR2GRAY)
            face_crop = face_crop.astype('float32')/255
            face_crop = np.asarray(face_crop)
            face_crop = face_crop.reshape(1, 1,face_crop.shape[0],face_crop.shape[1])
            result = target[np.argmax(model.predict(face_crop))]
            cv2.putText(im,result,(x,y), font, 1, (200,0,0), 3, cv2.LINE_AA)
            
    cv2.imshow('result', im)
    cv2.imwrite('result.jpg',im)
    cv2.waitKey(0) 
Example 3
Project: vehicle_brand_classification_CNN   Author: nanoc812   File: logoPredictor.py    (MIT License) View Source Project 8 votes vote down vote up
def loadImgs(imgsfolder, rows, cols):
    myfiles = glob.glob(imgsfolder+'*.jpg', 0)
    nPics = len(myfiles)
    X = np.zeros((nPics, rows, cols), dtype = 'uint8')
    i = 0; imgNames = []
    for filepath in myfiles:
        sd = filepath.rfind('/'); ed = filepath.find('.'); filename = filepath[int(sd+1):int(ed)]
        imgNames.append(filename)  
        
        temp = cv2.imread(filepath, 0)
        if temp == None:
            continue
        elif temp.size < 1000:
            continue
        elif temp.shape == [rows, cols, 1]:
            X[i,:,:] = temp
        else:
            X[i,:,:] = cv2.resize(temp,(cols, rows), interpolation = cv2.INTER_CUBIC)
        i += 1
    return X, imgNames 
Example 4
Project: detection-2016-nipsws   Author: imatge-upc   File: features.py    (MIT License) View Source Project 8 votes vote down vote up
def get_image_descriptor_for_image(image, model):
    im = cv2.resize(image, (224, 224)).astype(np.float32)
    dim_ordering = K.image_dim_ordering()
    if dim_ordering == 'th':
        # 'RGB'->'BGR'
        im = im[::-1, :, :]
        # Zero-center by mean pixel
        im[0, :, :] -= 103.939
        im[1, :, :] -= 116.779
        im[2, :, :] -= 123.68
    else:
        # 'RGB'->'BGR'
        im = im[:, :, ::-1]
        # Zero-center by mean pixel
        im[:, :, 0] -= 103.939
        im[:, :, 1] -= 116.779
        im[:, :, 2] -= 123.68
    im = im.transpose((2, 0, 1))
    im = np.expand_dims(im, axis=0)
    inputs = [K.learning_phase()] + model.inputs
    _convout1_f = K.function(inputs, [model.layers[33].output])
    return _convout1_f([0] + [im]) 
Example 5
Project: lsun_2017   Author: ternaus   File: downscale_images.py    (MIT License) View Source Project 8 votes vote down vote up
def downscale(old_file_name):
    img = cv2.imread(os.path.join(old_file_name))

    new_file_name = (old_file_name
                     .replace('training', 'training_' + str(min_size))
                     .replace('validation', 'validation_' + str(min_size))
                     .replace('testing', 'testing_' + str(min_size))
                     )

    height, width, _ = img.shape

    if width > height:
        new_width = int(1.0 * width / height * min_size)
        new_height = min_size

    else:
        new_height = int(1.0 * height / width * min_size)
        new_width = min_size

    img_new = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
    cv2.imwrite(new_file_name, img_new) 
Example 6
Project: FaceSwap   Author: Aravind-Suresh   File: main.py    (MIT License) View Source Project 7 votes vote down vote up
def videoize(func, args, src = 0, win_name = "Cam", delim_wait = 1, delim_key = 27):
    cap = cv2.VideoCapture(src)
    while(1):
        ret, frame = cap.read()
        # To speed up processing; Almost real-time on my PC
        frame = cv2.resize(frame, dsize=None, fx=0.5, fy=0.5)
        frame = cv2.flip(frame, 1)
        out = func(frame, args)
        if out is None:
            continue
        out = cv2.resize(out, dsize=None, fx=1.4, fy=1.4)
        cv2.imshow(win_name, out)
        cv2.moveWindow(win_name, (s_w - out.shape[1])/2, (s_h - out.shape[0])/2)
        k = cv2.waitKey(delim_wait)

        if k == delim_key:
            cv2.destroyAllWindows()
            cap.release()
            return 
Example 7
Project: RunescapeBots   Author: lukegarbutt   File: hsv-tuner.py    (license) View Source Project 7 votes vote down vote up
def resize_image(self,img,*args):
        # unpacks width, height
        height, width,_ = img.shape
        print("Original size: {} {}".format(width, height))
        count_times_resized = 0
        while width > 500 or height > 500:
        #if width > 300 or height > 300:
            # divides images WxH by half
            width = width / 2
            height = height /2
            count_times_resized += 1
        # prints x times resized to console
        if count_times_resized != 0:
            print("Resized {}x smaller, to: {} {}".format(count_times_resized*2,width, height))
        # makes sures image is not TOO small
        if width < 300 and height < 300:
            width = width * 2
            height = height * 2

        img = cv2.resize(img,(int(width),int(height)))

        return img 
Example 8
Project: human-rl   Author: gsastry   File: human_feedback.py    (MIT License) View Source Project 7 votes vote down vote up
def add_text(img, text, text_top, image_scale):
    """
    Args:
        img (numpy array of shape (width, height, 3): input image
        text (str): text to add to image
        text_top (int): location of top text to add
        image_scale (float): image resize scale

    Summary:
        Add display text to a frame.

    Returns:
        Next available location of top text (allows for chaining this function)
    """
    cv2.putText(
        img=img,
        text=text,
        org=(0, text_top),
        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
        fontScale=0.15 * image_scale,
        color=(255, 255, 255))
    return text_top + int(5 * image_scale) 
Example 9
Project: FCN_train   Author: 315386775   File: image_channel.py    (license) View Source Project 7 votes vote down vote up
def preprocess(image):
    """Takes an image and apply preprocess"""
    # ????????????
    image = cv2.resize(image, (data_shape, data_shape))
    # ?? BGR ? RGB
    image = image[:, :, (2, 1, 0)]
    # ?mean?????float
    image = image.astype(np.float32)
    # ? mean
    image -= np.array([123, 117, 104])
    # ??? [batch-channel-height-width]
    image = np.transpose(image, (2, 0, 1))
    image = image[np.newaxis, :]
    # ?? ndarray
    image = nd.array(image)
    return image 
Example 10
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: predict_video_res10.py    (license) View Source Project 7 votes vote down vote up
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[11:11+128,11:11+128];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores) 
Example 11
Project: AerialCrackDetection_Keras   Author: TTMRonald   File: measure_map.py    (license) View Source Project 7 votes vote down vote up
def format_img(img, C):
	img_min_side = float(C.im_size)
	(height,width,_) = img.shape
	
	if width <= height:
		f = img_min_side/width
		new_height = int(f * height)
		new_width = int(img_min_side)
	else:
		f = img_min_side/height
		new_width = int(f * width)
		new_height = int(img_min_side)
	fx = width/float(new_width)
	fy = height/float(new_height)
	img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
	img = img[:, :, (2, 1, 0)]
	img = img.astype(np.float32)
	img[:, :, 0] -= C.img_channel_mean[0]
	img[:, :, 1] -= C.img_channel_mean[1]
	img[:, :, 2] -= C.img_channel_mean[2]
	img /= C.img_scaling_factor
	img = np.transpose(img, (2, 0, 1))
	img = np.expand_dims(img, axis=0)
	return img, fx, fy 
Example 12
Project: robik   Author: RecunchoMaker   File: scanner.py    (GNU General Public License v2.0) View Source Project 6 votes vote down vote up
def switch_camara(self):
        self.activo = not self.activo
        if self.activo:

            # Capturo el primer frame para quedarme con el tamano y el factor de resize

            ret,frame = self.cap.read(self.camera_id)

            self.activo = ret
            if ret:
                self.img_height, self.img_width, self.img_channels = frame.shape
                self.img_zoomx = 320.0/self.img_width
                self.img_zoomy = 200.0/self.img_height
                # Ya tengo los datos. Capturo la imagen final y me quedo con el frame
                self.captura_frame()
            else:
                self.status = "No puedo encontrar la camara"
                print "No encuentro la camara!!!!" 
Example 13
Project: robik   Author: RecunchoMaker   File: scanner.py    (GNU General Public License v2.0) View Source Project 6 votes vote down vote up
def get_frame(self):

        ret,frame = self.cap.read(self.camera_id)
        self.frame = cv2.resize(frame,None,fx=self.img_zoomx, fy=self.img_zoomy, \
                interpolation = cv2.INTER_AREA)

        self.frame = cv2.blur(self.frame, (3,3))
        self.hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
        self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)

        self.colors = []
        if self.escaneando:
            self.draw_osd(self.frame)

        return self.frame 
Example 14
Project: human-rl   Author: gsastry   File: human_feedback.py    (MIT License) View Source Project 6 votes vote down vote up
def decorate_img_for_env(img, env_id, image_scale):
    """
    Args:
        img (numpy array of (width, height, 3)): input image
        env_id (str): the gym env id
        image_scale (float): a scale to resize the image

    Returns:
        an image

    Summary:
        Adds environment specific image decorations. Currently used to make it easier to
        block/label in Pong.

    """
    if env_id is not None and 'Pong' in env_id:
        h, w, _ = img.shape
        est_catastrophe_y = h - 142
        est_block_clearance_y = est_catastrophe_y - int(20 * image_scale)
        # cv2.line(img, (0, est_catastrophe_y), (int(500 * image_scale), est_catastrophe_y), (0, 0, 255))
        cv2.line(img, (250, est_catastrophe_y), (int(500 * image_scale), est_catastrophe_y), (0, 255, 255))
        # cv2.line(img, (0, est_block_clearance_y), (int(500 * image_scale), est_block_clearance_y),
        #          (255, 0, 0))
    return img 
Example 15
Project: yolo_tensorflow   Author: hizhangp   File: test.py    (MIT License) View Source Project 6 votes vote down vote up
def detect(self, img):
        img_h, img_w, _ = img.shape
        inputs = cv2.resize(img, (self.image_size, self.image_size))
        inputs = cv2.cvtColor(inputs, cv2.COLOR_BGR2RGB).astype(np.float32)
        inputs = (inputs / 255.0) * 2.0 - 1.0
        inputs = np.reshape(inputs, (1, self.image_size, self.image_size, 3))

        result = self.detect_from_cvmat(inputs)[0]

        for i in range(len(result)):
            result[i][1] *= (1.0 * img_w / self.image_size)
            result[i][2] *= (1.0 * img_h / self.image_size)
            result[i][3] *= (1.0 * img_w / self.image_size)
            result[i][4] *= (1.0 * img_h / self.image_size)

        return result 
Example 16
Project: vehicle_brand_classification_CNN   Author: nanoc812   File: logoSet.py    (MIT License) View Source Project 6 votes vote down vote up
def loadLogoSet(path, rows,cols,test_data_rate=0.15):
    random.seed(612)
    _, imgID = readItems('data.txt')
    y, _ = modelDict(path)
    nPics =  len(y)
    faceassset = np.zeros((nPics,rows,cols), dtype = np.uint8) ### gray images
    noImg = []
    for i in range(nPics):
        temp = cv2.imread(path +'logo/'+imgID[i]+'.jpg', 0)
        if temp == None:
            noImg.append(i)
        elif temp.size < 1000:
            noImg.append(i)
        else:
            temp = cv2.resize(temp,(cols, rows), interpolation = cv2.INTER_CUBIC)
            faceassset[i,:,:] = temp
    y = np.delete(y, noImg,0); faceassset = np.delete(faceassset, noImg, 0)
    nPics = len(y)
    index = random.sample(np.arange(nPics), int(nPics*test_data_rate))
    x_test = faceassset[index,:,:]; x_train = np.delete(faceassset, index, 0)
    y_test = y[index]; y_train = np.delete(y, index, 0)
    return (x_train, y_train), (x_test, y_test) 
Example 17
Project: FCN_train   Author: 315386775   File: dataAugment.py    (license) View Source Project 6 votes vote down vote up
def crop(image, name, crop_size, padding_size):
    (width, height) = image.shape
    cropped_images = []
    for i in xrange(0, width, padding_size):
        for j in xrange(0, height, padding_size):
            box = (i, j, i+crop_size, j+crop_size) #left, upper, right, lower
            cropped_name = name + '_' + str(i) + '_' + str(j) + '.jpg'
            cropped_image = image[i:i+crop_size, j:j+crop_size]
            resized_image = cv2.resize(cropped_image, (IMAGE_SIZE, IMAGE_SIZE))
            cropped_images.append(resized_image)
 
    return cropped_images




# ????
# ???????????????????????????????????data_num? 
Example 18
Project: detection-2016-nipsws   Author: imatge-upc   File: features.py    (MIT License) View Source Project 6 votes vote down vote up
def get_conv_image_descriptor_for_image(image, model):
    im = cv2.resize(image, (224, 224)).astype(np.float32)
    dim_ordering = K.image_dim_ordering()
    if dim_ordering == 'th':
        # 'RGB'->'BGR'
        im = im[::-1, :, :]
        # Zero-center by mean pixel
        im[0, :, :] -= 103.939
        im[1, :, :] -= 116.779
        im[2, :, :] -= 123.68
    else:
        # 'RGB'->'BGR'
        im = im[:, :, ::-1]
        # Zero-center by mean pixel
        im[:, :, 0] -= 103.939
        im[:, :, 1] -= 116.779
        im[:, :, 2] -= 123.68
    im = im.transpose((2, 0, 1))
    im = np.expand_dims(im, axis=0)
    inputs = [K.learning_phase()] + model.inputs
    _convout1_f = K.function(inputs, [model.layers[31].output])
    return _convout1_f([0] + [im]) 
Example 19
Project: US-image-prediction   Author: ChengruiWu008   File: multi_CNN.py    (license) View Source Project 6 votes vote down vote up
def get_batch():
    ran = random.randint(600, data_size)
    #print(ran)
    image = []
    label = []
    label_0 = []
    n_pic = ran
    # print(n_pic)
    for i in range(batch_size * n_steps):
        frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic+i), 0)
        frame_0 = cv2.resize(frame_0, (LONGITUDE, LONGITUDE))
        frame_0 = np.array(frame_0).reshape(-1)
        image.append(frame_0)
        #print(np.shape(image))
    for i in range(batch_size):
        frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
        frame_1 = cv2.resize(frame_1, (LONGITUDE, LONGITUDE))
        frame_1 = np.array(frame_1).reshape(-1)
        label.append(frame_1)
    for i in range(batch_size):
        frame_2 = cv2.imread('./cropedoriginalUS2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
        frame_2 = cv2.resize(frame_2, (LONGITUDE, LONGITUDE))
        frame_2 = np.array(frame_2).reshape(-1)
        label_0.append(frame_2)
    return image , label , label_0 
Example 20
Project: US-image-prediction   Author: ChengruiWu008   File: conv-conv.py    (license) View Source Project 6 votes vote down vote up
def get_batch(batch_size=20,data_size=6498):
    ran = np.random.choice(data_size, batch_size,replace=False)
    image=[]
    outline=[]
    for i in range(batch_size):
        n_pic=ran[i]
        #print(n_pic)
        frame_0 = cv2.imread('./cropPicY/%d.jpg' % n_pic,0)
        frame_0 = cv2.resize(frame_0, (24, 24))
        frame_0 = np.array(frame_0).reshape(-1)
        # print('np',frame_0)
        # frame_0 = gray2binary(frame_0)
        #print (frame_0)
        frame_1 = cv2.imread('./cropPicX/%d.jpg' % n_pic, 0)
        frame_1 = cv2.resize(frame_1, (24, 24))
        frame_1 = np.array(frame_1).reshape(-1)
        frame_1 = gray2binary(frame_1)
        image.append(frame_0)
        outline.append(frame_1)
        #print(image)
    return np.array(image),np.array(outline) 
Example 21
Project: US-image-prediction   Author: ChengruiWu008   File: denoise.py    (license) View Source Project 6 votes vote down vote up
def get_train_batch(noise=0):
    ran = random.randint(600, data_size)
    #print(ran)
    image = []
    label = []
    label_0 = []
    n_pic = ran
    # print(n_pic)
    for i in range(batch_size ):
        frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic+i), 0)
        frame_0 = add_noise(frame_0, n = noise)
        frame_0 = cv2.resize(frame_0, (LONGITUDE, LONGITUDE))
        frame_0 = np.array(frame_0).reshape(-1)
        image.append(frame_0)
        #print(np.shape(image))
    for i in range(batch_size):
        frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic + batch_size * (i+1) ), 0)
        frame_1 = cv2.resize(frame_1, (LONGITUDE, LONGITUDE))
        frame_1 = np.array(frame_1).reshape(-1)
        label.append(frame_1)
    return image , label 
Example 22
Project: US-image-prediction   Author: ChengruiWu008   File: AUTO.py    (license) View Source Project 6 votes vote down vote up
def get_batch(batch_size=20,data_size=6498):
    ran = np.random.choice(data_size, batch_size,replace=False)
    image=[]
    for i in range(batch_size):
        n_pic=ran[i]
        #print(n_pic)
        frame_0 = cv2.imread('./cropPicX/%d.jpg' % n_pic,0)
        frame_0 = cv2.resize(frame_0, (24, 24))
        frame_0 = np.array(frame_0).reshape(-1)
        image.append(frame_0)
        #print(image)
    return np.array(image)


# Visualize decoder setting
# Parameters 
Example 23
Project: US-image-prediction   Author: ChengruiWu008   File: 1.1.1autoencoder_self.py    (license) View Source Project 6 votes vote down vote up
def get_batch(batch_size=20,data_size=6498):
    ran = np.random.choice(data_size, batch_size,replace=False)
    image=[]
    outline=[]
    for i in range(batch_size):
        n_pic=ran[i]
        #print(n_pic)
        frame_0 = cv2.imread('./easyPixelImage2/%d.jpg' % n_pic,0)
        frame_0 = cv2.resize(frame_0, (24, 24))
        frame_0 = np.array(frame_0).reshape(-1)
        # print('np',frame_0)
        # frame_0 = gray2binary(frame_0)
        #print (frame_0)
        frame_1 = cv2.imread('./easyPixelImage2/%d.jpg' % n_pic, 0)
        frame_1 = cv2.resize(frame_1, (24, 24))
        frame_1 = np.array(frame_1).reshape(-1)
        frame_1 = gray2binary(frame_1)
        image.append(frame_0)
        outline.append(frame_1)
        #print(image)
    return np.array(image),np.array(outline) 
Example 24
Project: US-image-prediction   Author: ChengruiWu008   File: de_noise.py    (license) View Source Project 6 votes vote down vote up
def get_train_batch(noise=500):
    ran = np.random.randint(600,5800,size=10,dtype='int')
    #print(ran)
    image = []
    label = []
    label_0 = []
    n_pic = ran
    # print(n_pic)
    for i in range(10):
        frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
        frame_0 = add_noise(frame_0, n = noise)
        frame_0 = cv2.resize(frame_0, (24, 24))
        frame_0 = np.array(frame_0).reshape(-1)
        frame_0 = frame_0 / 255.0
        image.append(frame_0)
        #print(np.shape(image))
    for i in range(10):
        frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
        frame_1 = cv2.resize(frame_1, (24, 24))
        frame_1 = np.array(frame_1).reshape(-1)
        frame_1 = gray2binary(frame_1)
        label.append(frame_1)
    return np.array(image,dtype='float') , np.array(label,dtype='float') 
Example 25
Project: US-image-prediction   Author: ChengruiWu008   File: de_noise.py    (license) View Source Project 6 votes vote down vote up
def get_test_batch(noise=500):
    ran = np.random.randint(5800,6000,size=10,dtype='int')
    #print(ran)
    image = []
    label = []
    label_0 = []
    n_pic = ran
    # print(n_pic)
    for i in range(10):
        frame_0 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
        frame_0 = add_noise(frame_0, n = noise)
        frame_0 = cv2.resize(frame_0, (24, 24))
        frame_0 = np.array(frame_0).reshape(-1)
        frame_0 = frame_0 / 255.0
        image.append(frame_0)
        #print(np.shape(image))
    for i in range(10):
        frame_1 = cv2.imread('./cropedoriginalPixel2/%d.jpg' % (n_pic[i]), 0)
        frame_1 = cv2.resize(frame_1, (24, 24))
        frame_1 = np.array(frame_1).reshape(-1)
        frame_1 = gray2binary(frame_1)
        label.append(frame_1)
    return np.array(image,dtype='float') , np.array(label,dtype='float') 
Example 26
Project: shenlan   Author: vector-1127   File: cgan.py    (license) View Source Project 6 votes vote down vote up
def get_data(datadir):
    #datadir = args.data
    # assume each image is 512x256 split to left and right
    imgs = glob.glob(os.path.join(datadir, '*.jpg'))
    data_X = np.zeros((len(imgs),3,img_cols,img_rows))
    data_Y = np.zeros((len(imgs),3,img_cols,img_rows))  
    i = 0
    for file in imgs:
        img = cv2.imread(file,cv2.IMREAD_COLOR)
        img = cv2.resize(img, (img_cols*2, img_rows)) 
        #print('{} {},{}'.format(i,np.shape(img)[0],np.shape(img)[1]))
        img = np.swapaxes(img,0,2)

        X, Y = split_input(img)

        data_X[i,:,:,:] = X
        data_Y[i,:,:,:] = Y
        i = i+1
    return data_X, data_Y 
Example 27
Project: tfplus   Author: renmengye   File: list_image_data_provider.py    (license) View Source Project 6 votes vote down vote up
def get_batch_idx(self, idx):
        hh = self.inp_height
        ww = self.inp_width
        x = np.zeros([len(idx), hh, ww, 3], dtype='float32')
        orig_height = []
        orig_width = []
        ids = []
        for kk, ii in enumerate(idx):
            fname = self.ids[ii]
            ids.append('{:06}'.format(ii))
            x_ = cv2.imread(fname).astype('float32') / 255
            x[kk] = cv2.resize(
                x_, (self.inp_width, self.inp_height),
                interpolation=cv2.INTER_CUBIC)
            orig_height.append(x_.shape[0])
            orig_width.append(x_.shape[1])
            pass
        return {
            'x': x,
            'orig_height': np.array(orig_height),
            'orig_width': np.array(orig_width),
            'id': ids
        } 
Example 28
Project: C3D-tensorflow   Author: hx173149   File: input_data_v1.py    (MIT License) View Source Project 6 votes vote down vote up
def RandomCrop(rand_seed,img, top,left,height=224, width=224,u=0.5,aug_factor=9/8):
    #first zoom in by a factor of aug_factor of input img,then random crop by(height,width)
    # if rand_seed < u:
    if 1:
        # h,w,c = img.shape
        # img = cv2.resize(img, (round(aug_factor*w), round(aug_factor*h)), interpolation=cv2.INTER_LINEAR)
        # h, w, c = img.shape

        new_h, new_w = height,width

        # top = np.random.randint(0, h - new_h)
        # left = np.random.randint(0, w - new_w)

        img = img[top: top + new_h,
              left: left + new_w]
    return img 
Example 29
Project: deep-prior   Author: moberweger   File: handdetector.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def __init__(self, dpt, fx, fy, importer=None, refineNet=None):
        """
        Constructor
        :param dpt: depth image
        :param fx: camera focal lenght
        :param fy: camera focal lenght
        """
        self.dpt = dpt
        self.maxDepth = min(1500, dpt.max())
        self.minDepth = max(10, dpt.min())
        # set values out of range to 0
        self.dpt[self.dpt > self.maxDepth] = 0.
        self.dpt[self.dpt < self.minDepth] = 0.
        # camera settings
        self.fx = fx
        self.fy = fy
        # Optional refinement of CoM
        self.refineNet = refineNet
        self.importer = importer
        # depth resize method
        self.resizeMethod = self.RESIZE_CV2_NN 
Example 30
Project: deep-prior   Author: moberweger   File: handdetector.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def resizeCrop(self, crop, sz):
        """
        Resize cropped image
        :param crop: crop
        :param sz: size
        :return: resized image
        """
        if self.resizeMethod == self.RESIZE_CV2_NN:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
        elif self.resizeMethod == self.RESIZE_BILINEAR:
            rz = self.bilinearResize(crop, sz, self.getNDValue())
        elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
        else:
            raise NotImplementedError("Unknown resize method!")
        return rz 
Example 31
Project: sail   Author: GemHunt   File: caffe_image.py    (MIT License) View Source Project 6 votes vote down vote up
def get_whole_rotated_image(crop, mask, angle, crop_size, before_rotate_size, scale):
    #Better for larger:
    #pixels_to_jitter = 35 * scale
    #For Dates:
    pixels_to_jitter = 4 #Old Way

    center_x = before_rotate_size / 2 + (random.random() * pixels_to_jitter * 2) - pixels_to_jitter
    center_y = before_rotate_size / 2 + (random.random() * pixels_to_jitter * 2) - pixels_to_jitter

    rot_image = crop.copy()
    rot_image = rotate(rot_image, angle, center_x, center_y, before_rotate_size, before_rotate_size)
    # This is hard coded for 28x28.
    rot_image = cv2.resize(rot_image, (41, 41), interpolation=cv2.INTER_AREA)
    rot_image = rot_image[6:34, 6:34]

    # rot_image = rot_image * mask
    return rot_image 
Example 32
Project: mx-rfcn   Author: giorking   File: image_processing.py    (license) View Source Project 6 votes vote down vote up
def resize(im, target_size, max_size):
    """
    only resize input image to target size and return scale
    :param im: BGR image input by opencv
    :param target_size: one dimensional size (the short side)
    :param max_size: one dimensional max size (the long side)
    :return:
    """
    im_shape = im.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])
    im_scale = float(target_size) / float(im_size_min)
    # prevent bigger axis from being more than max_size:
    if np.round(im_scale * im_size_max) > max_size:
        im_scale = float(max_size) / float(im_size_max)
    im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR)
    return im, im_scale 
Example 33
Project: mx-rfcn   Author: giorking   File: predict.py    (license) View Source Project 6 votes vote down vote up
def resize(im, target_size, max_size):
    """
    only resize input image to target size and return scale
    :param im: BGR image input by opencv
    :param target_size: one dimensional size (the short side)
    :param max_size: one dimensional max size (the long side)
    :return:
    """
    im_shape = im.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])
    im_scale = float(target_size) / float(im_size_min)
    if np.round(im_scale * im_size_max) > max_size:
        im_scale = float(max_size) / float(im_size_max)
    im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR)
    return im, im_scale 
Example 34
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: extract_res10.py    (license) View Source Project 6 votes vote down vote up
def predict(the_net,image):
  inputs = []
  if not os.path.exists(image):
    raise Exception("Image path not exist")
    return
  try:
    tmp_input = cv2.imread(image)
    tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
    tmp_input = tmp_input[11:11+128,11:11+128]
    tmp_input = np.subtract(tmp_input,mean)
    tmp_input = tmp_input.transpose((2, 0, 1))
    tmp_input = np.require(tmp_input, dtype=np.float32)
  except Exception as e:
    #raise Exception("Image damaged or illegal file format")
    return None
  the_net.blobs['data'].reshape(1, *tmp_input.shape)
  the_net.reshape()
  the_net.blobs['data'].data[...] = tmp_input
  the_net.forward()
  scores = copy.deepcopy(the_net.blobs['feature'].data)
  return scores 
Example 35
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: test_vgg.py    (license) View Source Project 6 votes vote down vote up
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[13:13+224,13:13+224];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores) 
Example 36
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: test_res10.py    (license) View Source Project 6 votes vote down vote up
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[11:11+128,11:11+128];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores) 
Example 37
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: extract_emotion.py    (license) View Source Project 6 votes vote down vote up
def predict(the_net,image):
  inputs = []
  if not os.path.exists(image):
    raise Exception("Image path not exist")
    return
  try:
    tmp_input = cv2.imread(image)
    tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
    tmp_input = tmp_input[13:13+224,13:13+224]
    #tmp_input = np.subtract(tmp_input,mean)
    tmp_input = tmp_input.transpose((2, 0, 1))
    tmp_input = np.require(tmp_input, dtype=np.float32)
  except Exception as e:
    #raise Exception("Image damaged or illegal file format")
    return None
  the_net.blobs['data'].reshape(1, *tmp_input.shape)
  the_net.reshape()
  the_net.blobs['data'].data[...] = tmp_input
  the_net.forward()
  scores = copy.deepcopy(the_net.blobs['fc6'].data)
  return scores 
Example 38
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: predict_video.py    (license) View Source Project 6 votes vote down vote up
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[13:13+224,13:13+224];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores) 
Example 39
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: test_afew_face_vgg.py    (license) View Source Project 6 votes vote down vote up
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[13:13+224,13:13+224];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores) 
Example 40
Project: EmotiW-2017-Audio-video-Emotion-Recognition   Author: xujinchang   File: extract_emotion_bak.py    (license) View Source Project 6 votes vote down vote up
def predict(the_net,image):
  inputs = []
  if not os.path.exists(image):
    raise Exception("Image path not exist")
    return
  try:
    tmp_input = cv2.imread(image)
    tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
    tmp_input = tmp_input[13:13+224,13:13+224]
    tmp_input = np.subtract(tmp_input,mean)
    tmp_input = tmp_input.transpose((2, 0, 1))
    tmp_input = np.require(tmp_input, dtype=np.float32)
  except Exception as e:
    #raise Exception("Image damaged or illegal file format")
    return None
  the_net.blobs['data'].reshape(1, *tmp_input.shape)
  the_net.reshape()
  the_net.blobs['data'].data[...] = tmp_input
  the_net.forward()
  scores = copy.deepcopy(the_net.blobs['fc6'].data)
  return scores 
Example 41
Project: speed   Author: keon   File: dataset.py    (license) View Source Project 6 votes vote down vote up
def __init__(self,
                 folder:str,
                 resize:(int, int),
                 batch_size:int,
                 timesteps:int,
                 windowsteps:int,
                 shift:int,
                 train:bool):
        self.folder  = folder
        self.resize = resize
        self.batch_size = batch_size
        self.timesteps  = timesteps
        self.train = train
        self.images = sorted(os.listdir(folder + 'images/'))
        self.labels = open(folder + 'labels.txt').readlines()
        self.data = self._sliding_window(self.images, shift, windowsteps) 
Example 42
Project: speed   Author: keon   File: dataset.py    (license) View Source Project 6 votes vote down vote up
def get_batcher(self, shuffle=True, augment=True):
        """ produces batch generator """
        w, h = self.resize

        if shuffle: np.random.shuffle(self.data)
        data = iter(self.data)
        while True:
            x = np.zeros((self.batch_size, self.timesteps, h, w, 3))
            y = np.zeros((self.batch_size, 1))
            for b in range(self.batch_size):
                images, label = next(data)
                for t, img_name in enumerate(images):
                    image_path = self.folder + 'images/' + img_name
                    img = cv2.imread(image_path)
                    img = img[190:350, 100:520] # crop
                    if augment:
                        img = aug.augment_image(img) # augmentation
                    img = cv2.resize(img.copy(), (w, h))
                    x[b, t] = img
                y[b] = label
            x = np.transpose(x, [0, 4, 1, 2, 3])
            yield x, y 
Example 43
Project: fast-rcnn-distillation   Author: xiaolonw   File: blob.py    (license) View Source Project 6 votes vote down vote up
def prep_im_for_blob(im, pixel_means, target_size, max_size):
    """Mean subtract and scale an image for use in a blob."""
    im = im.astype(np.float32, copy=False)
    im -= pixel_means
    im = im / 127.5
    im_shape = im.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])
    im_scale = float(target_size) / float(im_size_min)
    # Prevent the biggest axis from being more than MAX_SIZE
    if np.round(im_scale * im_size_max) > max_size:
        im_scale = float(max_size) / float(im_size_max)
    im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
                    interpolation=cv2.INTER_LINEAR)

    return im, im_scale 
Example 44
Project: garden.facelock   Author: kivy-garden   File: download_images.py    (license) View Source Project 6 votes vote down vote up
def store_raw_images():
    '''To download images from image-net
        (Change the url for different needs of cascades)
    '''
    neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n07942152'
    neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()

    pic_num = 1

    for i in neg_image_urls.split('\n'):
        try:

            print i
            urllib.urlretrieve(i, "neg/" + str(pic_num) + '.jpg')
            img = cv2.imread("neg/" + str(pic_num) +'.jpg',
                                cv2.IMREAD_GRAYSCALE)
            resized_image = cv2.resize(img, (100, 100))
            cv2.imwrite("neg/" + str(pic_num) + '.jpg', resized_image)
            pic_num = pic_num + 1

        except:
            print "error" 
Example 45
Project: DistanceGAN   Author: sagiebenaim   File: dataset.py    (license) View Source Project 6 votes vote down vote up
def read_images( filenames, domain=None, image_size=64):

    images = []

    for fn in filenames:
        image = cv2.imread(fn)
        if image is None:
            continue

        if domain == 'A':
            kernel = np.ones((3,3), np.uint8)
            image = image[:, :256, :]
            image = 255. - image
            image = cv2.dilate( image, kernel, iterations=1 )
            image = 255. - image
        elif domain == 'B':
            image = image[:, 256:, :]

        image = cv2.resize(image, (image_size,image_size))
        image = image.astype(np.float32) / 255.
        image = image.transpose(2,0,1)
        images.append( image )

    images = np.stack( images )
    return images 
Example 46
Project: watermark   Author: lishuaijuly   File: watermark_invisiable.py    (license) View Source Project 6 votes vote down vote up
def _gene_signature(self,wm,size,key):
        '''????????????????????????'''
        wm = cv2.resize(wm,(size,size))        
        wU,_,wV = np.linalg.svd(np.mat(wm))


        sumU = np.sum(np.array(wU),axis=0)
        sumV = np.sum(np.array(wV),axis=0)

        sumU_mid = np.median(sumU)
        sumV_mid = np.median(sumV)

        sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
        sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])

        uv_xor=np.logical_xor(sumU,sumV)

        np.random.seed(key)
        seq=np.random.randint(2,size=len(uv_xor))

        signature = np.logical_xor(uv_xor, seq)

        sqrts = int(np.sqrt(size))
        return np.array(signature,dtype=np.int8).reshape((sqrts,sqrts)) 
Example 47
Project: watermark   Author: lishuaijuly   File: watermark_invisiable.py    (license) View Source Project 6 votes vote down vote up
def _gene_signature(self,wm,key):
        '''????????????????????????'''
        wm = cv2.resize(wm,(256,256))        
        wU,_,wV = np.linalg.svd(np.mat(wm))


        sumU = np.sum(np.array(wU),axis=0)
        sumV = np.sum(np.array(wV),axis=0)

        sumU_mid = np.median(sumU)
        sumV_mid = np.median(sumV)

        sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
        sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])

        uv_xor=np.logical_xor(sumU,sumV)

        np.random.seed(key)
        seq=np.random.randint(2,size=len(uv_xor))

        signature = np.logical_xor(uv_xor, seq)
        return np.array(signature,dtype=np.int8) 
Example 48
Project: traffic_detection_yolo2   Author: wAuner   File: process_predictions.py    (license) View Source Project 6 votes vote down vote up
def create_heatmaps(img, pred):
    """
    Uses objectness probability to draw a heatmap on the image and returns it
    """
    # find anchors with highest prediction
    best_pred = np.max(pred[..., 0], axis=-1)
    # convert probabilities to colormap scale
    best_pred = np.uint8(best_pred * 255)
    # apply color map
    # cv2 colormaps create BGR, not RGB
    cmap = cv2.cvtColor(cv2.applyColorMap(best_pred, cv2.COLORMAP_JET), cv2.COLOR_BGR2RGB)
    # resize the color map to fit image
    cmap = cv2.resize(cmap, img.shape[1::-1], interpolation=cv2.INTER_NEAREST)

    # overlay cmap with image
    return cv2.addWeighted(cmap, 1, img, 0.5, 0) 
Example 49
Project: FaceSwapper   Author: QuantumLiu   File: faceswapper.py    (license) View Source Project 6 votes vote down vote up
def swap(self,head_name,face_path):
        '''
        ??? ????
        head_name?
            ?????????
        face_path:
            ?????????
        '''
        im_head,landmarks_head,im_face,landmarks_face=self.resize(*self.heads[head_name],*self.read_and_mark(face_path))
        M = self.transformation_from_points(landmarks_head[self.ALIGN_POINTS],
                                       landmarks_face[self.ALIGN_POINTS])
        
        face_mask = self.get_face_mask(im_face, landmarks_face)
        warped_mask = self.warp_im(face_mask, M, im_head.shape)
        combined_mask = np.max([self.get_face_mask(im_head, landmarks_head), warped_mask],
                                  axis=0)
        
        warped_face = self.warp_im(im_face, M, im_head.shape)
        warped_corrected_im2 = self.correct_colours(im_head, warped_face, landmarks_head)
        
        out=im_head * (1.0 - combined_mask) + warped_corrected_im2 * combined_mask
        return out 
Example 50
Project: YOLO-Object-Detection-Tensorflow   Author: huseinzol05   File: main.py    (license) View Source Project 6 votes vote down vote up
def detect(img):
    img_h, img_w, _ = img.shape
    inputs = cv2.resize(img, (settings.image_size, settings.image_size))
    inputs = cv2.cvtColor(inputs, cv2.COLOR_BGR2RGB).astype(np.float32)
    inputs = (inputs / 255.0) * 2.0 - 1.0
    inputs = np.reshape(inputs, (1, settings.image_size, settings.image_size, 3))
    result = detect_from_cvmat(inputs)[0]
    print result

    for i in range(len(result)):
        result[i][1] *= (1.0 * img_w / settings.image_size)
        result[i][2] *= (1.0 * img_h / settings.image_size)
        result[i][3] *= (1.0 * img_w / settings.image_size)
        result[i][4] *= (1.0 * img_h / settings.image_size)

    return result