infer.py 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import glob
  2. import numpy as np
  3. import threading
  4. import time
  5. import random
  6. import os
  7. import base64
  8. import cv2
  9. import json
  10. import paddlex as pdx
  11. image_name = 'dataset/JPEGImages/6B898244.jpg'
  12. model = pdx.load_model('output/ppyolo_r50vd_dcn/best_model')
  13. img = cv2.imread(image_name)
  14. result = model.predict(img)
  15. keep_results = []
  16. areas = []
  17. f = open('result.txt', 'a')
  18. count = 0
  19. for dt in np.array(result):
  20. cname, bbox, score = dt['category'], dt['bbox'], dt['score']
  21. if score < 0.5:
  22. continue
  23. keep_results.append(dt)
  24. count += 1
  25. f.write(str(dt) + '\n')
  26. f.write('\n')
  27. areas.append(bbox[2] * bbox[3])
  28. areas = np.asarray(areas)
  29. sorted_idxs = np.argsort(-areas).tolist()
  30. keep_results = [keep_results[k]
  31. for k in sorted_idxs] if len(keep_results) > 0 else []
  32. print(keep_results)
  33. print(count)
  34. f.write("the total number is :" + str(int(count)))
  35. f.close()
  36. pdx.det.visualize(
  37. image_name, result, threshold=0.5, save_dir='./output/ppyolo_r50vd_dcn')