forked from TMet-Aerial-Vehicles/Computer-Vision-Drone-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (110 loc) · 3.78 KB
/
main.py
File metadata and controls
144 lines (110 loc) · 3.78 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import time
import sys
from qr.qr_reader import QRReader
from detection import Detection
# from serial_communication import SerialCommunication
from calculations import intruder_centre_offset, bounding_box_centroid
from sound import countdown
from cv2 import cv2
from datetime import datetime
import logging
from logging_script import start_logging
start_logging()
# Main Python script
def detection(image_path=None, detect_once=False):
"""
Perform intruder detection
Using input feed, find the intruder on the screen
Args:
image_path:
detect_once:
Returns:
"""
# Initialize Detection class
d = Detection()
exit_call = False
while not exit_call:
# Create image frames or read from source
# success, frame = capture.read()
frame = cv2.imread(
'/Users/craig/Projects/JTC_ComputerVision/images/multi2.png')
# Perform intruder detection on frame
bounding_boxes = d.intruder_detection(frame, display_detection=True)
# Return offsets
if len(bounding_boxes) == 0:
# Not intruder detected
# Spin
# Check history
pass
elif len(bounding_boxes) > 1:
# Multiple intruders detected
pass
else:
# Determine centroid of bounding box
x_centre, y_centre = bounding_box_centroid(bounding_boxes[0])
logging.info(f"Image Size: {frame.shape}")
x_offset, y_offset = intruder_centre_offset(frame.shape[1],
frame.shape[0],
x_centre, y_centre)
logging.info(f"{datetime.now()}: Offset ({x_offset}, {y_offset})")
# Save offset to file?
pass
# Calculate intruder position (long, lat)
# long, lat = calculate_intruder_position(cam_height, cam_angle)
# Check stdin for exit call
# if detect_once or sys.stdin:
if detect_once:
exit_call = True
if __name__ == "__main__":
# START PROGRAM
# Boot camera
camera = cv2.VideoCapture(0)
try:
# Sleep to load camera
time.sleep(5)
# Test that camera is ready
if camera.isOpened():
time.sleep(1)
else:
raise IOError("Unable to open webcam")
except:
logging.info("Unable to open camera capture")
sys.exit(1)
# Boot ports
# Set up connection with Arduino
# ser = SerialCommunication()
# Detect QR from image feeds and Decode/Format Message
qrr = QRReader()
# Live Feed
qrr.read(camera=camera, active_display=True, display_time=20)
# From Image
# img_path = "/Users/craig/Projects/JTC_ComputerVision/sampleQR2.png"
# qrr.read(active_display=display_qr, image=img_path)
# Read QR and coordinates
if qrr.message:
logging.info(qrr.message)
longitude = qrr.message.longitude
latitude = qrr.message.latitude
else:
logging.info("Unable to detect QR")
sys.exit()
# Send Coordinates to PixHawk loaded with ArduPilot
# Figure out connection to send data
# 20 sec countdown,
countdown(20)
# Fly to position
# Figure out how to know when Drone reaches coordinates
# Get image picture
# Find intruder
# detection(detect_once=True)
# Rotate Camera to centre
# Calculate drone position
# Calculate intruder position
# Save position data
# Repeat get image picture and finding intruder
#
# END PROGRAM
# SAFETY FEATURES
# 1) Check drone coordinates. Ensure its within the given position
# If flying outside, return back to starting position
# 2) Anything goes wrong, send data to return to home, Dont land