-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove2object.py
More file actions
126 lines (112 loc) · 3.59 KB
/
move2object.py
File metadata and controls
126 lines (112 loc) · 3.59 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 11 20:58:34 2019
@author: kota421
"""
def move2object(IMG_SHAPE,x,y,h,w):
"""
Function to tell the drone in which direction and with how much
distance it should move in order to centerlize the object in its
vision.
<FRAME DIRECTIONS>
2D directions in the drone's frame in terms of the XY directions
in the image of shape IMG_SHAPE:
forward: -y
back: +y
right: +x
left: -x
Plus, vertical directions are defined as follows:
up: +z
down: -z
<ARGUMENTS>
Assumes:
-IMG_SHAPE: tuple of two ints (height,width)
-x,y: int, coordinates of the center of the object
-h,w: int, height and width of the object
Returns:
-(x-direction,delta_x*): x-direction is str, either 'right' or 'left' or '+' **
-(y-direction,delta_y): y-direction is str, either 'forward' or 'back' or '+'
-(z-direction,delta_z): z-direction is str, either 'up' or 'down' or '+'
* delta_x is a float representing the ratio of the separation of the center
of the image and the center of the object in x direction.
(-1.0 < delta_x < 1.0)
** '+' means that the drone should keep the current position in that axis
delta_x is 0 in this case.
"""
assert x>=0 and y>=0
import numpy as np
H,W = IMG_SHAPE[0], IMG_SHAPE[1]
# Center of the image
O = np.array([H/2, W/2])
# Center of the object
P = np.array([y,x])
OP = P - O
OP_y = list(OP)[0]
OP_x = list(OP)[1]
# Determines the possible range of the distance that the drone will keep with
# respect to the object
a,b = 0.2, 0.3
c,d = a,b
x_frac = OP_x/(W/2)
y_frac = OP_y/(H/2)
# X
if x_frac > 0.05:
(x_direction,delta_x) = 'right', x_frac
elif x_frac < -0.05:
(x_direction,delta_x) = 'left', x_frac
else:
(x_direction,delta_x) = '+', 0
# Y
if y_frac > 0.05:
(y_direction,delta_y) = 'back', y_frac
elif y_frac < -0.05:
(y_direction,delta_y) = 'forward', y_frac
else:
(y_direction,delta_y) = '+', 0
# Z
if h/H < a or w/W < c:
(z_direction,delta_z) = 'down', h/H - a
elif h/H > b or w/W > d:
(z_direction,delta_z) = 'up', h/H - b
else:
(z_direction,delta_z) = '+', 0
return (x_direction,delta_x),(y_direction,delta_y),(z_direction,delta_z)
### TEST
## Variables
H = 2.0
W = 2.0
IMG_SHAPE = (H,W)
x = 1.15
y = 0.9
z = 7.00
h = 3/z
w = 2/z
## Visualize
def showMeHelmet(IMG_SHAPE,x,y,h,w):
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
H, W = IMG_SHAPE[0], IMG_SHAPE[1]
if h*w > 0:
delta_x = x - (W/2)
delta_y = y - (H/2)
plt.figure()
fig = plt.gca()
plt.scatter([delta_x],[delta_y],color='black')
obj = Rectangle((delta_x-(w/2),delta_y-(h/2)), w,h,alpha=0.5,facecolor='red')
fig.add_patch(obj)
plt.xlim(-W/2,W/2)
plt.ylim(H/2,-H/2)
plt.scatter([0],[0],marker='+')
plt.show()
#showMeHelmet(IMG_SHAPE,x,y,h,w)
## Info for Humans
def moveDrone(IMG_SHAPE,x,y,h,w):
H, W = IMG_SHAPE[0], IMG_SHAPE[1]
(x_direction,delta_x),(y_direction,delta_y),(z_direction,delta_z) = move2object(IMG_SHAPE,x,y,h,w)
print("Image Width and Height:",W,H)
print("Object Position:", delta_x, delta_y)
print('Move',x_direction,'by', delta_x)
print('Move',y_direction,'by', delta_y)
print('Move',z_direction,'by', delta_z)
#moveDrone(IMG_SHAPE,x,y,h,w)