test.py 1.3 KB

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