import paddlex as pdx from PIL import Image def transparence2white(img): # img=img.convert('RGBA') # 此步骤是将图像转为灰度(RGBA表示4x8位像素,带透明度掩模的真彩色;CMYK为4x8位像素,分色等),可以省略 sp=img.size width=sp[0] height=sp[1] print(sp) for yh in range(height): for xw in range(width): dot=(xw,yh) color_d=img.getpixel(dot) # 与cv2不同的是,这里需要用getpixel方法来获取维度数据 if(color_d[3]==0): color_d=(255,255,255,255) img.putpixel(dot,color_d) # 赋值的方法是通过putpixel return img handwriting_jpg = 'data/test/test_deal.png'; img=Image.open('data/test/test.png') img=transparence2white(img) # 将图片传入,改变背景色后,返回 img=img.resize((64,64)) img.save(handwriting_jpg) # 保存图片 test1_jpg = 'data/test/test1.png' test2_jpg = 'data/test/test2.png' test3_jpg = 'data/test/test3.jpg' # model = pdx.load_model('output/mobilenetv3/export') model = pdx.load_model('output/resnet/export') result = model.predict(handwriting_jpg,topk=8) print("Predict Result: ", result) result2 = model.predict(test2_jpg, topk=3) print("Predict Result: ", result2) result3 = model.predict(test3_jpg, topk=3) print("Predict Result: ", result3)