-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercises03.py
129 lines (102 loc) · 4.4 KB
/
Exercises03.py
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
# MIT LICENSE
#
# Copyright 2024 Michael J. Reale
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###############################################################################
# IMPORTS
###############################################################################
import sys
import numpy as np
import torch
import cv2
import pandas
import sklearn
def make_bounce_video(image_shape=(480,640,3),
frame_cnt=30,
start_pos=(100,300),
radius=40,
speed_factor=1):
all_frames = []
pos = np.array(start_pos)
velocity = np.array([2,0]) #left to right, not going down
accel = np.array([0,1])
velocity *= speed_factor
accel *= speed_factor
for i in range(frame_cnt):
image = np.zeros(image_shape, dtype="uint8") #blank bg image
cv2.circle(image, pos, radius, (0,0,255), -1) #red color, -1 means filled
all_frames.append(image)
pos += velocity
velocity += accel
#y coord only.
if (pos[1] + radius) >= image_shape[0]:
#if ball hits the bottom boundary
pos = image_shape[0] - 1 - radius #bounce
velocity[1] = -0.6*velocity[1] #1 is y coord
return all_frames
###############################################################################
# MAIN
###############################################################################
def main():
###############################################################################
# PYTORCH
###############################################################################
b = torch.rand(5,3)
print(b)
print("Torch CUDA?:", torch.cuda.is_available())
###############################################################################
# PRINT OUT VERSIONS
###############################################################################
print("Torch:", torch.__version__)
print("Numpy:", np.__version__)
print("OpenCV:", cv2.__version__)
print("Pandas:", pandas.__version__)
print("Scikit-Learn:", sklearn.__version__)
###############################################################################
# OPENCV
key = -1
ESC_KEY = 27
index = 0
# OVERRIDE
pos = (100,100)
radius = 40
frame_cnt = 60
speed_factor = 2
video_frames = make_bounce_video(start_pos=pos,
frame_cnt=frame_cnt,
radius=radius,
speed_factor=speed_factor)
# x, y, width, height
track_window = (pos[0]-radius,pos[1]-radius,2*radius,2*radius)
criteria = (cv2.TERM_CRITERIA_COUNT | cv2.TERM_CRITERIA_EPS, 10, 1)
while key != ESC_KEY:
cur_frame = video_frames[index]
track_image = np.copy(cur_frame)
cv2.rectangle(track_image,track_window[0:2],
(track_window[0]+track_window[2], #starting x + width
track_window[1]+track_window[3]), #startying y + width
(0,255,0), 3)
cv2.imshow("ORIGINAL", cur_frame)
key = cv2.waitKey(33)
index += 1
if index >= len(video_frames):
index = 0
cv2.destroyAllWindows()
# Close down...
print("Closing application...")
if __name__ == "__main__":
main()
# The end