Skip to content

Commit f53a2b6

Browse files
committed
object_tracking added
1 parent b659575 commit f53a2b6

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Object Tracking"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 2,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import cv2\n",
17+
"import numpy as np\n",
18+
"\n",
19+
"# camera is inirialised\n",
20+
"capture = cv2.VideoCapture(0)\n",
21+
"\n",
22+
"# range of HSV color space\n",
23+
"lower_purple=np.array([130,50,90])\n",
24+
"upper_purple = np.array([170,255,255])\n",
25+
"\n",
26+
"# to store the center points\n",
27+
"points=[]\n",
28+
"\n",
29+
"# get frame size\n",
30+
"ret,frame=capture.read()\n",
31+
"Height,Width = frame.shape[:2]\n",
32+
"frame_count=0\n",
33+
"\n",
34+
"while True:\n",
35+
" # cam frame is read\n",
36+
" ret,frame = capture.read()\n",
37+
" hsv_img = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)\n",
38+
"\n",
39+
" # threshodl to get only purple colors\n",
40+
" mask =cv2.inRange(hsv_img,lower_purple,upper_purple)\n",
41+
"\n",
42+
" # find countors\n",
43+
" contours,_ = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,\n",
44+
" cv2.CHAIN_APPROX_SIMPLE)\n",
45+
" # get the centroid of mass\n",
46+
" center = int(Height/2),int(Width/2)\n",
47+
"\n",
48+
" if len(contours)>0:\n",
49+
" # get only the lasrgest contour\n",
50+
" c=max(contours,key=cv2.contourArea)\n",
51+
" (x,y ),radius = cv2.minEnclosingCircle(c)\n",
52+
" M=cv2.moments(c)\n",
53+
"\n",
54+
" try:\n",
55+
" center=(int(M[\"m10\"]/M[\"m00\"]),int(M[\"m01\"]/M[\"m00\"]))\n",
56+
" except:\n",
57+
" center=int(Height/2),int(Width/2)\n",
58+
"\n",
59+
" # draw circle only if its greater than a particular size\n",
60+
" if radius>25:\n",
61+
" cv2.circle(frame,(int(x),int(y)),int(radius),(0,0,255))\n",
62+
" cv2.circle(frame,center,5,(0,255,0),-1)\n",
63+
"\n",
64+
"\n",
65+
" points.append(center)\n",
66+
"\n",
67+
" # loop over the points\n",
68+
" if radius>25:\n",
69+
" for i in range(1,len(points)):\n",
70+
" try:\n",
71+
" cv2.line(frame,points[i-1],points[i],(0,255,0),2)\n",
72+
" except:\n",
73+
" pass\n",
74+
" frame_count = 0\n",
75+
"\n",
76+
" else:\n",
77+
" # count the frames\n",
78+
" frame_count+=1\n",
79+
" # if its 10 frames then trails are deleted\n",
80+
" if frame_count==10:\n",
81+
" points=[]\n",
82+
" frame_count=0\n",
83+
"\n",
84+
" frame = cv2.flip(frame,1)\n",
85+
" cv2.imshow(\"Object tracker\",frame)\n",
86+
"\n",
87+
" if cv2.waitKey(1)==13:\n",
88+
" break\n",
89+
"capture.release()\n",
90+
"cv2.destroyAllWindows()\n"
91+
]
92+
}
93+
],
94+
"metadata": {
95+
"kernelspec": {
96+
"display_name": "Python 3",
97+
"language": "python",
98+
"name": "python3"
99+
},
100+
"language_info": {
101+
"codemirror_mode": {
102+
"name": "ipython",
103+
"version": 3
104+
},
105+
"file_extension": ".py",
106+
"mimetype": "text/x-python",
107+
"name": "python",
108+
"nbconvert_exporter": "python",
109+
"pygments_lexer": "ipython3",
110+
"version": "3.7.3"
111+
}
112+
},
113+
"nbformat": 4,
114+
"nbformat_minor": 2
115+
}

0 commit comments

Comments
 (0)