def encodeAWholeFolderOfImagesAsSingleH264Frames(myDir): fileList = createFileList(myDir, takeAll=True, format='.tif') quants = [0, 7, 14, 21, 28, 35, 42, 49] #quants = [0,49] for quant in quants: # make a directory dirName = "quant_{}".format(quant) dirName = os.path.join(myDir, dirName) if not os.path.exists(dirName): os.makedirs(dirName) for entry in fileList: filename = entry[0] baseFileName = os.path.basename(filename) baseFileName, ext = os.path.splitext(baseFileName) baseFileName = "{}/{}".format(dirName, baseFileName) width, height, yuvData = functions.convertTifToYUV420(filename) tempYUVFilename = "myyuv.yuv" functions.saveToFile(yuvData, tempYUVFilename) print("width: {} height: {} filename:{}".format( width, height, filename)) h264Filename = "{}_s{}x{}_q{}.h264".format(baseFileName, width, height, quant) compYuvFilename = "{}_s{}x{}_q{}.yuv".format( baseFileName, width, height, quant) isize, psize, bsize = functions.compressFile( x264, tempYUVFilename, width, height, quant, h264Filename, compYuvFilename)
def encodeAWholeFolderAsH264(myDir, takeAll=False, intraOnly=False, deblock=False, encodedFolder=""): fileList = createFileList(myDir, takeAll=takeAll) print("The file list:") print(fileList) for quant in quants: # make a directory dirName = "quant_{}".format(quant) dirName = os.path.join(myDir, dirName) if encodedFolder == "": if not os.path.exists(dirName): os.makedirs(dirName) for entry in fileList: filename = entry[0] baseFileName = os.path.basename(filename) baseFileName, ext = os.path.splitext(baseFileName) baseFileName = "{}/{}".format(dirName, baseFileName) #print("The baseFileName is: {}".format(baseFileName)) width = entry[1] height = entry[2] print("width: {} height: {} filename:{}".format( entry[1], entry[2], entry[0])) h264Filename = "{}_q{}.h264".format(baseFileName, quant) #compYuvFilename = "{}_q{}_intra{}.yuv".format(baseFileName, quant) compYuvFilename = "{}_q{}.yuv".format(baseFileName, quant) if encodedFolder != "": print("Storing in {}".format(encodedFolder)) d, n = os.path.split(filename) n, e = os.path.splitext(n) h264Filename = "{}_q{}.h264".format(n, quant) h264Filename = os.path.join(encodedFolder, h264Filename) compYuvFilename = "{}_q{}.yuv".format(n, quant) compYuvFilename = os.path.join(encodedFolder, compYuvFilename) #Do the compression isize, psize, bsize = functions.compressFile(x264, filename, width, height, quant, h264Filename, compYuvFilename, intraOnly=intraOnly, deblock=deblock)
yuvByteArray = bytearray(datayuv) with open("diff.yuv", "wb") as yuvFile: yuvFile.write(yuvByteArray) savedFrameNo = 23 # now save off a single frame yuvview.yuvFileTobmpFile(filename_output, width, height, savedFrameNo, "i420", "fig_uncomp_munge.bmp") yuvview.yuvFileTobmpFile(filename_output_ori, width, height, savedFrameNo, "i420", "fig_uncomp_ori.bmp") yuvview.yuvFileTobmpFile(filename_mask, width, height, savedFrameNo, "i420", "fig_uncomp_mask.bmp", bw=True) yuvview.yuvFileTobmpFile("diff.yuv", width, height, savedFrameNo, "i420", "fig_uncomp_diff.bmp", bw=True) #compress the munged file qp=384000 filename_outcomp = "c_munge.264" filename_outdecomp = "c_munge_qp{}.yuv".format(qp) functions.compressFile("x264", filename_output, width, height, qp, filename_outcomp, filename_outdecomp) # and the original file filename_outcomp_ori = "c_ori.264" filename_outdecomp_ori = "c_ori_qp{}.yuv".format(qp) functions.compressFile("x264", filename_output_ori, width, height, qp, filename_outcomp_ori, filename_outdecomp_ori) # and the mask filename_outcomp_mask = "c_mask.264" filename_outdecomp_mask = "c_mask_qp{}.yuv".format(qp) functions.compressFile("x264", filename_mask, width, height, qp, filename_outcomp_mask, filename_outdecomp_mask) with open(filename_outdecomp, "rb") as f: munge_c = np.fromfile(f, 'u1') munge_c = munge_c.reshape((numFrames, frameSize)) with open(filename_outdecomp_ori, "rb") as f: a_mask_source_c = np.fromfile(f, 'u1')
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!")