Exemplo n.º 1
0
def decode_jpeg(bytestring, shape, dtype):
    colorspace = "RGB" if len(shape) > 3 and shape[3] > 1 else "GRAY"
    data = simplejpeg.decode_jpeg(
        bytestring,
        colorspace=colorspace,
    ).ravel()
    return data.reshape(shape, order='F')
 def _run(self):
     receiver = imagezmq.ImageHub(self.connstr, REQ_REP=False)
     while not self._stop:
         data, jpg_buffer = receiver.recv_jpg()
         self._data = (data, simplejpeg.decode_jpeg( jpg_buffer, colorspace='BGR'))
         self._data_ready.set()
     receiver.close()
Exemplo n.º 3
0
	def __getitem__(self, idx):
		#print("IMGLIST SIZE: ", len(self.imglist))
		#print("MASKLIST SIZE: ", len(self.masklist))
		#print("CURRENT INDEX: ", idx)

		with open(os.path.join(self.path_img, self.imglist[idx]), 'rb') as f1:
			image = decode_jpeg(f1.read())[:,:,1]
		with open(os.path.join(self.path_mask, self.masklist[idx]), 'rb') as f2:
			mask = decode_jpeg(f2.read())[:,:,1]

		#if(center==True):
		#	image = image - self.img_mean

		if(config.extension=="model12"):
			augmented = light_aug(image=image, mask=mask)
			image = augmented['image']
			mask = augmented['mask']
		if(config.extension=="model13"):
			augmented = medium_aug(image=image, mask=mask)
			image = augmented['image']
			mask = augmented['mask']
		if(config.extension=="model14"):
			augmented = heavy_aug(image=image, mask=mask)
			image = augmented['image']
			mask = augmented['mask']

		#resized = res(image=image, mask=mask)
		#image = resized['image']
		#mask = resized['mask']
		sample = {'image': image, 'mask': mask}  

		sample['image'] = np.expand_dims(sample['image'], axis=0)
		sample['mask'] = np.expand_dims(sample['mask'], axis=0)
		sample['image'].astype(float)
		sample['mask'].astype(float)

		sample['image'] = sample['image']/255.0 #image being rescaled to contain values between 0 to 1 for BCE Loss
		if sample['mask'].max()==255:
		    sample['mask'] = sample['mask']/255.0

		#sample['image'] = (sample['image']/255)*2 -1 #image being rescaled to contain values between -1 to 1 for BCE Loss
		#sample['mask'] = (sample['mask']/255)*2 -1

		return sample
Exemplo n.º 4
0
def jpeg_bytes_to_img(jpeg_bytes: bytes) -> np.ndarray:
    colorspace = simplejpeg.decode_jpeg_header(jpeg_bytes)[2]
    # 3-channel jpegs are internally encoded as YCbCr and we have to impose our desired
    # colorspace conversion which we assume is RGB.
    if colorspace == "YCbCr":
        colorspace = "RGB"
    img = simplejpeg.decode_jpeg(jpeg_bytes,
                                 fastdct=True,
                                 fastupsample=True,
                                 colorspace=colorspace)
    if img.shape[-1] == 1:
        img = np.squeeze(img, axis=-1)
    return img
Exemplo n.º 5
0
    def angles_to_image(self, tl: int, pl: int, tv: int, pv: int) -> BGRImage:
        """`tl`, `pl`, `tv`, `pv`の角度条件の画像をndarray形式で返す

        `filename`が含まれるファイルが存在しない場合は`ValueError`を投げる。
        """
        key = (tl, pl, tv, pv)
        filepath = self.__angles_vs_filepath_dict.get(key)
        if not filepath:
            raise ValueError(
                f"Condition {key} does not exist in '{self.zip_filepath}'.")

        with self.__z.open(filepath) as f:
            return decode_jpeg(f.read(), colorspace="BGR")
Exemplo n.º 6
0
def transform(image):
    # image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    try:
        image = simplejpeg.decode_jpeg(image, colorspace="bgr")
    except:
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    image = resize(image)
    image = random_crop(image, 224, 224)

    if random.random() < 0.5:
        image = cv2.flip(image, 1)

    image = image / 255.0
    image = image - (0.485, 0.456, 0.406)
    image = image / (0.229, 0.224, 0.225)
    image = image.transpose((2, 0, 1))
    # print(image.shape)
    image = torch.from_numpy(image)
    return image
