Detect and Aware pt.2
In the previous article i have told you about how you can install the dependencies and send messages to the farmers. In this article i will be continuing from where i have left. We will be seeing how the videos are analyzed and how the we mark the birds in our live video or the video we are playing in this.
So let’s dive into it.
We will start by seeing how the video is analyzed. For that we have two variables, ret will store the number of boxes formed(from boxes you can think of how how many birds are there in the frame, in some cases the value of ret can be boolean defining if we have the thing we are searching for, here birds, in the frame or not) and the frame variable will be in the form of numpy arrays and will consider the info of the pixels in a particular frame. We will print the number of boxes by using a print statement.
ret, frame = cap.read() print("Ret is ", ret)
Now if there are birds will will start a loop and convert the colored frame in gray. This is done using a COLOR_BGR2GRAY method which is pre built in cv2.cvtColor function.
if ret: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Now we will use the cascade which is a pre-built data-set to detect birds in the video with the help of a detectMultiScale function. It will have a scale factor which will determine how much the image size is reduced each time, minNeighbors which will determine the minimum number of neighbors around a particular box formed or bird found, maxSize will be the size of the object which is to be identified,flags will be some extra info which we want to pass like here we have passed the cascade_scale_image.
birds = birdsCascade.detectMultiScale( gray, scaleFactor=1.4, minNeighbors=1, # minSize=(10,10), maxSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE )
Now we will check if we have the desired number of birds or not in the frame and will print the birds in the frame.
if (len(birds) >= MAX_NUM_BIRDS): print("Detected {0} Birds.".format(len(birds)))
Next we will be drawing a rectangle around the birds which have been detected and stored inside the birds variable above.
for (x, y, w, h) in birds: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 200, 0), 2)
x,y,w,h are the coordinate which are the x and y point and width and height. We have drawn a rectangle on the frame, starting point of the rectangle is (x,y), (x+w,y+h) will be the ending coordinates of the rectangle, (0,200,0) will be the color with which the rectangle will be drawn and 2 is the size of the rectangle line i.e. the thickness.
Now displaying the resulting frame along with the rectangle. We have set a waitkey and have set that when we will press the ‘q’ key the process will stop that is the video will be stopped and the variable which results in running of the function is set to False.
cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): running = False else: running = False
Next we will releasing the capture image or the video which is being recorded and will destroy all the windows which have been aroused due to the running of the program.
cap.release() cv2.destroyAllWindows()
The code which we have used in this part is given below.
ret, frame = cap.read() print("Ret is ", ret) if ret: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) birds = birdsCascade.detectMultiScale( gray, scaleFactor=1.4, minNeighbors=1, # minSize=(10,10), maxSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE ) if (len(birds) >= MAX_NUM_BIRDS): print("Detected {0} Birds.".format(len(birds))) # Drawing a rectangle around a bird approaching the farm for (x, y, w, h) in birds: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 200, 0), 2) # Displaying the resulting frame cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): running = False else: running = False cap.release() cv2.destroyAllWindows()
The full code for the complete tutorial is also given below.
import cv2 import nexmo # Nexmo messaging client = nexmo.Client(key='Your nexmo key', secret='Your nexmo secret key') number = "your phone number with the country code" message = "Pigeons have entered your farm" # initalizing the camera cap = cv2.VideoCapture('birds.mp4') birdsCascade = cv2.CascadeClassifier("birds1.xml") MAX_NUM_BIRDS = 5 running = True count = 0 # Detecting the birds while running: if count == 1: response = client.send_message({ 'from': 'GYMAALE', 'to': number, 'text': message, }) response = response['messages'][0] if response['status'] == '0': print("MESSAGE DELIVERED SUCCESSFULLY", response['message-id']) else: print("ERROR SENDING MESSAGE", response['error-text']) count += 1 ret, frame = cap.read() print("Ret is ", ret) if ret: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) birds = birdsCascade.detectMultiScale( gray, scaleFactor=1.4, minNeighbors=1, # minSize=(10,10), maxSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE ) if (len(birds) >= MAX_NUM_BIRDS): print("Detected {0} Birds.".format(len(birds))) # Drawing a rectangle around a bird approaching the farm for (x, y, w, h) in birds: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 200, 0), 2) # Displaying the resulting frame cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): running = False else: running = False cap.release() cv2.destroyAllWindows()
I will be attaching the github repositary below for your help to have a better look at the code and it also considers the assets used in this tutorial.
I hope you have understood this. If you have any doubt then please comment it down. I would be happy to help.
Originally published at https://harshblog.xyz on May 7, 2020.