def binDiffYUV(srcFile, srcNumber, width, height, start, end, dstDir): pngNames = [] pngNumber = 0 frameSize = int(width * height * 3 / 2) ysize = width * height usize = (width * height) // 4 with open(srcFile, "rb") as f: allbytes = np.fromfile(f, 'u1') numFrames = int(allbytes.shape[0] // frameSize) allbytes = allbytes.reshape((numFrames, frameSize)) for frameNumber in range(start, end - 1): pngName = os.path.join(dstDir, "{}_{}.png".format(srcNumber, pngNumber)) one = allbytes[frameNumber, :] two = allbytes[(frameNumber + 1), :] diff = abs(one - two) #Here we do the binarising...! myFrame = np.copy(diff) myFrame[(width * height):] = 128 #y = myFrame[0:(width*height)] #u = myFrame[ysize:(ysize+usize)] #v = myFrame[(ysize+usize):] frame = functions.YUV420_2_YUV444(myFrame, height, width) rgbframe = functions.planarYUV_2_planarRGB(frame, height, width) savePic(pngName, rgbframe, width, height) pngNames.append(pngName) pngNumber = pngNumber + 1 return pngNames
def YUVFramesToPNGs(srcFile, srcNumber, width, height, start, end, dstDir, resize=False): pngNames = [] frameSize = int(width * height * 3 / 2) with open(srcFile, "rb") as f: allbytes = np.fromfile(f, 'u1') numFrames = int(allbytes.shape[0] // frameSize) allbytes = allbytes.reshape((numFrames, frameSize)) print("Converting YUV to PNG files: {} frames".format(numFrames)) print(allbytes.shape) pngNumber = 0 for frameNumber in range(start, end): pngName = os.path.join(dstDir, "{}_{}.png".format(srcNumber, pngNumber)) print("Save {}".format(pngName)) myFrame = allbytes[frameNumber, :] frame = functions.YUV420_2_YUV444(myFrame, height, width) rgbframe = functions.planarYUV_2_planarRGB(frame, height, width) savePic(pngName, rgbframe, width, height, resize) pngNames.append(pngName) pngNumber = pngNumber + 1 return pngNames
def extractPixelPatch(filename, frameNo, mbNo, frameW, frameH, channels=1.5, patchW=16, patchH=16, xstride=16, ystride=16, lborder=8, rborder=8, tborder=8, bborder=8): frameSize = int(frameW * frameH * channels) bytePos = frameSize * frameNo mbwidth = int(width / 16) mbheight = int(height / 16) mbsize = mbwidth * mbheight with open(filename, "rb") as f: f.seek(bytePos) pixels = f.read(frameSize) pixels = bytearray(pixels) # Adding the border to the frame saves us worrying about testing borders during patch-ification frame444 = functions.YUV420_2_YUV444(pixels, height, width) frame444, newWidth, newHeight = encodeYUV.addABorder( frame444, width, height, lborder, rborder, tborder, bborder) # with the border, (lborder, tborder) is the top left of the first macroblock ySize = newWidth * newHeight uvSize = newWidth * newHeight frameData = frame444 yData = frameData[0:ySize] uData = frameData[ySize:(ySize + uvSize)] vData = frameData[(ySize + uvSize):(ySize + uvSize + uvSize)] yData = yData.reshape(newHeight, newWidth) uData = uData.reshape(newHeight, newWidth) vData = vData.reshape(newHeight, newWidth) xCo = int((mbNo % mbsize) * 16) yCo = int(((mbNo - xCo) % mbsize) * 16) patchY = yData[yCo:(yCo + patchH), xCo:(xCo + patchW)] patchU = uData[yCo:(yCo + patchH), xCo:(xCo + patchW)] patchV = vData[yCo:(yCo + patchH), xCo:(xCo + patchW)] yuv = np.concatenate( (np.divide(patchY.flatten(), 8), np.divide( patchU.flatten(), 8), np.divide(patchV.flatten(), 8)), axis=0) yuv = yuv.flatten() return yuv
def getBGRFrameFromYUV420File(filename, width, height, frameNumber, RGB=False): yuvFrameSize = int(width * height * 3 / 2) bytePos = yuvFrameSize * frameNumber offEnd = 0 with open(filename, "rb") as f: #pixels = [] #for i in range(0, yuvFrameSize): # pixels = f.read(yuvFrameSize) # pixel = struct.unpack('c', p) # pixels.append(pixels) #print(pixels) #print(bytePos) f.seek(bytePos) pixels = f.read(yuvFrameSize) pixels = bytearray(pixels) #print("Obtained len {} vs {}".format(len(pixels), yuvFrameSize)) if len(pixels) != yuvFrameSize: #wrap round when we get to the end... f.seek(0) pixels = f.read(yuvFrameSize) pixels = bytearray(pixels) offEnd = 1 pixels = np.asarray(pixels) #print("The pixels size: {}".format(pixels.shape)) yuv444 = functions.YUV420_2_YUV444(pixels, height, width) if RGB: pixels = functions.planarYUV_2_planarRGB(yuv444, height, width) else: pixels = functions.planarYUV_2_planarBGR(yuv444, height, width) pixels = pixels.reshape((3, height, width)) pixels = np.swapaxes(pixels, 0, 1) pixels = np.swapaxes(pixels, 1, 2) return offEnd, pixels
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
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)) for f in range(0, num_frames, cropTempStep): print("Frame number {}".format(f)) if f in avoidFrames: print("Avoiding frame {}".format(f)) f = f + 1 if f > num_frames: break 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) # DEBUGGING starts here!!!!! patches_array = patches_array.flatten() patchSize = (cropDim * cropDim * 3) + 1 print(patches_array.shape) numPatches = patches_array.shape[0] / patchSize print("Dims: {}, numPatches {}".format(patches_array.shape, numPatches))
def extractPatchesAndReturnWholeFile(fileName, frameW, frameH, channels=1.5, patchW=32, patchH=32, xstride=16, ystride=16, lborder=8, rborder=8, tborder=8, bborder=8): patchesList = [] numPatches = 0 frameSize = int(frameW * frameH * channels) mbwidth = int(width / 16) mbheight = int(height / 16) mbsize = mbwidth * mbheight with open(fileName, "rb") as f: pixels = f.read() allpixels = bytearray(pixels) bytePos = 0 print("We have {} pixels in total".format(len(allpixels))) while bytePos < len(allpixels): print("Frame: {}".format(bytePos // frameSize)) print("The byte position in the yuv file {}: {}".format( fileName, bytePos)) nextBytePos = (bytePos + frameSize) # Adding the border to the frame saves us worrying about testing borders during patch-ification pixels = allpixels[bytePos:nextBytePos] frame444 = functions.YUV420_2_YUV444(pixels, height, width) frame444, newWidth, newHeight = encodeYUV.addABorder( frame444, width, height, lborder, rborder, tborder, bborder) frameData = frame444 ySize = newWidth * newHeight uvSize = newWidth * newHeight frameData = frame444 yData = frameData[0:ySize] uData = frameData[ySize:(ySize + uvSize)] vData = frameData[(ySize + uvSize):(ySize + uvSize + uvSize)] yData = yData.reshape(newHeight, newWidth) uData = uData.reshape(newHeight, newWidth) vData = vData.reshape(newHeight, newWidth) pixelSample = 0 xCo = 0 yCo = 0 maxPixelSample = ((height - patchH) * width) + (width - patchW) #print("maxPixelSample: {}".format(maxPixelSample)) #print("newHeight: {} and {}".format(newHeight, (1 + newHeight - patchH))) while yCo < (1 + newHeight - patchH): #print("Taking sample from: ({}, {})".format(xCo, yCo)) patchY = yData[yCo:(yCo + patchH), xCo:(xCo + patchW)] patchU = uData[yCo:(yCo + patchH), xCo:(xCo + patchW)] patchV = vData[yCo:(yCo + patchH), xCo:(xCo + patchW)] #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() patchesList.append(yuv) numPatches = numPatches + 1 xCo = xCo + xstride if xCo > (1 + newWidth - patchW): xCo = 0 yCo = yCo + ystride #print("numPatches: {}".format(numPatches)) #print(yCo) bytePos += frameSize patches_array = np.array(patchesList) #np.random.shuffle(patches_array) return patches_array
def processFilePair(o, a, tidyUp=False, crop=True, saveOneFrame=True): dims = getAVIFileDims(o) width, height = getDimsFromFileName(dims) channels = 3 frameSize = (width * height * 3) // 2 print("Dimensions are {} x {}, frame size {}".format(width, height, frameSize)) oyuvname = convertAVItoYUV(o) ayuvname = convertAVItoYUV(a) print("files are {} and {}".format(oyuvname, ayuvname)) oyuv = np.fromfile(oyuvname, 'u1') ayuv = np.fromfile(ayuvname, 'u1') #print(oyuv.shape) oFrame = oyuv[0:frameSize] aFrame = ayuv[0:frameSize] if crop == False: dims = "oneFrame_{}x{}".format(width, height) oCropName = o.replace(".avi", "_{}.yuv".format(dims)) aCropName = a.replace(".avi", "_{}.yuv".format(dims)) f.saveToFile(oFrame, oCropName) f.saveToFile(aFrame, aCropName) #return odata = f.YUV420_2_YUV444(oFrame, height, width) adata = f.YUV420_2_YUV444(aFrame, height, width) odata = odata.reshape((channels, height, width)) adata = adata.reshape((channels, height, width)) diff = abs(odata - adata) if saveOneFrame == True: diff420 = f.YUV444_2_YUV420(diff, height, width) maskFileName = o.replace(".avi", "_{}.yuv".format(dims)) maskFileName = maskFileName.replace("original", "mask") f.saveToFile(diff420, maskFileName) if crop == True: diffY = diff[0:(width*height)] diffInds1 = np.nonzero(diff) minX = np.amin(diffInds1[2]) maxX = np.amax(diffInds1[2]) minY = np.amin(diffInds1[1]) maxY = np.amax(diffInds1[1]) print("bounding box ({},{}) to ({},{})".format(minX, minY, maxX, maxY)) # Adjust the cropped region by adding a border alignTo16grid = True minX = addBorder(minX, width, alignTo16grid, -8) maxX = addBorder(maxX, width, alignTo16grid, 8) minY = addBorder(minY, height, alignTo16grid, -8) maxY = addBorder(maxY, height, alignTo16grid, 8) if (maxX - minX) < 96: maxX = minX + 96 if (maxY - minY) < 96: maxY = minY + 96 print("adjusted bounding box ({},{}) to ({},{})".format(minX, minY, maxX, maxY)) croppedWidth = maxX - minX croppedHeight = maxY - minY dims = "cropped_{}x{}".format(croppedWidth, croppedHeight) odata = odata.reshape((channels, height, width)) adata = adata.reshape((channels, height, width)) oROI = odata[:, minY:maxY, minX:maxX] aROI = adata[:, minY:maxY, minX:maxX] # Now save the crops to a file: oCropName = o.replace(".avi", "_{}.yuv".format(dims)) aCropName = a.replace(".avi", "_{}.yuv".format(dims)) oROI420 = f.YUV444_2_YUV420(oROI, croppedHeight, croppedWidth) aROI420 = f.YUV444_2_YUV420(aROI, croppedHeight, croppedWidth) f.saveToFile(oROI420, oCropName) f.saveToFile(aROI420, aCropName) print("Made files {} and {}".format(oCropName, aCropName)) if tidyUp: os.remove(oyuvname) os.remove(ayuvname)
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!")
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