Object Detection and Tracking Using Opencv in Python: March 2020
Object Detection and Tracking Using Opencv in Python: March 2020
net/publication/343282935
CITATIONS READS
0 7,059
2 authors:
Some of the authors of this publication are also working on these related projects:
Forecasting of Tourist Inflow for Infrastructure and Logistics Planning View project
All content following this page was uploaded by Sidra Mehtab on 29 July 2020.
By
Sidra Mehtab
(Reg. No: 182341810028)
Under the Supervision of Prof. Jaydip Sen
• We now define the main function and start by initializing the video
“capture” object.
• Then, we define the actual value of the “scaling factor” to be used for
resizing the captured frames from the video.
• We, then, use a “while loop” to iterate indefinitely until the user interrupts
the program by hitting the ESC key to terminate the program.
• We convert each frame grabbed by the capturing unit to HSV color
space using the inbuilt library function in OpenCV. Then, we define the
approximate HSV color region for the color of human skin.
• We use the HSV threshold values to create the “mask”.
Tracking Objects Using Colorspace (contd…)
• Next, we compute the bitwise-AND between the pixels in the “mask” and
those in the original image.
• We run a median blurring to smoothen the image.
• Finally, we display the input and output frames of the image side by side.
if __name__=='__main__':
cap = cv2.VideoCapture('bihag_flute.mp4’)
scaling_factor = 0.5
while True:
frame = get_frame(cap, scaling_factor)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower = np.array([0, 30, 50])
upper = np.array([30, 150, 255])
mask = cv2.inRange(hsv, lower, upper)
img_bitwise_and = cv2.bitwise_and(frame, frame, mask=mask)
img_median_blurred = cv2.medianBlur(img_bitwise_and, 5)
cv2.imshow('Input', frame)
cv2.imshow('Output', img_median_blurred)
c = cv2.waitKey(5)
if c == 27:
break
cv2.destroyAllWindows()
Results : Tracking by Colorspace
if __name__=='__main__’:
cap = cv2.VideoCapture('bihag_flute.mp4')
bg_subtractor = cv2.createBackgroundSubtractorMOG2()
history = 75
learning_rate = 1.0/history
while True:
frame = get_frame(cap, 0.5)
mask = bg_subtractor.apply(frame, learningRate=learning_rate)
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
cv2.imshow('Input', frame)
cv2.imshow('Output', mask & frame)
c = cv2.waitKey(10)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()
Results : Tracking by Background Separation
if __name__ == '__main__’:
# Start the tracker
start_tracking()
# Close all the windows
cv2.destroyAllWindows()
Results : Optical Flow-Based Tracking
• We initialize the video “capture” object and define the “scaling factor” using
the following:
cap = cv2.VideoCapture('bihag_nazrul_geeti.mp4’)
ds_factor = 0.5
• Next, we use a “while loop” to iterate indefinitely until the user presses the
ESC key or the input video completes its playing. We convert the image
into a “grayscale” image.
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
• Next, we run the face detector on the grayscale image using the following
line with a “scaleFactor” value of 1.3 (default value) and the “minNeighbors”
value of 15.
faces = face_cascade.detectMultiScale(gray, 1.3, 15)
Face Detection and Tracking (contd…)
• The first parameter “scaleFactor” of the detectMultiScale function determines a
trade-off between detection accuracy and speed of detection. The detection
window starts at size “minSize”, and after testing all windows of that size, the
window is scaled up the “scaleFactor”, and re-tested, and so on until the window
reaches or exceeds the “maxSize”. If the “scaleFactor” is large, (e.g., 2.0), there
will be fewer steps, so detection will be faster, but we may miss objects whose
size is between two tested scales. We usually don’t change the default value,
which is 1.3.
• Next, we iterate through the detected faces and draw rectangles around them, so
that the faces are clearly identified and the detected faces are displayed.
eye_cascade = cv2.CascadeClassifier('haar_cascade_files/haarcascade_eye.xml')
• Next, we initialize the video “capture” object and define the scaling factor.
cap = cv2.VideoCapture('bihag_nazrul_geeti.mp4’)
ds_factor = 0.5
• Next, we use a “while loop” to iterate indefinitely until the user presses the
ESC key or the input video completes its playing. We convert the image into
a “grayscale” image.
while True:
_, frame = cap.read()
frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor,
interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
• Next, we run the face detector on the “grayscale” image using the following
line with “scaleFactor” value of 1.3 (default value) and the minNeighbors
value of 20 in an outer “for loop”.
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
Eyes Detection in a Face (contd…)
• In an inner “for loop”, we detected the eyes for each “face”
detected in the outer “for loop”.
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),4)
• We initialize the video “capture” object and define the “scaling factor”
using the following:
cap = cv2.VideoCapture('bihag_nazrul_geeti.mp4’)
ds_factor = 0.7
• Next, we, use a “while loop” to iterate indefinitely until the user presses the
ESC key or the input video completes its playing. We convert the image
into a “grayscale” image.
while True:
_, frame = cap.read()
frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor,
interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
• Next, we run the nose detector on the grayscale image using the following
line with a “scaleFactor” value of 1.3 (default value) and the minNeighbors
value of 8.
nose_rects = nose_cascade.detectMultiScale(gray, 1.3, 8)
Nose Detection in Face (contd…)
• Next, we iterate through the detected noses and draw rectangles around them,
so that the noses are clearly identified.