-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomDeer.py
More file actions
109 lines (79 loc) · 3.01 KB
/
randomDeer.py
File metadata and controls
109 lines (79 loc) · 3.01 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
import os, random, ctypes,time,cv2
import numpy as np
from pathlib import Path
from apscheduler.schedulers.background import BlockingScheduler
import paths
path = paths.path
transitionPath = paths.transitionPath
newDeer = None
oldDeer = None
for x in range(0, len(os.listdir(transitionPath))):
os.remove(os.path.join(transitionPath, "frame"+str(x)+".jpg"))
for p, d, f in os.walk(paths.mainPath, False):
for file in f:
if file.endswith('.mp4'):
global videoPath
videoPath = os.path.join(paths.mainPath, file.title())
# First Run
def getNewDeer():
global newDeer
newDeer = random.choice(os.listdir(path))
print(newDeer)
getNewDeer()
oldDeer = newDeer
def transitionDeer():
# input the code of replacing blue / green with the deer
video = cv2.VideoCapture(videoPath)
image1 = cv2.imread(os.path.join(path, oldDeer))
image2 = cv2.imread(os.path.join(path, newDeer))
# Breaking it down
currentFrame = 0
while(True):
# Capture frame-by-frame
ret, frame = video.read()
if not ret: break
# Operate (blue)
lower_blue = np.array([70,70,70])
upper_blue = np.array([240,140,255])
copiedImage = cv2.resize(image1, (1920,1080))
frame = cv2.resize(frame, (1920,1080))
mask = cv2.inRange(frame, lower_blue, upper_blue)
frame[mask != 0 ] = [0,0,0]
copiedImage[mask == 0] = [0,0,0]
frame = copiedImage + frame
# Operate (green)
lower_green = np.array([0,180,0])
upper_green = np.array([100,255,100])
copiedImage = cv2.resize(image2, (1920,1080))
frame = cv2.resize(frame, (1920,1080))
mask = cv2.inRange(frame, lower_green, upper_green)
frame[mask != 0 ] = [0,0,0]
copiedImage[mask == 0] = [0,0,0]
frame = copiedImage + frame
# Saves image of the current frame in jpg file
name = './transitionFrames/frame' + str(currentFrame) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
# When everything done, release the capture
video.release()
cv2.destroyAllWindows()
# Playing it
for x in range(0, len(os.listdir(transitionPath))):
ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.join(transitionPath, "frame"+str(x)+".jpg"), 0)
time.sleep(0.1)
#Keeping last frame as background, to prevent scaling thing.
#ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.join(path, newDeer), 0)
for x in range(0, len(os.listdir(transitionPath))):
os.remove(os.path.join(transitionPath, "frame"+str(x)+".jpg"))
transitionDeer()
def deerSwap():
getNewDeer()
transitionDeer()
global oldDeer
oldDeer = newDeer
# Automatic Run
scheduler = BlockingScheduler()
scheduler.add_job(deerSwap, 'interval', seconds=40)
scheduler.start()