-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbird-detection.py
105 lines (84 loc) · 3.73 KB
/
bird-detection.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Importing necessary libraries
import cv2
import time
# Class Definition
class DetectBirds(object):
# Constructor
def __init__(self, video, mx_num_birds = 3):
# Getting a video
self.cap = cv2.VideoCapture(f"Videos/{video}")
# Uncomment the following line for automatic USBCam (0-web cam default)
# self.cap = cv2.VideoCapture(0)
# Definition of haar cascades for bird recognition
self.birdsCascade = cv2.CascadeClassifier("bird-cascade.xml")
# Validate if ESC key is pressed to close the window
self.running = True
# Previous Time Elapsed
self.pTime = 0
# Number of birds detected in a frame
self.num_birds = 0
# Set minimum number of birds to be detected per frame as 3
self.MIN_NUM_BIRDS = mx_num_birds
# Function to detect birds during runtime
def detect(self):
while self.running:
# Capture frame-by-frame from a video
ret, frame = self.cap.read()
if ret:
# Convert the frame into gray scale for better analysis
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect birds in the gray scale image
birds = self.birdsCascade.detectMultiScale(
gray,
scaleFactor=1.4,
minNeighbors=5,
maxSize=(30, 30),
flags = cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the detected birds in the frame
for (x, y, w, h) in birds:
cv2.rectangle(
frame,
(x, y),
(x+w, y+h),
(0, 200, 0),
2
)
cv2.putText(
frame,
'Detected Bird',
(x, y - 10 if y > 20 else y + 10),
cv2.FONT_HERSHEY_COMPLEX,
0.6,
(255,0,255),
2
)
# Getting the value of frames per second (FPS)
cTime = time.time()
fps = 1/(cTime-self.pTime)
self.pTime = cTime
# If detected birds greater than minimum number of birds to be detected
if (len(birds)>=self.MIN_NUM_BIRDS):
self.num_birds=len(birds)
# Output the value of FPS and Number of birds per frame
cv2.putText(frame,"Press 'ESC' to exit",(30,70), cv2.FONT_HERSHEY_PLAIN, 2.5, (0,0,0), 3, cv2.FILLED)
cv2.putText(frame, "FPS: " + str(int(fps)), (30,120), cv2.FONT_HERSHEY_PLAIN, 2.5, (0,0,0), 3, cv2.FILLED)
cv2.putText(frame, f"Birds Detected: {self.num_birds}", (30,170), cv2.FONT_HERSHEY_PLAIN, 2.5, (0,0,0), 3, cv2.FILLED)
# Display the resulting frame
cv2.imshow('Detected Birds', frame)
# Pressing "ESC" will stop the program
k = cv2.waitKey(1) & 0xff
if k == 27:
self.running = False
else:
self.running = False
# When everything done, release the capture and go back take another one
self.cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
print("\n\n\t\t---BIRD DETECTION IN VIDEO---")
print("\nAUTHORS : \n\t1.Mohamed Asif (2005032)\n\t2.Priyanka S (2005038)\n\t3.Shrikanth D (2005046)")
video = input("\nEnter the video file (in .mp4 format): ")
D = DetectBirds(video=video)
D.detect()
print("\nTHANK YOU! :)")