Go through all the file in destination path:
import os
import sys
def GetFileList(dir, fileList):
newDir = dir
if os.path.isfile(dir):
fileList.append(dir.decode('gbk'))
elif os.path.isdir(dir):
for s in os.listdir(dir):
newDir=os.path.join(dir,s)
GetFileList(newDir, fileList)
return fileList
def MyGetFileList(dir, filelist):
if os.path.isdir(dir):
for s in os.listdir(dir):
if re.match(r"([\s\S]*)\.yuv", s):
filelist.append(os.path.join(dir, s))
return filelist
ValueError: invalid literal for int() with base 10
When read file, we just want to calculate the ASCII value to achieve our goal. Here MAP is very important.
def CalculatePSNRForTwoYUVs(f1, f2, dir1, dir2):
d1 = dir1 + '\\' + f1
d2 = dir2 + '\\' + f2
if (os.path.isfile(d1) and os.path.isfile(d2)):
file1Object = open(d1, 'r')
file2Object = open(d2, 'r')
while True:
chunk1 = file1Object.read(1)
chunk2 = file2Object.read(1)
if chunk1 == '':
break
#print string.atoi(chunk1)
ck1 = map(ord, chunk1)
print ck1[0]
def CalculatePSNRForTwoYUVs(f1, f2, dir1, dir2):
d1 = dir1 + '\\' + f1
d2 = dir2 + '\\' + f2
sumOfPSNR = 0
if (os.path.isfile(d1) and os.path.isfile(d2)):
file1Object = open(d1, 'r')
file2Object = open(d2, 'r')
print d1
print d2
while True:
chunk1 = file1Object.read(1)
chunk2 = file2Object.read(1)
if chunk1 == '':
break
if chunk2 == '':
break
ck1 = map(ord, chunk1)
ck2 = map(ord, chunk2)
while ck1[0] >= 255 or ck1[0] < 0:
chunk1 = file1Object.read(1)
ck1 = map(ord, chunk1)
while ck2[0] >= 255 or ck2[0] < 0:
chunk2 = file2Object.read(1)
ck2 = map(ord, chunk2)
if ck1[0] != ck2[0]:
print ck1[0]," ",ck2[0]
sumOfPSNR += abs(ck1[0] - ck2[0])
print sumOfPSNR
print "---------------------------------------------------------\n"
If we just want to change ‘1’ to int, just string.atoi or string.atol
totalCount = ‘234’
totalPage = int(totalCount)/35
How to calculate PSNR:
Peak Signal to Noise Ratio
其中,MSE是原图像与处理图像之间均方误差。
MAXI:表示图像颜色的最大数值,8位采样点表示为255。
Peak就是指8bits表示法的最大值255。MSE指MeanSquareError,I(角标n)指原始影像第n个pixel值,P(角标n)指经处理后的影像第n个pixel值。PSNR的单位为dB。所以PSNR值越大,就代表失真越少。
Here I give an example for a batch to calculate PSNR of a lot YUV file.
If U need calculate YUV whole plane:
tmpy = (float)(4.0 / pow(10, snr_y / 10));
tmpu = (float)(1.0 / pow(10, snr_u / 10));
tmpv = (float)(1.0 / pow(10, snr_v / 10));
snr_yuv = (float)(10 * log10(6 / ( tmpy + tmpu + tmpv ) ));
#-*-coding:utf-8-*-
'''
Create on 2015/11/16 9:05:59
@author: Chen Yu in RockChip Internship
'''
import os
import sys
import re
import string
import math
# processing folder path
dirGood = r"E:\ErrorConcealment\Sequence\Sequence_Bat_Good"
dirCopy = r"E:\ErrorConcealment\Sequence\Sequence_Bat"
dirFirstBatch = r"E:\ErrorConcealment\Sequence\Sequence_Bat\First Batch"
dirSecondBatch = r"E:\ErrorConcealment\Sequence\Sequence_Bat\Second Batch"
def MyGetFileList(dir, filelist):
if os.path.isdir(dir):
for s in os.listdir(dir):
if re.match(r"([\s\S]*)\.yuv", s):
filelist.append(s)
return filelist
def CalculatePSNRForTwoFolder(fileList1, fileList2, dir1, dir2):
for f1 in fileList1:
for f2 in fileList2:
if f1 == f2:
CalculatePSNRForTwoYUVs(f1, f2, dir1, dir2)
def CalculatePSNRForTwoYUVs(f1, f2, dir1, dir2):
d1 = dir1 + '\\' + f1
d2 = dir2 + '\\' + f2
sumOfPSNR = 0
if (os.path.isfile(d1) and os.path.isfile(d2)):
file1Object = open(d1, 'rb')
file2Object = open(d2, 'rb')
print d1
print d2
while True:
chunk1 = file1Object.read(1)
chunk2 = file2Object.read(1)
'''
if not chunk1:
break
if not chunk2:
break
'''
if chunk1 == '':
print ck1
break
if chunk2 == '':
print ck2
break
ck1 = ord(chunk1)
ck2 = ord(chunk2)
# if ck1 != ck2:
# print ck1," ",ck2, " ", sumOfPSNR
sumOfPSNR += (abs(ck1 - ck2)**2)
file1Object.close()
file2Object.close()
print sumOfPSNR
MSE = math.sqrt(sumOfPSNR)
print MSE
if MSE != 0:
MAXDivMSE = 255 / MSE
if MAXDivMSE > 0:
PSNR = 20 * log10(MAXDivMSE)
print PSNR
print "---------------------------------------------------------\n\n"
def BatchCalculatePSNR():
FileListOfGood = MyGetFileList(dirGood, [])
FileListOfCopy = MyGetFileList(dirCopy, [])
FileListOfFirstBatch = MyGetFileList(dirFirstBatch, [])
FileListOfSecondBatch = MyGetFileList(dirSecondBatch, [])
CalculatePSNRForTwoFolder(FileListOfGood, FileListOfCopy, dirGood, dirCopy)
# CalculatePSNRForTwoFolder(FileListOfGood, FileListOfFirstBatch, dirGood, dirFirstBatch)
# CalculatePSNRForTwoFolder(FileListOfGood, FileListOfSecondBatch, dirGood, dirSecondBatch)
if __name__ == "__main__":
BatchCalculatePSNR()
Some blog I think is helpful to understand file operation:
http://maincoolbo.iteye.com/blog/626655
http://blog.csdn.net/nanjunxiao/article/details/9086079
http://www.sharejs.com/codes/python/4611
http://www.cnblogs.com/ylan2009/articles/2382868.html
http://www.jb51.net/article/47956.htm
http://www.mamicode.com/info-detail-308445.html
Type Tranformation
http://www.xuebuyuan.com/1884369.html
http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html
Some Mistakes
Maybe U find that using read(1) is too slow and U can read all the file one time.
if __name__ == "__main__":
d1 = "E:/ErrorConcealment/Sequence/Sequence_Bat/Basketball_1920x1080_frame2_confirm.264.yuv"
d2 = "E:/ErrorConcealment/Sequence/Sequence_Bat_Good/Basketball_1920x1080_frame2_confirm.264.yuv"
result = re.findall("([\s\S]*)\_(\d+)x(\d+)\_([\s\S]*)",d1)
for line in result:
width = string.atoi(line[1])
height = string.atoi(line[2])
Area = width * height
chunkSize = (Area * 3) >> 1 # 420
frameNum = 0
MSE = 0
MAXDivMSE = 0
PSNR = 0
if (os.path.isfile(d1) and os.path.isfile(d2)):
file1Object = open(d1, 'rb')
file2Object = open(d2, 'rb')
while True:
chunk1 = file1Object.read(chunkSize)
chunk2 = file2Object.read(chunkSize)
if not chunk1:
break
if not chunk2:
break
sumOfPSNR = 0
l1 = list(chunk1)
l1 = list(chunk2)
for i in range (0, Area):
ck1 = ord(l1[i])
ck2 = ord(l2[i])
sumOfMSE += (abs(ck1 - ck2)**2)
MSE = math.sqrt(sumOfPSNR)
if MSE != 0:
MAXDivMSE = 255 / MSE
if MAXDivMSE > 0:
PSNR = 20 * math.log10(MAXDivMSE)
frameNum++
file1Object.close()
file2Object.close()
print PSNR
print "---------------------------------------------------------\n\n"
#win32api.ShellExecute(0, 'open', 'F:/My_Tool/evalvid-2.7-w32/psnr.exe', '1920 1080 420 Basketball_1920x1080_frame2_confirm.264.yuv Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr.txt',"", 0)
# BatchCalculatePSNR()
# os.system('F:/My_Tool/evalvid-2.7-w32/psnr.exe 1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr.txt')
# win32api.ShellExecute(0, 'open', 'F:/My_Tool/evalvid-2.7-w32/psnr.exe','1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv >F:/My_Tool/evalvid-2.7-w32/ref_psnr2.txt','',1)
'''
cmda = 'F:/My_Tool/evalvid-2.7-w32/psnr.exe 1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr1.txt'
p = Popen(cmda, shell = True, stdout = PIPE, stderr = PIPE)
output = p.communicate()[0] #标准输出
error = p.communicate()[1] #标准错误
#p.terminnate() #杀死进程
cmda = 'F:/My_Tool/evalvid-2.7-w32/psnr.exe 1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr2.txt'
p2 = Popen(cmda, shell = True, stdout = PIPE, stderr = PIPE)
'''
'''
handle = win32process.CreateProcess('F:/My_Tool/evalvid-2.7-w32/psnr.exe','1920 1080 420 F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm.264.yuv F:/My_Tool/evalvid-2.7-w32/Basketball_1920x1080_frame2_confirm_copy.yuv > F:/My_Tool/evalvid-2.7-w32/ref_psnr2.txt', None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None , win32process.STARTUPINFO())
win32event.WaitForSingleObject(handle[0], -1) #win32process.TerminateProcess(handle[0],0)
'''