Skip to content

Commit 7ab207e

Browse files
Merge pull request avinashkranjan#2895 from smty2018/cartrack
Car Tracking using YOLO and OpenCV
2 parents 79593fb + be4b217 commit 7ab207e

File tree

5 files changed

+934
-0
lines changed

5 files changed

+934
-0
lines changed

Car Detection OpenCV/ReadMe.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Car Tracking using YOLO and OpenCV:
2+
3+
This project demonstrates car tracking in videos using the YOLO (You Only Look Once) object detection model and the OpenCV library. The YOLO model is used to detect cars in each frame of a video stream, and bounding boxes are drawn around the detected cars to track their movement.
4+
5+
- Download YOLOv3 weights: [yolov3.weights](https://pjreddie.com/media/files/yolov3.weights)
6+
- Download YOLOv3 configuration: [yolov3.cfg](https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg)
7+
- Download COCO names file: [coco.names](https://github.com/pjreddie/darknet/blob/master/data/coco.names)
8+
9+
pip install opencv-python

Car Detection OpenCV/app.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
yolo_net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
6+
7+
8+
class_names = []
9+
with open('coco.names', 'r') as f:
10+
class_names = f.read().strip().split('\n')
11+
12+
13+
video_capture = cv2.VideoCapture('video_file.mp4')
14+
15+
while video_capture.isOpened():
16+
ret, frame = video_capture.read()
17+
if not ret:
18+
break
19+
20+
height, width, _ = frame.shape
21+
22+
23+
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
24+
yolo_net.setInput(blob)
25+
26+
layer_names = yolo_net.getUnconnectedOutLayersNames()
27+
outs = yolo_net.forward(layer_names)
28+
29+
for out in outs:
30+
for detection in out:
31+
scores = detection[5:]
32+
33+
class_id = np.argmax(scores)
34+
confidence = scores[class_id]
35+
if confidence > 0.5 and class_id == 2:
36+
center_x = int(detection[0] * width)
37+
38+
center_y = int(detection[1] * height)
39+
bbox_width = int(detection[2] * width)
40+
41+
bbox_height = int(detection[3] * height)
42+
43+
x = int(center_x - bbox_width / 2)
44+
y = int(center_y - bbox_height / 2)
45+
cv2.rectangle(frame, (x, y), (x + bbox_width, y + bbox_height), (0, 255, 0), 2)
46+
47+
48+
resized_frame = cv2.resize(frame, (720, 480))
49+
50+
cv2.imshow('Car Tracking', frame)
51+
52+
if cv2.waitKey(1) & 0xFF == ord('q'):
53+
break
54+
55+
video_capture.release()
56+
cv2.destroyAllWindows()

Car Detection OpenCV/coco.names

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
person
2+
bicycle
3+
car
4+
motorbike
5+
aeroplane
6+
bus
7+
train
8+
truck
9+
boat
10+
traffic light
11+
fire hydrant
12+
stop sign
13+
parking meter
14+
bench
15+
bird
16+
cat
17+
dog
18+
horse
19+
sheep
20+
cow
21+
elephant
22+
bear
23+
zebra
24+
giraffe
25+
backpack
26+
umbrella
27+
handbag
28+
tie
29+
suitcase
30+
frisbee
31+
skis
32+
snowboard
33+
sports ball
34+
kite
35+
baseball bat
36+
baseball glove
37+
skateboard
38+
surfboard
39+
tennis racket
40+
bottle
41+
wine glass
42+
cup
43+
fork
44+
knife
45+
spoon
46+
bowl
47+
banana
48+
apple
49+
sandwich
50+
orange
51+
broccoli
52+
carrot
53+
hot dog
54+
pizza
55+
donut
56+
cake
57+
chair
58+
sofa
59+
pottedplant
60+
bed
61+
diningtable
62+
toilet
63+
tvmonitor
64+
laptop
65+
mouse
66+
remote
67+
keyboard
68+
cell phone
69+
microwave
70+
oven
71+
toaster
72+
sink
73+
refrigerator
74+
book
75+
clock
76+
vase
77+
scissors
78+
teddy bear
79+
hair drier
80+
toothbrush

Car Detection OpenCV/video_file.mp4

24.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)