Skip to content

Commit 5602b54

Browse files
committed
add remain files of opencv
1 parent 0f11ec4 commit 5602b54

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

Readme.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
26. lane_detection_opencv.py
3434
27. circle_detection_using_hough_transform.py
3535
28. face_detection_opencv.py
36-
29. harris_corner_detection.py
37-
30. shi_tomasi_corner_detection.py
38-
31. background_subtraction_opencv.py
36+
29. eyes_detection_opencv.py
37+
30. harris_corner_detection.py
38+
31. shi_tomasi_corner_detection.py
39+
32. background_subtraction_opencv.py
40+
33. meanshift_object_tracking_opencv.py
3941

4042
## Helpful Documentations⭐:
4143
* OpenCV : https://docs.opencv.org/master/

eyes_detection_opencv.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cv2
2+
import numpy as np
3+
4+
# You have to download both cacasde file from internet
5+
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
6+
eyes_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
7+
8+
# img = cv2.imread('test_images/test.jpg')
9+
cap = cv2.VideoCapture(0)
10+
11+
while cap.isOpened():
12+
_, img = cap.read()
13+
14+
# face detection
15+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
16+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
17+
18+
for (x, y, w, h) in faces:
19+
cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0),3)
20+
21+
# for eyes deteciton
22+
roi_gray = gray[y:y+h , x:x+w]
23+
roi_color = img[y:y+h, x:x+w]
24+
eyes = eyes_cascade.detectMultiScale(roi_gray)
25+
for (ex,ey,ew,eh) in eyes:
26+
cv2.rectangle(roi_color, (ex,ey) , (ex+ew,ey+eh),(0,255,0) , 3)
27+
28+
cv2.imshow('img', img)
29+
if cv2.waitKey(1) & 0xff == ord('q'):
30+
break
31+
32+
cv2.destroyAllWindows()
33+
34+
cap.release()

meanshift_object_tracking_opencv.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
# mean shift
32+
ret ,track_window = cv2.meanShift(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+
# cv2.imshow('dst' , dst)
40+
cv2.imshow('final_image' , final_image)
41+
42+
if cv2.waitKey(30) & 0xFF == ord('q'):
43+
break
44+
45+
cap.release()
46+
cv2.destroyAllWindows()

test_images/slow_traffic_small.mp4

1.92 MB
Binary file not shown.

0 commit comments

Comments
 (0)