-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfExercises00.py
189 lines (141 loc) · 6.27 KB
/
ProfExercises00.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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 burn_eyes(image):
output = np.copy(image)
output = cv2.resize(output, dsize=(0,0), fx=0.1, fy=0.1)
output = cv2.resize(output, dsize=(0,0), fx=10.0, fy=10.0,
interpolation=cv2.INTER_NEAREST)
return output
def blur_across_buffer(frame_buffer, time_index, frame):
frame = frame.astype(np.float64)
frame /= 255.0
frame_buffer[time_index] = frame
ave_image = np.mean(frame_buffer, axis=0)
time_index += 1
time_index %= len(frame_buffer) # frame_buffer.shape[0]
return frame_buffer, time_index, ave_image
###############################################################################
# MAIN
###############################################################################
def main():
myimage = np.zeros((480, 640, 3), dtype="uint8")
myimage[:240,...,0] = 255
cv2.imshow("Test", myimage)
cv2.waitKey(-1)
cv2.destroyAllWindows()
###############################################################################
# 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
###############################################################################
if len(sys.argv) <= 1:
# Webcam
print("Opening webcam...")
# Linux/Mac (or native Windows) with direct webcam connection
capture = cv2.VideoCapture(1) #, cv2.CAP_DSHOW) # CAP_DSHOW recommended on Windows
# WSL: Use Yawcam to stream webcam on webserver
# https://www.yawcam.com/download.php
# Get local IP address and replace
#IP_ADDRESS = "192.168.0.7"
#capture = cv2.VideoCapture("http://" + IP_ADDRESS + ":8081/video.mjpg")
# Did we get it?
if not capture.isOpened():
print("ERROR: Cannot open capture!")
exit(1)
# Set window name
windowName = "Webcam"
else:
# Trying to load video from argument
# Get filename
filename = sys.argv[1]
# Load video
capture = cv2.VideoCapture(filename)
# Check if data is invalid
if not capture.isOpened():
print("ERROR: Could not open or find the video!")
exit(1)
# Set window name
windowName = "Video"
# Create window ahead of time
cv2.namedWindow(windowName)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
my_frames = []
frame_buffer = None
time_index = 0
# While not closed...
key = -1
while key == -1:
# Get next frame from capture
ret, frame = capture.read()
my_frames.append(frame)
if ret == True:
if frame_buffer is None:
frame_cnt = 10
video_shape = (frame_cnt,) + frame.shape
frame_buffer = np.zeros(video_shape, dtype=np.float64)
# Show the image
cv2.imshow(windowName, frame)
proc_frame = burn_eyes(frame)
cv2.imshow("UNSPEAKABLE HORRORS", proc_frame)
frame_buffer, time_index, ave_image = blur_across_buffer(frame_buffer,
time_index,
frame)
cv2.imshow("AVERAGE", ave_image)
fimage = frame.astype(np.float64)/255.0
fimage = np.absolute(fimage - ave_image)
cv2.imshow("GHOST", fimage)
frame_cnt = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_index = int(capture.get(cv2.CAP_PROP_POS_FRAMES))
if(frame_cnt != -1 and frame_cnt == frame_index):
capture.set(cv2.CAP_PROP_POS_FRAMES, 0)
else:
break
# Wait 30 milliseconds, and grab any key presses
key = cv2.waitKey(30)
my_video = np.array(my_frames)
print("VIDEO:", my_video.shape)
# Release the capture and destroy the window
capture.release()
cv2.destroyAllWindows()
# Close down...
print("Closing application...")
if __name__ == "__main__":
main()
# The end