Exemplo n.º 7
0
def transform(raw):
    # image = np.asarray(bytearray(raw), dtype="uint8")
    images = []
    for i in range(len(raw)):
        # images.append(cv2.imdecode(raw[i], cv2.IMREAD_COLOR))
        images.append(simplejpeg.decode_jpeg(raw[i], colorspace="bgr"))
        # print(images[i])
    # for image in images:
    #     image = resize(image)
    #     image = random_crop(image,224,224)


    #     if random.random()<0.5:
    #         image = cv2.flip(image, 1)

    #     image = image / 255.0
    #     image = image - (0.485, 0.456, 0.406)
    #     image = image / (0.229, 0.224, 0.225)
    #     image = image.transpose((2, 0, 1))
        # print(image)
    return images
Exemplo n.º 8
0
def test_jpeg(shape, num_channels):
  import simplejpeg

  xshape = list(shape) + [ num_channels ]
  data = np.zeros(shape=xshape, dtype=np.uint8)
  encode_decode(data, 'jpeg', shape, num_channels)
  encode_decode(data + 255, 'jpeg', shape, num_channels)

  jpg = simplejpeg.decode_jpeg(
    encode(data, 'jpeg'), 
    colorspace="GRAY",
  )
  assert jpg.shape[0] == shape[1] * shape[2]
  assert jpg.shape[1] == shape[0]

  # Random jpeg won't decompress to exactly the same image
  # but it should have nearly the same average power
  random_data = np.random.randint(255, size=xshape, dtype=np.uint8)
  pre_avg = random_data.copy().flatten().mean()
  encoded = encode(random_data, 'jpeg')
  decoded = decode(encoded, 'jpeg', shape=xshape, dtype=np.uint8)
  post_avg = decoded.copy().flatten().mean()

  assert abs(pre_avg - post_avg) < 1
Exemplo n.º 9
0
import cv2
import imagezmq
import pyfakewebcam
import simplejpeg

# Create fake webcam device:
# sudo modprobe v4l2loopback devices=1
# check stream:
# ffplay /dev/video2

image_hub = imagezmq.ImageHub(open_port="tcp://192.168.12.29:5555", REQ_REP=False)
fake_camera = None

while True:
    sender_name, jpg_buffer = image_hub.recv_jpg()
    image = simplejpeg.decode_jpeg(jpg_buffer, colorspace="BGR")
    if fake_camera is None:
        height, width, _ = image.shape
        fake_camera = pyfakewebcam.FakeWebcam("/dev/video2", width, height)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    fake_camera.schedule_frame(image)
