文章目录
- 数据简介
- 开始实验
- 小波分解
- 得出结果
- 结果分析
- 误差分析
- 参考文献
数据简介
各找一篇中文,日文,韩文,英文,俄文较长的学术论文。将论文转化为JPG格式。拆分每张JPG生成更多小的JPG。最终获得很多5个不同语言的JPG并且自带标签。数据链接:提取码8848。
将PDF转化为JPG。
import aspose.words as aw
for i in range(1,6):doc=aw.Document(f"data/{i}/{i}.pdf")for page in range(0,doc.page_count):extractedPage=doc.extract_pages(page,1)extractedPage.save(f"dataset/{i}/{page+1}.jpg")
确认所有JPG大小是否一样。结果为假。
from PIL import Image
import os
sizes=[]
for i in range(1,6):for filename in os.listdir(f"dataset/{i}"):if filename.endswith(".jpg"):with Image.open(os.path.join(f"dataset/{i}",filename)) as img:sizes.append(img.size)
flag=True
for i in sizes:if i!=sizes[0]:flag=False;break
print(flag)
初步裁切JPG取正中间的400*800个像素点(因为所有JPG的大小都大于400*800)。
from PIL import Image
import os
sizes=[]
for i in range(1,6):for filename in os.listdir(f"dataset/{i}"):if filename.endswith(".jpg"):with Image.open(os.path.join(f"dataset/{i}",filename)) as img:width,height=img.sizeleft=(width-400)/2top=(height-800)/2right=(width+400)/2bottom=(height+800)/2copped_img=img.crop((left,top,right,bottom))copped_img.save(f"dataset_new/{i}/{filename}")
拆分大小为400*800的JPG为32张100*100的JPG。
from PIL import Image
import os
sizes=[]
for i in range(1,6):for filename in os.listdir(f"dataset_new/{i}"):if filename.endswith(".jpg"):with Image.open(os.path.join(f"dataset_new/{i}",filename)) as img:for x in range(0,400,100):for y in range(0,800,100):box=(x,y,x+100,y+100)tile=img.crop(box)tile.save(f"dataset_last_temp/{i}/{filename[:-4]}"+f"_{x//100}{y//100}"+".jpg")
人为地手动删除一些没有文字地的JPG,保存在dataset_last中。
展示其中一些数据:从上往下依次是中、日、韩、英、俄。
开始实验
小波分解
为了方便展示结果,对LL2,LH2,HL2,HH2,LH1,HL1,HH1进行了裁剪。实际实验中没有进行裁剪。为什么这么分解看参考文献,我就不再过多赘述了。
from PIL import Image
import os
import numpy as np
import pywt
import matplotlib.pyplot as plt
def fc(LL,LH,HL,HH,x):LL=LL[:x,:x]LH=LH[:x,:x]HL=HL[:x,:x]HH=HH[:x,:x]image=np.zeros((LL.shape[0]+LH.shape[0],LL.shape[1]+HL.shape[1]))image[:LL.shape[0],:LL.shape[1]]=LLimage[LL.shape[0]:,:LL.shape[1]]=LHimage[:LL.shape[0],LL.shape[1]:]=HLimage[LL.shape[0]:,LL.shape[1]:]=HHreturn image
for i in range(1,6):for filename in os.listdir(f"dataset_last/{i}"):if filename.endswith(".jpg"):with Image.open(os.path.join(f"dataset_last/{i}",filename)) as img:img=img.convert('L')coeffs1=pywt.dwt2(img,'db4')LL1,(LH1,HL1,HH1)=coeffs1coeffs2=pywt.dwt2(LL1,'db4')LL2,(LH2,HL2,HH2)=coeffs2image=fc(fc(LL2,LH2,HL2,HH2,25),LH1,HL1,HH1,50)image=Image.fromarray(image.astype('uint8'))image.save(f"temp/{i}/{filename}")
展示其中一些结果:从上往下依次是中、日、韩、英、俄。
得出结果
为什么这么做看参考文献,我就不再过多赘述了。
from PIL import Image
import os
import numpy as np
import pywt
def fc(matrix):count=0for i in matrix:for j in i:count+=j**2return count/(matrix.shape[0]*matrix.shape[1])
def metric1(LH,HL,HH):return [fc(LH),fc(HL),fc(HH)]
def metric2(LH,HL,HH):x=metric1(LH,HL,HH)a,b,c=x[0],x[1],x[2]d=a+b+creturn [a/d,b/d,c/d]
lt1=[[] for _ in range(5)]
lt2=[[] for _ in range(5)]
for i in range(1,6):for filename in os.listdir(f"dataset_last/{i}"):if filename.endswith(".jpg"):with Image.open(os.path.join(f"dataset_last/{i}",filename)) as img:img=img.convert('L')coeffs1=pywt.dwt2(img,'db4')LL1,(LH1,HL1,HH1)=coeffs1coeffs2=pywt.dwt2(LL1,'db4')LL2,(LH2,HL2,HH2)=coeffs2lt1[i-1].append([LH1,HL1,HH1])lt2[i-1].append([LH2,HL2,HH2])
metrics11=[[metric1(_[0],_[1],_[2]) for _ in lt1[i]] for i in range(5)]
metrics12=[[metric2(_[0],_[1],_[2]) for _ in lt1[i]] for i in range(5)]
mean11=[np.mean(metrics11[i],axis=0) for i in range(5)]
mean12=[np.mean(metrics12[i],axis=0) for i in range(5)]
var11=[np.var(metrics11[i],axis=0) for i in range(5)]
var12=[np.var(metrics12[i],axis=0) for i in range(5)]
metrics21=[[metric1(_[0],_[1],_[2]) for _ in lt2[i]] for i in range(5)]
metrics22=[[metric2(_[0],_[1],_[2]) for _ in lt2[i]] for i in range(5)]
mean21=[np.mean(metrics21[i],axis=0) for i in range(5)]
mean22=[np.mean(metrics22[i],axis=0) for i in range(5)]
var21=[np.var(metrics21[i],axis=0) for i in range(5)]
var22=[np.var(metrics22[i],axis=0) for i in range(5)]
zd={1:"中文",2:"日文",3:"韩文",4:"英文",5:"俄文"}
print(f"{'1次分解-DEMW:':<14}",end=" ")
for i in range(5):count=0for j in metrics11[i]:d=[sum((np.array(j)-_)**2) for _ in mean11]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics11[i])*10000)/100),end=" ")
print()
print(f"{'1次分解-DPMW:':<14}",end=" ")
for i in range(5):count=0for j in metrics12[i]:d=[sum((np.array(j)-_)**2) for _ in mean12]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics12[i])*10000)/100),end=" ")
print()
print(f"{'1次分解-DEMWV:':<14}",end=" ")
for i in range(5):count=0for j in metrics11[i]:d=[sum(((np.array(j)-mean11[k])**2)/(var11[k]**2)) for k in range(5)]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics11[i])*10000)/100),end=" ")
print()
print(f"{'1次分解-DPMWV:':<14}",end=" ")
for i in range(5):count=0for j in metrics12[i]:d=[sum(((np.array(j)-mean12[k])**2)/(var12[k]**2)) for k in range(5)]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics12[i])*10000)/100),end=" ")
print()
print(f"{'2次分解-DEMW:':<14}",end=" ")
for i in range(5):count=0for j in metrics21[i]:d=[sum((np.array(j)-_)**2) for _ in mean21]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics21[i])*10000)/100),end=" ")
print()
print(f"{'2次分解-DPMW:':<14}",end=" ")
for i in range(5):count=0for j in metrics22[i]:d=[sum((np.array(j)-_)**2) for _ in mean22]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics22[i])*10000)/100),end=" ")
print()
print(f"{'2次分解-DEMWV:':<14}",end=" ")
for i in range(5):count=0for j in metrics21[i]:d=[sum(((np.array(j)-mean21[k])**2)/(var21[k]**2)) for k in range(5)]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics21[i])*10000)/100),end=" ")
print()
print(f"{'2次分解-DPMWV:':<14}",end=" ")
for i in range(5):count=0for j in metrics22[i]:d=[sum(((np.array(j)-mean22[k])**2)/(var22[k]**2)) for k in range(5)]if np.argmin(d)==i:count+=1print(zd[i+1],end="")print(" :{:06.2f}%".format(int(count/len(metrics22[i])*10000)/100),end=" ")
print()
结果分析
这是一个5分类任务,乱猜猜中的概率为20%。根据上述实验结果,我们能够保证至少有一种判断方法判断一种语言正确的概率大于80%(除了英语)。大胆猜测英语判断效果不好的原因是我找的不同语言的论文中或多或少都包括了英文。
误差分析
1.数据数量不足:每个语言我只找了一篇论文,显然是不够的。2.数据质量欠佳:论文是我从知网上随便扒的(有如下缺点:各种水印,奇奇怪怪的论文插图,大面积的空白等)。
参考文献
基于多尺度小波纹理分析的文字种类自动识别。