Ask Your Question
0

KCF Tracking_ Not responding_Frame

asked 2019-02-28 04:55:09 -0500

Abdu gravatar image

Hi, I tried to use the following code:


 import numpy as np
import cv2
import sys
cap = cv2.VideoCapture('C:\Sample3.mp4')

def rescale_frame(frame, percent=30):

    width = int(frame.shape[1] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

while True:
    ret ,frame = cap.read()
    if not ret:
        break
    frame25 = rescale_frame(frame, percent=25)
    #cv2.imshow('frame25',frame25)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
# Foot Detection
## Define an initial bounding box
bbox = (100, 23, 86, 200)
# Set up tracker.
tracker = cv2.TrackerKCF_create()
tracker_type = 'KCF'
#
#  Uncomment the line below to select a different bounding box
bbox = cv2.selectROI(frame25, False)
# # Initialize tracker with first frame and bounding box
ok = tracker.init(frame25, bbox)

while True:
    # Read a new frame
    ok, frame = cap.read()
    if not ok:
        break

    # Start timer
    timer = cv2.getTickCount()

    # Update tracker
    ok, bbox = tracker.update(frame)

    # Calculate Frames per second (FPS)
    fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);

    # Draw bounding box
    if ok:
        # Tracking success
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
    else:
        # Tracking failure
        cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

    # Display tracker type on frame

    cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);

    # Display FPS on frame
    cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);

    # Display result
    cv2.imshow("Tracking", frame)

    # Exit if ESC pressed
    k = cv2.waitKey(1) & 0xff
    if k == 27: break
 

However, I have got the following frame:

image description without any further result. Could you please assist me with it please? Thank you in advance.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-28 06:39:47 -0500

supra56 gravatar image

updated 2019-02-28 08:57:11 -0500

Remove this release and destroyAllWindowws and put at the end of code. Like this:

# Exit if ESC pressed
    k = cv2.waitKey(1) & 0xff
    if k == 27: break

cap.release()
cv2.destroyAllWindows()

You cannot used both whle condition block. But I haven't test it yet

import numpy as np
import cv2
import sys

cap = cv2.VideoCapture('C:\Sample3.mp4')

def rescale_frame(frame, percent=30):
    width = int(frame.shape[1] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

while True:
    ret ,frame = cap.read()
    if not ret:
        break
    frame25 = rescale_frame(frame, percent=25)
    # Foot Detection
    ## Define an initial bounding box
    bbox = (100, 23, 86, 200)
    # Set up tracker.
    tracker = cv2.TrackerKCF_create()
    tracker_type = 'KCF'

    #  Uncomment the line below to select a different bounding box
    bbox = cv2.selectROI(frame25, False)
    # # Initialize tracker with first frame and bounding box
    ok = tracker.init(frame25, bbox)

    # Start timer
    timer = cv2.getTickCount()

    # Update tracker
    ok, bbox = tracker.update(frame)

    # Calculate Frames per second (FPS)
    fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);

    # Draw bounding box
    if ok:
        # Tracking success
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
    else:
        # Tracking failure
        cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

    # Display tracker type on frame
    cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);

    # Display FPS on frame
    cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);

    # Display result
    cv2.imshow("Tracking", frame)

    # Exit if ESC pressed
    k = cv2.waitKey(1) & 0xff
    if k == 27: break

cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

1

@supra56 thank you for your answer. But, after selecting the ROI,I would like to get another frame of Tracking like in this link: https://www.youtube.com/watch?v=0uT3q...

Abdu gravatar imageAbdu ( 2019-02-28 06:48:10 -0500 )edit

OIC. Take a look at ROI

supra56 gravatar imagesupra56 ( 2019-02-28 07:14:28 -0500 )edit

The ROI is correct in my code. Is there anything to change in the code in order to get similar result as: https://www.youtube.com/watch?v=0uT3q...

Abdu gravatar imageAbdu ( 2019-03-01 04:19:59 -0500 )edit

The issue is that there is no displaying of tracker on frame.

Abdu gravatar imageAbdu ( 2019-03-01 04:28:52 -0500 )edit

I haven't test your code yet. I will attempt by this weekend. You will have to used mouseevent to drawn rectangle. As for me, I used picamera.

supra56 gravatar imagesupra56 ( 2019-03-01 08:25:45 -0500 )edit

Thank you so much for your help. I am looking to hearing from you soon.

Abdu gravatar imageAbdu ( 2019-03-01 08:42:27 -0500 )edit

@supra56 could you please let me know if you tried my code and if there is any possibility to help me to choose an ROI from different frame than the first one?

Abdu gravatar imageAbdu ( 2019-03-04 04:00:13 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-02-28 04:55:09 -0500

Seen: 37 times

Last updated: Feb 28