-
Notifications
You must be signed in to change notification settings - Fork 0
/
car and pedestrain detection
66 lines (64 loc) · 2.23 KB
/
car and pedestrain detection
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"import cv2\n",
"import numpy as np\n",
"\n",
"# Create our body classifier\n",
"body_classifier = cv2.CascadeClassifier('Haarcascades\\haarcascade_fullbody.xml')\n",
"\n",
"# Initiate video capture for video file\n",
"cap = cv2.VideoCapture('images/walking.avi')\n",
"\n",
"# Loop once video is successfully loaded\n",
"while cap.isOpened():\n",
" \n",
" # Read first frame\n",
" ret, frame = cap.read()\n",
" frame = cv2.resize(frame, None,fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)\n",
"\n",
" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" \n",
" \n",
" # Pass frame to our body classifier\n",
" bodies = body_classifier.detectMultiScale(gray, 1.2, 3)\n",
" \n",
" # Extract bounding boxes for any bodies identified\n",
" for (x,y,w,h) in bodies:\n",
" cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)\n",
" cv2.imshow('Pedestrians', frame)\n",
"\n",
" if cv2.waitKey(1) == 13: #13 is the Enter Key\n",
" break\n",
"\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
"import cv2\n",
"import time\n",
"import numpy as np\n",
"\n",
"# Create our body classifier\n",
"car_classifier = cv2.CascadeClassifier('Haarcascades\\haarcascade_car.xml')\n",
"\n",
"# Initiate video capture for video file\n",
"cap = cv2.VideoCapture('images/cars.avi')\n",
"\n",
"\n",
"# Loop once video is successfully loaded\n",
"while cap.isOpened():\n",
" \n",
" time.sleep(.05)\n",
" # Read first frame\n",
" ret, frame = cap.read()\n",
" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" \n",
" # Pass frame to our car classifier\n",
" cars = car_classifier.detectMultiScale(gray, 1.4, 2)\n",
" \n",
" # Extract bounding boxes for any bodies identified\n",
" for (x,y,w,h) in cars:\n",
" cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 255), 2)\n",
" cv2.imshow('Cars', frame)\n",
"\n",
" if cv2.waitKey(1) == 13: #13 is the Enter Key\n",
" break\n",
"\n",
"cap.release()\n",
"cv2.destroyAllWindows()"