-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject tracking.py
More file actions
37 lines (27 loc) · 1.03 KB
/
object tracking.py
File metadata and controls
37 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import cv2 as cv
import numpy as np
cam = cv.VideoCapture(0)
lower_orange = np.array([5,100,100])
upper_orange = np.array([15,255,255])
while(True):
ret,frame = cam.read()
frame = cv.flip(frame,1)
#Smoothen the image
image_smooth = cv.GaussianBlur(frame,(7,7),0)
#Threshold the image for orange color
image_hsv = cv.cvtColor(image_smooth,cv.COLOR_BGR2HSV)
image_threshold = cv.inRange(image_hsv,lower_orange,upper_orange)
# Find contours
contours,hierarchy = cv.findContours(image_threshold,cv.RETR_TREE,cv.CHAIN_APPROX_NONE)
# Find the index of the largest contour
if(len(contours)!=0):
area = [cv.contourArea(c) for c in contours]
max_index = np.argmax(area)
cnt = contours[max_index]
x,y,w,h = cv.boundingRect(cnt)
cv.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
cv.imshow('Frame',frame)
if cv.waitKey(10) == 27:
break
cam.release()
cv.destroyAllWindows()