Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. #Local do video: https://pysource.com/wp-content/uploads/2018/04/mouthwash.avi
  2.  
  3. import cv2
  4. import numpy as np
  5.  
  6. video = cv2.VideoCapture("c:\mouthwash.avi")
  7.  
  8. _, first_frame = video.read()
  9. x = 300
  10. y = 305
  11. width = 100
  12. height = 115
  13. roi = first_frame[y: y + height, x: x + width]
  14. hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
  15. roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180])
  16. roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
  17.  
  18. term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
  19.  
  20. while True:
  21.     _, frame = video.read()
  22.     hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  23.     mask = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
  24.  
  25.     _, track_window = cv2.meanShift(mask, (x, y, width, height), term_criteria)
  26.     x, y, w, h = track_window
  27.     cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  28.  
  29.     cv2.imshow("Mask", mask)
  30.     cv2.imshow("Frame", frame)
  31.  
  32.     key = cv2.waitKey(60)
  33.     if key == 27:
  34.         break
  35.  
  36. video.release()
  37. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement