Skip to content

Commit 68cb76a

Browse files
committed
add object tracking file
1 parent 5602b54 commit 68cb76a

4 files changed

+69
-8
lines changed

‎Readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
31. shi_tomasi_corner_detection.py
3939
32. background_subtraction_opencv.py
4040
33. meanshift_object_tracking_opencv.py
41+
34. camshift_object_tracking_opencv.py
42+
43+
## Cascade Information⭐:
44+
#### Download Following Resources for Corosponding Tutorials.
45+
46+
* Face-detection :- https://.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
47+
48+
* Eyes Detection :- https://.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml
49+
50+
Other Cascade :- https://.com/opencv/opencv/tree/master/data/haarcascades
4151

4252
## Helpful Documentations⭐:
4353
* OpenCV : https://docs.opencv.org/master/

‎camshift_object_tracking_opencv.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import cv2
2+
import numpy as np
3+
4+
cap = cv2.VideoCapture('test_images/slow_traffic_small.mp4')
5+
6+
# read first frame
7+
ret, frame = cap.read()
8+
9+
# initial location
10+
x, y, width, height = 300, 200, 100, 50
11+
tracked_window = (x,y,width,height)
12+
13+
# ROI
14+
roi = frame[y:y+height , x:x+width]
15+
hsv_roi = cv2.cvtColor(roi , cv2.COLOR_BGR2HSV)
16+
mask = cv2.inRange(hsv_roi , np.array((0.,60.,32.)) , np.array((180.,255.,255.)))
17+
roi_hist = cv2.calcHist([hsv_roi] , [0] ,mask ,[180],[0,180] )
18+
cv2.normalize(roi_hist , roi_hist , 0,255, cv2.NORM_MINMAX)
19+
20+
# setup termination criteria
21+
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT , 10, 1)
22+
# cv2.imshow('roi' , roi)
23+
24+
while True:
25+
ret, frame = cap.read()
26+
if ret == True:
27+
28+
hsv = cv2.cvtColor(frame , cv2.COLOR_BGR2HSV)
29+
dst = cv2.calcBackProject([hsv] , [0] , roi_hist , [0,180] , 1)
30+
31+
# Cam shift
32+
ret ,track_window = cv2.CamShift(dst , tracked_window , term_crit)
33+
34+
# # draw it on image
35+
# x,y,w,h = track_window
36+
37+
# final_image = cv2.rectangle(frame , (x,y) , (x+w,y+h) , (0,0,255) , 3)
38+
39+
pts = cv2.boxPoints(ret)
40+
pts = np.int0(pts)
41+
final_image = cv2.polylines(frame , [pts] , True , (255,0,0) , 3)
42+
43+
# cv2.imshow('dst' , dst)
44+
cv2.imshow('final_image' , final_image)
45+
46+
if cv2.waitKey(30) & 0xFF == ord('q'):
47+
break
48+
49+
cap.release()
50+
cv2.destroyAllWindows()

‎eyes_detection_opencv.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import numpy as np
33

44
# You have to download both cacasde file from internet
5+
# Download link is in readme.md file of this repository.
6+
57
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
68
eyes_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
79

‎face_detection_opencv.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@
44
#image face detection
55
# face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
66
# img = cv2.imread('test_images/test.jpg')
7-
#
8-
#
7+
98
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
109
# faces = face_cascade.detectMultiScale(gray, 1.1, 4)
11-
#
10+
1211
# for (x, y, w, h) in faces:
1312
# cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0),3)
14-
#
15-
#
13+
1614
# cv2.imshow('img', img)
1715
# cv2.waitKey(0)
18-
#
19-
# cv2.destroyAllWindows()
2016

17+
# cv2.destroyAllWindows()
2118

2219

2320
# video face detection
2421

25-
## You have to download haarcascade_frontalface_default from internet
22+
## You have to download cascade file from internet
23+
## Download link is in readme.md file of this repository
2624

2725
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
26+
2827
# img = cv2.imread('test_images/test.jpg')
2928
cap = cv2.VideoCapture(0)
3029

0 commit comments

Comments
 (0)