-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (59 loc) · 1.45 KB
/
main.py
File metadata and controls
76 lines (59 loc) · 1.45 KB
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
from time import time
import cv2
from numpy import intersect1d
import torch
from gpiozero import Buzzer, Servo
from serial import Serial
model = torch.hub.load('ultralytics/yolov5', 'yolov5m')
danger = open('danger.txt').read().strip().split('\n')
gsm = Serial("/dev/ttyUSB0", 9600, timeout=0.5)
gsm.flush()
servo = Servo(4)
bz = Buzzer(3)
mobile = '##########' # Mobile Number to send the SMS
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
def sendSms(msg):
print("Sending SMS\n")
gsm.write(b'AT+CMGF=1\r\n')
time.sleep(0.5)
gsm.write(b'AT+CMGS=\"')
gsm.write(mobile.encode())
gsm.write(b'\"\r\n')
time.sleep(0.5)
data = msg
gsm.write(data.encode())
gsm.write(b'\x1A')
time.sleep(3)
def detect(img):
result = model(img)
result.print()
object = result.pandas().xyxy[0].name.unique()
print(object)
common = intersect1d(object, danger)
if(len(common)):
# Raise Alarm
sendSms('There is an intrusion in the farm')
bz.on()
print("ALARM")
while True:
t0 = time()
val = -10
add = 2
frame = None
success, frame = cap.read()
if success:
detect(frame)
else:
print('Error in Camera') # For debugging purposes
continue
servo.value = val/10.0
while(time()-t0 < 10):
continue
bz.off()
val += add
if(val == 10 or val == -10):
add *= -1
print('time taken: ')
print(time()-t0)