Exemplo n.º 10
0
    def decode(self,
               img,
               key,
               func=2,
               verbose=True,
               use_rs=True,
               output_file="stego",
               greyscale=False):
        if verbose:
            with open(img, 'r') as f:
                bitstring = f.read()

            with open('.imgdim', 'rb') as fp:
                self.img_height, self.img_width = pickle.load(fp)

            with open('.v_imgdim', 'rb') as fp:
                v_img_height, v_img_width = pickle.load(fp)

            self.hor_block_count = self.img_width // self.BLOCK_SIZE
            self.ver_block_count = self.img_height // self.BLOCK_SIZE

            hash_path = self.retrievePath(key)
            # extract data from Huffman encoding
            Y_decoded_img, Cb_decoded_img, Cr_decoded_img = self.huffmanDecode(
                bitstring)
            print("finished decode")

            img = self.unRLE([Y_decoded_img, Cb_decoded_img, Cr_decoded_img])
            print("extracted zigzags")

            if func == 0:
                msg_path = self.formatPathF5(hash_path)
                message = self.extractF5(msg_path, img, False)
            elif func == 1:
                msg_path = self.formatPath(hash_path, mode=1)
                message = self.extractsdcsF5(msg_path, img)
            elif func == 2:
                msg_path = self.formatPath(hash_path, mode=0)
                message = self.extractdmcss(msg_path, img)
            elif func == 3:
                msg_path = self.formatPathF5(hash_path)
                message = self.extractF5(msg_path, img, True)
            if use_rs:
                rs_obj = rs(self.RS_PARAM)
                polys = self.extractRSPoly(message)
                polys = [
                    polys[i:i + rs_obj.N]
                    for i in range(0, len(polys), rs_obj.N)
                ]
                message = ''
                for poly in polys:
                    corrected_message = rs_obj.detectErrors(poly)
                    message += ''.join([
                        chr(x)
                        for x in corrected_message[:len(corrected_message) -
                                                   (rs_obj.T * 2)]
                    ])
                print("extracted message:", message)

            else:
                message = self.extractMsgTxt(message)
                print("non-rs extracted message:", message)

            img = self.unDPCM(img)
            print("extracted DC values from DPCM")

            img = self.unZigZag(img)
            print("restored 8x8 tiles")

            img = self.deQuantize(img)
            print("reversed quantization")

            print("beginning dct...")
            img = self.DCT_3(img)
            print("performed inverse DCT")

            img = self.YCbCr2BGR(img)
            print("converted YCbCr to BGR")

            img = self.assembleImage(img)

            if self.img_height != v_img_height:
                img = self.removeVPadding(img, v_img_height)
            if self.img_width != v_img_width:
                img = self.removeHPadding(img, v_img_width)
            cv2.imwrite(output_file + '.png', img)
            print("done!")

        else:
            from encoder import encoder
            encoder_obj = encoder(self.BLOCK_SIZE, self.RS_PARAM)
            encoder_obj.defineBlockCount(self.ver_block_count,
                                         self.hor_block_count)
            if not greyscale:
                with open(img + ".jpg", "rb") as f:
                    jpg_img = simplejpeg.decode_jpeg(f.read(), 'BGR', False,
                                                     False)
                    jpg_img = cv2.cvtColor(jpg_img, cv2.COLOR_BGR2YCR_CB)
            else:
                jpg_img = cv2.imread(img + ".jpg", cv2.IMREAD_GRAYSCALE)
            self.img_height, self.img_width = self.getImageDimensions(jpg_img)
            encoder_obj.defineImgDim(self.img_height, self.img_width)
            if self.img_width % self.BLOCK_SIZE != 0:
                jpg_img = encoder_obj.padImageWidth(jpg_img)
            if self.img_height % self.BLOCK_SIZE != 0:
                jpg_img = encoder_obj.padImageHeight(jpg_img)
            new_img_height, new_img_width = self.getImageDimensions(jpg_img)
            self.hor_block_count, self.ver_block_count = new_img_width // self.BLOCK_SIZE, new_img_height // self.BLOCK_SIZE
            total_blocks = self.ver_block_count * self.hor_block_count

            if not greyscale:
                Y_img, Cr_img, Cb_img = cv2.split(jpg_img)
                img = encoder_obj.blockify([Y_img, Cb_img, Cr_img])
            else:
                img = encoder_obj.blockify([jpg_img])
            #print("Separated successfully")

            #print("beginning dct...")
            img = encoder_obj.DCT_2(img)
            #print("finished dct")

            img = encoder_obj.quantizeAndRound(img)
            #print("finished quantization and round")

            img = encoder_obj.zigZagEncode(img)
            #print("finished zigzag")

            img = [
                np.reshape(channel,
                           (total_blocks, self.BLOCK_SIZE * self.BLOCK_SIZE))
                for channel in img
            ]

            hash_path = self.retrievePath(key)

            if func == 0:
                msg_path = self.formatPathF5(hash_path)
                message = self.extractF5(msg_path, img, False)
            elif func == 1:
                msg_path = self.formatPath(hash_path, mode=1)
                message = self.extractsdcsF5(msg_path, img)
            elif func == 2:
                msg_path, parity = self.formatPath(hash_path, mode=0)
                message = self.extractOptimaldmcss(msg_path, parity, img)
            elif func == 3:
                msg_path = self.formatPathF5(hash_path)
                message = self.extractF5(msg_path, img, True)
            if use_rs:
                rs_obj = rs(self.RS_PARAM)
                polys = self.extractRSPoly(message)
                polys = [
                    polys[i:i + rs_obj.N]
                    for i in range(0, len(polys), rs_obj.N)
                ]
                message = ''
                for poly in polys:
                    corrected_message = rs_obj.detectErrors(poly)
                    message += ''.join([
                        chr(x)
                        for x in corrected_message[:len(corrected_message) -
                                                   (rs_obj.T * 2)]
                    ])
            else:
                message = self.extractMsgTxt(message)
                #print("non-rs extracted message:", message)
            return message
            with open(output_file + ".txt", 'w', encoding="utf-8") as f:
                f.write(message)
            print("message extracted successfully")
            exit(0)