diff options
| author | zhang <zch921005@126.com> | 2022-05-21 14:23:49 +0800 |
|---|---|---|
| committer | zhang <zch921005@126.com> | 2022-05-21 14:23:49 +0800 |
| commit | 678fab50280b647d95213a9695d07c49542696f2 (patch) | |
| tree | 74ca60de14311a8a2ff58dbf82d9b7574c9cd3ef /cv/tracker | |
| parent | 2180c68999eb8dc0c7bcec015b2703f5b8b20223 (diff) | |
0521
Diffstat (limited to 'cv/tracker')
| -rw-r--r-- | cv/tracker/object_tracker.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/cv/tracker/object_tracker.py b/cv/tracker/object_tracker.py new file mode 100644 index 0000000..315ffa3 --- /dev/null +++ b/cv/tracker/object_tracker.py @@ -0,0 +1,35 @@ +import cv2 + +cap = cv2.VideoCapture(0) + +tracker = cv2.TrackerGOTURN_create() +success, img = cap.read() + +# select a bounding box ( ROI ) +bbox = cv2.selectROI("Tracking", img, False) +tracker.init(img, bbox) + + +def drawBox(img, bbox): + x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) + cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 3, 1) + cv2.putText(img, "Tracking", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + + +while True: + timer = cv2.getTickCount() + success, img = cap.read() + + success, bbox = tracker.update(img) + + if success: + drawBox(img, bbox) + else: + cv2.putText(img, "Loss", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) + + fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) + cv2.putText(img, str(int(fps)), (75, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) + cv2.imshow("Tracking", img) + + if cv2.waitKey(1) & 0xff == ord('q'): + break
\ No newline at end of file |
