sot.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import cv2
  2. mark_color = (150, 250, 250)
  3. mark_thick = 2
  4. bbox_color = (0, 0, 255)
  5. bbox_thick = 2
  6. track_mode = False
  7. height = 0
  8. width = 0
  9. channels = 0
  10. def createTreker():
  11. return cv2.TrackerMIL_create() # метод Multiple Instance Learning
  12. #return cv2.legacy.TrackerMOSSE_create() # Minimum Output Sum of Squared Error
  13. #return cv2.TrackerGOTURN_create() # Neural network
  14. def drawMark():
  15. cv2.line(img, (bbox_x, bbox_y), (int(bbox_x+bbox_w/4), bbox_y), mark_color, mark_thick)
  16. cv2.line(img, (bbox_x+bbox_w, bbox_y), (int(bbox_x+bbox_w-bbox_w/4), bbox_y), mark_color, mark_thick)
  17. cv2.line(img, (bbox_x, bbox_y+bbox_h), (int(bbox_x+bbox_w/4), bbox_y+bbox_h), mark_color, mark_thick)
  18. cv2.line(img, (bbox_x+bbox_w, bbox_y+bbox_h), (int(bbox_x+bbox_w-bbox_w/4), bbox_y+bbox_h), mark_color, mark_thick)
  19. cv2.line(img, (bbox_x, bbox_y), (bbox_x, int(bbox_y+bbox_h/4)), mark_color, mark_thick)
  20. cv2.line(img, (bbox_x+bbox_w, bbox_y), (bbox_x+bbox_w, int(bbox_y+bbox_h/4)), mark_color, mark_thick)
  21. cv2.line(img, (bbox_x, bbox_y+bbox_h), (bbox_x, int(bbox_y+bbox_h-bbox_h/4)), mark_color, mark_thick)
  22. cv2.line(img, (bbox_x+bbox_w, bbox_y+bbox_h), (bbox_x+bbox_w, int(bbox_y+bbox_h-bbox_h/4)), mark_color, mark_thick)
  23. def drawBox():
  24. cv2.rectangle(img, (int(bbox[0]), int(bbox[1])), (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])), bbox_color, bbox_thick, 1)
  25. def doAction():
  26. objX = int(bbox[0] + bbox[2]/2)
  27. objY = int(bbox[1] + bbox[3]/2)
  28. startX = int(width/2)
  29. startY = int(height/2)
  30. cv2.line(img, (startX, startY), (objX, startY), (0,255,0), 2)
  31. cv2.line(img, (startX, startY), (startX, objY), (0,255,0), 2)
  32. tracker = createTreker()
  33. cap = cv2.VideoCapture(0)
  34. #frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  35. #frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  36. success, img = cap.read()
  37. if success:
  38. height, width, channels = img.shape
  39. bbox_w = int(width/5)
  40. bbox_x = int(width/2 - bbox_w/2)
  41. bbox_h = int(height/5)
  42. bbox_y = int(height/2 - bbox_h/2)
  43. bbox = (bbox_x, bbox_y, bbox_w, bbox_h)
  44. while True:
  45. timer = cv2.getTickCount()
  46. success, img = cap.read()
  47. if not success:
  48. break
  49. drawMark()
  50. if track_mode == True:
  51. success, bbox = tracker.update(img)
  52. if success:
  53. drawBox()
  54. doAction()
  55. else:
  56. cv2.putText(img, "Lost", (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
  57. fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
  58. cv2.putText(img, str(int(fps)), (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
  59. cv2.imshow("Tracking", img)
  60. key = cv2.waitKey(1)
  61. if key & 0xff == ord(' '):
  62. track_mode = not track_mode
  63. if track_mode == True:
  64. bbox=(bbox_x, bbox_y, bbox_w, bbox_h)
  65. tracker = createTreker()
  66. tracker.init(img, bbox)
  67. if key & 0xff == ord('q'):
  68. break
  69. cap.release()
  70. cv2.destroyAllWindows()