Detect and Aware pt.1
This is my first article related to the projects i that we will be building with the help of OpenCV and python.
The idea of the project is related to the fact that birds or animals often destroy the standing crops which farmers have. We will be creating a solution to it. It will be in a way that whenever birds will enter the farm a text message will be sent to the farmer instantly warning him that birds have entered his farm. Currently i have used a video of birds entering a farm to show the working of the project but we can extend it to cameras which will be installed near the farm. I am using a pre-trained data which you will find in the github repo i will be attaching at the lasts.
Lets dive into creating it.
First we will install the essentials which we need in this project with the command as given below.
pip install opencv-contrib-python pip install nexmo
I am using the contrib version of opencv rather than the ordinary version as it has much more things related to the basic version. Nexmo is the api which we use in python to send messages to a phone number. Nexmo provides 100 messages as a free trial pack and we can sent these messages to a number which we have used in out nexmo account.
Once we have those installed we will import the things in our main file.
import cv2 import nexmo
Now creating a nexmo client.
client = nexmo.Client(key='Your nexmo key', secret='Your nexmo secret key')
You have to visit the link given below and create a key and secret key for your nexmo client.
Once we have those created then we will replace in the code above.
Now we will specify the phone number on which we want to send the messages. Also we will be specifying the message that will be sent.
number = "your phone number with the country code" message = "Pigeons have entered your farm"
Now we will be starting the video capture. I have used a video as mentioned above.
cap = cv2.VideoCapture('birds.mp4')
Next we will be specifying the pre-trained data which is trained according to the birds. We have an xml file for that and we have used a Cascade classifier for it which is a pre-built opencv method to use the data from the file.
birdsCascade = cv2.CascadeClassifier("birds1.xml")
Next specifying the maximum number of birds which will be entering in the farm and then a message will be sent. Running is a variable which we will be using to run our loop ntil the birds are present in the frame being captured. Count will be the number which is used as the variable with the help of which the message will be sent to through nexmo.
MAX_NUM_BIRDS = 5 running = True count = 0
Now starting the loop with the help of running variable.
while running:
Next triggering our nexmo api for the value of count to be one. We have sent the message with the help of client. We have specified form whom the ,message will be sent and we have also specified the number and the message which is to be sent.
if count == 1: response = client.send_message({ 'from': 'GYMAALE', 'to': number, 'text': message, })
After we have send the message we will check for the json repsonse which we get and from that we will see the status of the message delivered and print the output accordingly and will increase the variable count for avoiding message to be sent again.
if response['status'] == '0': print("MESSAGE DELIVERED SUCCESSFULLY", response['message-id']) else: print("ERROR SENDING MESSAGE", response['error-text']) count += 1
Till now we have seen how the message will be sent to the farmer. In the next blog i will be telling you when the message will be send and how we will classify and mark birds in the video.
The code for this tutorial is 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
Link to the github repo will be attached in the next blog.
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 6, 2020.