You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_with_labels.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import cv2
  2. import time
  3. import argparse
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. parser = argparse.ArgumentParser(description='Test yolo data.')
  7. parser.add_argument('-i', help='Image', dest='img', required=True)
  8. parser.add_argument('-t', help='Yolo text file', dest='txt',required=True)
  9. parser.add_argument('-l', help='labels txt file', dest='label',required=True)
  10. args = parser.parse_args()
  11. frame = cv2.imread(args.img)
  12. cv2.namedWindow("f", cv2.WINDOW_NORMAL);
  13. coordinates = []
  14. with open(args.txt, "r") as lines:
  15. for line in lines:
  16. hT, wT, cT = frame.shape
  17. coordinates = line.rstrip('\n').split(' ')
  18. idx = coordinates[0]
  19. x1, y1,w2,h2 = float(coordinates[1]), float(coordinates[2]), float(coordinates[3]), float(coordinates[4])
  20. print(x1,y1,w2,h2)
  21. cmap = plt.get_cmap('tab20b')
  22. colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]
  23. color = colors[int(idx) % len(colors)]
  24. color = [i * 255 for i in color]
  25. w, h = int(w2 * wT), int(h2 * hT)
  26. x, y = int((x1 * wT) - w / 2), int((y1 * hT) - h / 2)
  27. with open(args.label, "r") as labels:
  28. classNames = labels.read().rstrip('\n').split('\n')
  29. # # 0 1 2 3 x1 - центр по x в процентах y1- центр по y в процентах w2- центр по w в процентах h2- центр по h в процентах
  30. cv2.rectangle(frame, (x,y), (x+w, y+h), color,3)
  31. print(len(classNames[1]))
  32. cv2.rectangle(frame, (x,y-30), (x+len(classNames[int(idx)]*15), y), color,-1)
  33. cv2.putText(frame,str(classNames[int(idx)]), (x+4, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
  34. cv2.imshow("f", frame)
  35. cv2.waitKey(0)