Ejemplo n.º 1
0
def convertFileToYUV(infilename, outfilename):

    if "TIF" in infilename:
        width, height, pixels = functions.convertTifToYUV420(infilename)
        functions.appendToFile(pixels, outfilename)
        return width, height
    if "PNG" in infilename:
        img = Image.open(infilename)
        print(img)
        print(img.size)
        print(img.mode)
        #I = img.load()
        width, height = img.size
        print("width {}, height {}".format(width, height))
        #print(pixels)
        #pixels = np.asarray(Image.open(infilename))
        if "L" in img.mode:
            print("It's greyscale")
            if "A" in img.mode:
                print("There's an alpha channel")
                pixels = np.asarray(img.getdata()).reshape(height, width, 2)
                pixels = pixels[:, :, 0]
            else:
                pixels = np.asarray(img.getdata()).reshape(img.size)
        print("The shame of the pixels: {}".format(pixels.shape))
        pixels = pixels.flatten()
        frameSize = width * height
        uv = np.full((frameSize // 2), 128)
        print("The framesize is {}, width {}, height {}".format(frameSize, width, height))
        print(uv.shape)
        functions.appendToFile(pixels, outfilename)
        functions.appendToFile(uv, outfilename)

        return width, height
Ejemplo n.º 2
0
def patchABitOfOneFile(fileIn,
                       fileOut,
                       start=0,
                       end=0,
                       label="qp",
                       cropDim=80,
                       cropTempStep=1,
                       cropSpacStep=16,
                       num_channels=3,
                       bit_depth=8):
    width, height = getDimsFromFileName(fileIn)
    patchList = []
    #print("Width is {}, height is {}".format(width, height))
    if label == "qp":
        qp = getQuantFromFileName(fileIn)
        if qp < 0:
            qp = 0
        label = int(qp / 7)
        label = qp
        label = int(qp / 2)
        label = int(qp / 3)
        print(label)
    if label == "qp_only":
        qp = getQuantFromFileName(fileIn)
        print("Got qp {} from {}".format(qp, fileIn))
        if qp < 0:
            qp = 0
        label = int(qp)
    if label == "none":
        label = 0

    frameSize = width * height * 3 // 2
    print("The file is {} with width {}, height {}, label {}".format(
        fileIn, width, height, label))

    with open(fileIn, "rb") as f:
        mybytes = np.fromfile(f, 'u1')
    print("There are {} bytes in the file width: {} height: {}".format(
        len(mybytes), width, height))
    num_frames = len(mybytes) / frameSize
    print("There are {} frames".format(num_frames))

    if end == 0:
        end = num_frames

    for f in range(start, end, cropTempStep):
        #print("Frame number {}".format(f))
        start = f * frameSize
        end = start + frameSize
        myframe = mybytes[start:end]
        my444frame = functions.YUV420_2_YUV444(myframe, height, width)

        patches = makeNormalisedPatches(my444frame, width, height, cropDim,
                                        cropDim, cropSpacStep, num_channels,
                                        bit_depth, label)
        patchList.extend(patches)

    patches_array = np.array(patchList)

    patches_array = patches_array.flatten()
    patchSize = (cropDim * cropDim * 3) + 1
    numPatches = patches_array.shape[0] / patchSize
    print("Dims: {}, numPatches {}".format(patches_array.shape, numPatches))
    patches_array = patches_array.reshape((numPatches, patchSize))

    ############## Here's where you name the files!!!!###########

    multipleOutFiles = False
    if multipleOutFiles:
        numBinFiles = 10
        patchesPerFile = numPatches // numBinFiles
        if numPatches < 100:
            patchesPerFile = 1

        for i in range(0, (numBinFiles - 1)):
            outFileName = "{}_{}.bin".format(fileOut, i)
            start = patchesPerFile * i
            end = start + patchesPerFile
            arrayCut = patches_array[start:end, :]
            outFileName = os.path.join(cropDir, outFileName)
            functions.appendToFile(arrayCut, outFileName)

        # the last file is a bit bigger
        outFileName = "{}_{}.bin".format(binFileName, numBinFiles)
        start = patchesPerFile * (numBinFiles - 1)
        arrayCut = patches_array[start:, :]
        outFileName = os.path.join(cropDir, outFileName)
        functions.appendToFile(arrayCut, outFileName)
    else:
        functions.appendToFile(patches_array, fileOut)
    return numPatches
Ejemplo n.º 3
0
            value = ((maxDist - dist) / maxDist) * 192
            value = round(value)
            #print(value)
            y[j, i] = value

    doUV = False
    if doUV:
        for i in range(0, uw):
            for j in range(0, uh):
                xdist = abs(i - centx2)
                ydist = abs(j - centy2)
                value1 = (xdist * 255 / uw)
                value1 = round(value1)
                value2 = (ydist * 255 / uw)
                value2 = round(value2)
                print(value1)
                print(value2)
                #print(i, j)
                u[j, i] = value1
                v[j, i] = value2

    y = y.flatten()
    u = u.flatten()
    v = v.flatten()

    frame = np.concatenate((y, u, v), axis=0)
    filename = "aMeh_{}x{}.yuv".format(width, height)

    fn.appendToFile(frame, filename)
Ejemplo n.º 4
0
            datasetList.append(datayuv)
            numPatches = numPatches + 1
            patchNo = patchNo + 1

        #quit()
    totPatches = len(datasetList)
    print(
        "TotalPatches: {}, tampered patches: {}, we took {} patches from each auth file"
        .format(totPatches, totTamperedPatches, cropsPerAuthPic))
    print("failed list {}".format(failedList))
    dataset_array = np.array(datasetList)
    print("Size of Dataset: {}".format(dataset_array.shape))
    if shuffled:
        np.random.shuffle(dataset_array)
        print("Size of Dataset: {}".format(dataset_array.shape))
    functions.appendToFile(dataset_array, datasetName)

    numFrags = 10
    if shuffled == False:
        numFrags = 1
        # print the file names list to a file
        with open("{}_names.txt".format(datasetName), "w") as ff:
            for item in unshuffledNames:
                ff.write("%s\n" % item)

    patchesPerFrag = dataset_array.shape[0] // numFrags
    print(patchesPerFrag)
    for x in range(0, numFrags):
        fragName = "{}_{}.bin".format(datasetName, x)
        start = x * patchesPerFrag
        end = start + patchesPerFrag
Ejemplo n.º 5
0
    # dirty shuffle, hope the array isn't too big!!!
    if shuffled:
        np.random.shuffle(patches_array)

    print("Ok, shuffling done, we have {} patches and shape {}".format(
        numPatches, patches_array.shape))

    ############## Here's where you name the files!!!!###########
    numBinFiles = 10
    patchesPerFile = numPatches // numBinFiles
    if numPatches < 100:
        patchesPerFile = 1

    for i in range(0, (numBinFiles - 1)):
        outFileName = "{}_{}.bin".format(binFileName, i)
        start = patchesPerFile * i
        end = start + patchesPerFile
        #print("{} end {}".format(start, end))
        arrayCut = patches_array[start:end, :]
        outFileName = os.path.join(cropDir, outFileName)
        functions.appendToFile(arrayCut, outFileName)

    # the last file is a bit bigger
    outFileName = "{}_{}.bin".format(binFileName, (numBinFiles - 1))
    start = patchesPerFile * (numBinFiles - 1)
    arrayCut = patches_array[start:, :]
    outFileName = os.path.join(cropDir, outFileName)
    functions.appendToFile(arrayCut, outFileName)
    quit()
                firstHalvesList.append(outfile0)
                secondHalvesList.append(outfile1)
                thirdHalvesList.append(outfile2)
                fourthHalvesList.append(outfile3)

                width, height = pi.getDimsFromFileName(infile0)
                with open(infile0, "rb") as f:
                    mybytes = np.fromfile(f, 'u1')
                frameSize = width * height * 3 // 2  # magic numbers because it's YUV 420.
                num_frames = len(mybytes) / frameSize
                #print(num_frames)
                #print(num_frames//2)
                #print(mybytes.shape)
                firstHalf = mybytes[0:((num_frames // 4) * frameSize)].copy()
                functions.appendToFile(firstHalf, outfile0)
                secondHalf = mybytes[((num_frames // 4) *
                                      frameSize):((num_frames // 2) *
                                                  frameSize)].copy()
                functions.appendToFile(secondHalf, outfile1)
                thirdHalf = mybytes[((num_frames // 2) *
                                     frameSize):((num_frames // 4) *
                                                 frameSize * 3)].copy()
                functions.appendToFile(thirdHalf, outfile2)
                fourthHalf = mybytes[((num_frames // 4) * frameSize *
                                      3):].copy()
                functions.appendToFile(fourthHalf, outfile3)

            fileList = firstHalvesList
            print(firstHalvesList)
            print(secondHalvesList)
Ejemplo n.º 7
0
                    print("Taking MB: {}".format(mbNo))
                    yuv = patches_nonintra[mbNo]
                    datayuv = np.concatenate((np.array([label]), yuv), axis=0)
                    datayuv = datayuv.flatten()
                    patchesList.append(datayuv)
                    numPatches = numPatches + 1

                    intralabel = 0
                    yuv = patches_intra[mbNo]
                    datayuv = np.concatenate((np.array([intralabel]), yuv),
                                             axis=0)
                    datayuv = datayuv.flatten()
                    patchesList.append(datayuv)
                    numPatches = numPatches + 1

                mbNo += mbInc
                if ((mbNo % mbwidth) == 0) or ((mbNo % mbwidth)
                                               == (mbwidth - 1)):
                    mbNo += 2
            frameNo += frameInc

        patches_array = np.array(patchesList)
        print("Dims: {}, numPatches {}".format(patches_array.shape,
                                               numPatches))
        ############## Here's where you name the files!!!!###########
        patchNumber = numPatches // 1000
        outFileName = "patches_{}.bin".format(patchNumber)
        outFileName = os.path.join(outputDir, outFileName)
        functions.appendToFile(patches_array, outFileName)
        patchesList = []
Ejemplo n.º 8
0
    with open(inFilename, "rb") as f:
        mybytes = np.fromfile(f, 'u1')
    print("There are {} bytes in the file width: {} height: {}".format(
        len(mybytes), width, height))
    num_frames = len(mybytes) / frameSize

    a = mybytes.reshape((num_frames, frameSize))

    # Here's the crop!
    if (frameStart != 0) or (frameEnd != 0):
        print("cropping")
        a = a[frameStart:frameEnd, :]

    if normalise:
        row_sums = a[:, 0:frameArea].sum(axis=1)
        print(a[0, :])
        #print(row_sums)
        a = a - (row_sums[:, np.newaxis] // frameArea)
        print(a[0, :])
        a = abs(a)
        print(a[0, :])
        #a = a * 256
        # now reset the uv components to 128...?
        a[:, frameArea:] = 128
        print(a[0, :])

    functions.appendToFile(a, outFilename)

    quit()
def compressWithConstantQuant(inputFileName,
                              theOutputFileName,
                              qps=[
                                  0,
                              ],
                              patchWidth=256,
                              patchHeight=256,
                              patchChannels=3,
                              labelSize=1):
    pixelSize = patchWidth * patchHeight * patchChannels
    recordSize = labelSize + pixelSize
    bitDepth = 8

    with open(inputFileName, "rb") as f:
        allTheData = np.fromfile(f, 'u1')
    numRecords = allTheData.shape[0] / recordSize
    allTheData = allTheData.reshape(numRecords, recordSize)
    print(allTheData.shape)

    # Convert each picture to a YUV file
    tempYUVFileName = "temp_{}x{}.yuv".format(patchWidth, patchHeight)
    open(tempYUVFileName, 'w').close()

    labels = allTheData[:, 0].copy()
    yuvFrames = allTheData[:, 1:].copy()
    yuvFrames = yuvFrames * bitDepth
    print("Saving frames to {}, shape {}".format(tempYUVFileName,
                                                 yuvFrames.shape))
    for frame in yuvFrames:
        #print("Frame")
        yuv420 = functions.YUV444_2_YUV420(frame, patchWidth, patchHeight)
        functions.appendToFile(yuv420, tempYUVFileName)

    # encode that file as all intra...
    for qp in qps:
        tempH264FileName = "temp_{}x{}_{}.264".format(patchWidth, patchHeight,
                                                      qp)
        tempDecompFileName = "temp_{}x{}_{}_decomp.yuv".format(
            patchWidth, patchHeight, qp)
        open(tempH264FileName, 'w').close()
        open(tempDecompFileName, 'w').close()
        #outputFileName = theOutputFileName.replace('.bin', '_{}.bin'.format(qp))
        outputFileName = theOutputFileName.replace('/t', '/qp{}/t'.format(qp))
        functions.compressFile(x264,
                               tempYUVFileName,
                               patchWidth,
                               patchHeight,
                               qp,
                               tempH264FileName,
                               tempDecompFileName,
                               deblock=False,
                               intraOnly=True,
                               verbose=False)

        # read in the decompressed YUV file:
        with open(tempDecompFileName, "rb") as f:
            allTheYUV420 = np.fromfile(f, 'u1')
        allTheYUV420 = allTheYUV420.reshape((numRecords, -1))
        datasetList = []
        for idx, frame in enumerate(allTheYUV420):
            datayuv = functions.YUV420_2_YUV444(frame, patchWidth, patchHeight)
            datayuv = np.divide(datayuv, 8)
            label = labels[idx]
            datayuv = np.concatenate((np.array([label]), datayuv), axis=0)
            datayuv = datayuv.flatten()
            datasetList.append(datayuv)

        dataset_array = np.array(datasetList)
        print("Size of Dataset: {}".format(dataset_array.shape))
        functions.appendToFile(dataset_array, outputFileName)

    print("All done!")
Ejemplo n.º 10
0
def prepareDataForHeatMapGeneration():
    print("Preparing a bin file that's suitable for a heat map")
    patchDim = 80
    patchStride = 16  # for macroblock-ness
    fileList = [['/Users/pam/Documents/data/yuv/flower_1f_q0.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q7.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q14.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q21.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q28.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q35.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q42.yuv', 352, 288],
                ['/Users/pam/Documents/data/yuv/flower_1f_q49.yuv', 352, 288]]
    fileList = [
        [
            '/Users/pam/Documents/data/SULFA/forged_sequences_avi/01_forged_1f.yuv',
            320, 240
        ],
        [
            '/Users/pam/Documents/data/SULFA/forged_sequences_avi/01_original_1f.yuv',
            320, 240
        ]
    ]
    fileList = [[
        '/Users/pam/Documents/data/DeepFakes/creepy2_1f.yuv', 1280, 720
    ]]
    for file in fileList:
        filename = file[0]
        fileBaseName, ext = os.path.splitext(filename)
        width = file[1]
        height = file[2]
        frameSize = height * width * 3 / 2  # for i420 only
        try:
            quant = getQuant(filename)
        except:
            quant = 0
        print("The quant for {} is {}".format(quant, filename))
        with open(filename, "rb") as f:
            allTheData = np.fromfile(f, 'u1')
        print(allTheData.shape)
        numFrames = allTheData.shape[0] / frameSize
        print("There's {} frames".format(numFrames))

        frameData = allTheData
        frame444 = functions.YUV420_2_YUV444(frameData, height, width)
        frame444, newWidth, newHeight = addABorder(frame444, width, height, 32,
                                                   32, 32, 32)
        outFileName = "{}.YUV444_{}x{}".format(fileBaseName, newWidth,
                                               newHeight)
        functions.appendToFile(frame444, outFileName)

        # And now the generation of patches
        patchesList = []
        numPatches = 0
        filesWritten = 0
        frameData = frame444
        ySize = newWidth * newHeight
        uvSize = newWidth * newHeight
        yData = frameData[0:ySize]
        uData = frameData[ySize:(ySize + uvSize)]
        vData = frameData[(ySize + uvSize):(ySize + uvSize + uvSize)]
        # print("yData shape: {}".format(yData.shape))
        # print("uData shape: {}".format(uData.shape))
        # print("vData shape: {}".format(vData.shape))
        yData = yData.reshape(newHeight, newWidth)
        uData = uData.reshape(newHeight, newWidth)
        vData = vData.reshape(newHeight, newWidth)
        pixelSample = 0
        xCo = 0
        yCo = 0
        maxPixelSample = (
            (newHeight - patchDim) * newWidth) + (newWidth - patchDim)
        # print("maxPixelSample: {}".format(maxPixelSample))
        while yCo <= (newHeight - patchDim):
            # print("Taking sample from: ({}, {})".format(xCo, yCo))
            patchY = yData[yCo:(yCo + patchDim), xCo:(xCo + patchDim)]
            patchU = uData[yCo:(yCo + patchDim), xCo:(xCo + patchDim)]
            patchV = vData[yCo:(yCo + patchDim), xCo:(xCo + patchDim)]

            # print("patch dims: y {} u {} v {}".format(patchY.shape, patchU.shape, patchV.shape))
            yuv = np.concatenate(
                (np.divide(patchY.flatten(), 8), np.divide(
                    patchU.flatten(), 8), np.divide(patchV.flatten(), 8)),
                axis=0)
            # print("patch dims: {}".format(yuv.shape))
            yuv = yuv.flatten()
            datayuv = np.concatenate((np.array([quant]), yuv), axis=0)
            datayuv = datayuv.flatten()
            patchesList.append(datayuv)
            numPatches = numPatches + 1

            xCo = xCo + patchStride
            if xCo > (newWidth - patchDim):
                xCo = 0
                yCo = yCo + patchStride
            pixelSample = (yCo * newWidth) + xCo

            patches_array = np.array(patchesList)
            print("Dims: {}, numPatches {}".format(patches_array.shape,
                                                   numPatches))
            ############## Here's where you name the files!!!!###########
            outFileName = "{}.bin".format(fileBaseName)
            outFileName = "patches_test_{}.bin".format(quant)
            functions.appendToFile(patches_array, outFileName)
            patchesList = []

        #Add more "patches" so that we have blank patches up to a multiple of 128 (batch size)
        batchPatchNum = (np.ceil(numPatches / 128.0) * 128) - numPatches
        print("Adding a further {} patches to round to batches".format(
            batchPatchNum))
        patchY = np.full([patchDim, patchDim], 0)
        patchU = np.full([patchDim, patchDim], 128)
        patchV = np.full([patchDim, patchDim], 128)
        while batchPatchNum > 0:
            yuv = np.concatenate(
                (np.divide(patchY.flatten(), 8), np.divide(
                    patchU.flatten(), 8), np.divide(patchV.flatten(), 8)),
                axis=0)
            # print("patch dims: {}".format(yuv.shape))
            yuv = yuv.flatten()
            datayuv = np.concatenate((np.array([quant]), yuv), axis=0)
            datayuv = datayuv.flatten()
            patchesList.append(datayuv)
            patches_array = np.array(patchesList)
            functions.appendToFile(patches_array, outFileName)
            patchesList = []
            batchPatchNum = batchPatchNum - 1
Ejemplo n.º 11
0
def extractPatches_byQuant(fileList,
                           outFileBaseName,
                           patchDim=80,
                           patchStride=48,
                           frameSampleStep=30,
                           numChannels=3):
    patchesList = []
    numPatches = 0
    filesWritten = 0
    for file in fileList:
        filename = file[0]
        width = file[1]
        height = file[2]
        frameSize = height * width * 3 / 2  # for i420 only
        quant = getQuant(filename)
        with open(filename, "rb") as f:
            allTheData = np.fromfile(f, 'u1')
        print(allTheData.shape)
        numFrames = allTheData.shape[0] / frameSize
        print("There are {} frames".format(numFrames))

        allTheData = allTheData.reshape(numFrames, frameSize)
        frameSample = 0
        lastFrame = numFrames
        if width == 1280:
            frameSample = 2
            lastFrame = numFrames - 2

        while frameSample < lastFrame:
            frameData = allTheData[frameSample]
            ySize = width * height
            uvSize = (width * height) / 4
            yData = frameData[0:ySize]
            uData = frameData[ySize:(ySize + uvSize)]
            vData = frameData[(ySize + uvSize):(ySize + uvSize + uvSize)]
            #print("yData shape: {}".format(yData.shape))
            #print("uData shape: {}".format(uData.shape))
            #print("vData shape: {}".format(vData.shape))
            yData = yData.reshape(height, width)
            uData = uData.reshape(height / 2, width / 2)
            vData = vData.reshape(height / 2, width / 2)
            pixelSample = 0
            xCo = 0
            yCo = 0
            maxPixelSample = ((height - patchDim) * width) + (width - patchDim)
            #print("maxPixelSample: {}".format(maxPixelSample))
            while yCo < (height - patchDim):
                #print("Taking sample from: ({}, {})".format(xCo, yCo))
                patchY = yData[yCo:(yCo + patchDim), xCo:(xCo + patchDim)]
                patchU = uData[(yCo / 2):((yCo + patchDim) / 2),
                               (xCo / 2):((xCo + patchDim) / 2)]
                patchU = np.repeat(patchU, 2, axis=0)
                patchU = np.repeat(patchU, 2, axis=1)
                patchV = vData[(yCo / 2):((yCo + patchDim) / 2),
                               (xCo / 2):((xCo + patchDim) / 2)]
                patchV = np.repeat(patchV, 2, axis=0)
                patchV = np.repeat(patchV, 2, axis=1)

                #print("patch dims: y {} u {} v {}".format(patchY.shape, patchU.shape, patchV.shape))
                yuv = np.concatenate((np.divide(
                    patchY.flatten(), 8), np.divide(
                        patchU.flatten(), 8), np.divide(patchV.flatten(), 8)),
                                     axis=0)
                #print("patch dims: {}".format(yuv.shape))
                yuv = yuv.flatten()
                datayuv = np.concatenate((np.array([quant]), yuv), axis=0)
                datayuv = datayuv.flatten()
                patchesList.append(datayuv)
                numPatches = numPatches + 1

                xCo = xCo + patchStride
                if xCo > (width - patchDim):
                    xCo = 0
                    yCo = yCo + patchStride
                pixelSample = (yCo * width) + xCo
                #print("numPatches: {}".format(numPatches))

                patches_array = np.array(patchesList)
                print("Dims: {}, numPatches {}".format(patches_array.shape,
                                                       numPatches))
                outFileName = "{}_{}.bin".format(outFileBaseName, quant)
                functions.appendToFile(patches_array, outFileName)
                filesWritten += 1
                patchesList = []
                numPatches = 0

            frameSample = frameSample + frameSampleStep
Ejemplo n.º 12
0
import functions
import time



if __name__ == "__main__":

    print("Converting a .png to YUV")

    infilename = "/Users/pam/Documents/data/Breast/p00001/CC.png"
    img = Image.open(infilename)
    print(img)
    I = img.load()
    orig_width, orig_height = img.size
    width = (orig_width//16)*16
    height = (orig_height//16)*16
    print("width {}, height {}".format(width, height))
    #print(pixels)
    pixels = np.asarray(Image.open(infilename))
    pixels = pixels[:height, :width]
    pixels = pixels.flatten()
    frameSize = width * height
    uv = np.full((frameSize // 2), 128)

    outfilename = "/Users/pam/Documents/data/Breast/p00001/CC_{}x{}.yuv".format(width, height)
    if os.path.exists(outfilename):
       os.remove(FLAGS.heatmap)
    functions.appendToFile(pixels, outfilename)
    functions.appendToFile(uv, outfilename)
    quit()