-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_cannyedge.py
133 lines (122 loc) · 5.96 KB
/
example_cannyedge.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
try:
import sys
import os
#u gotta run this from cmd, run python file/F5(debug) on vscode fails., u have to PRESS THE BUTTON
'''
to compile with pyinstaller on windows/mac
cd to examples folder (this folder)
python -m PyInstaller WIN_CannyEdge.spec --clean
OR
python -m PyInstaller MAC_CannyEdge.spec --clean
'''
# # / and \ works on windows, only / on mac tho
# sourcelocation = "examples\creativecommonsmedia\Elephants Dream charstart2.webm"
# sourcelocation = os.path.join("examples", "creativecommonsmedia", "Elephants Dream charstart2FULL_265.webm")
sourcelocation = os.path.join("examples", "creativecommonsmedia", "Elephants Dream charstart2FULL_265.mp4")
# sourcelocation = "examples\creativecommonsmedia\\30 fps counter.webm"
# sourcelocation = "NDA"
if hasattr(sys, "_MEIPASS"):
pass
else:
# if you're making your own app, you don't need this if-else block. This is just vanity code so this file can be run from main FastCVApp folder or from the examples subfolder.
# this example is importing from a higher level package if running from cmd: https://stackoverflow.com/a/41575089
# add the right path depending on if you're running from examples or from main folder:
if os.path.join("fastcvapp", "fastcvapp", "examples").lower() in os.getcwd().lower():
# when running from examples folder, append the upper level
sys.path.append(
".."
)
elif os.path.join("fastcvapp", "fastcvapp").lower() in os.getcwd().lower():
# assume they're in main folder (fastcvapp/fastcvapp) trying `python examples/example_backgroundsubtraction.py`
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
else:
import pathlib
solution = []
#check all paths in sys.path and find the fastcvapp folder that holds FastCVapp.py
for pathstr in sys.path+[os.getcwd()]:
pathoption = list(pathlib.Path(pathstr).rglob(os.path.join("FastCVApp", "FastCVApp.py")))
testfilter = [pathselection for pathselection in pathoption]
if pathoption != []:
# solution = list(pathlib.Path(pathstr).rglob("FastCVApp.py"))[0].resolve().__str__()
# solution.append(*testfilter)
solution += testfilter
# print("sol??", solution)
# solution = [print("strvar", strvar) for strvar in solution]
solution = [os.path.dirname(pathobj) for pathobj in solution]
if len(solution) != 1:
#warn user if multiple paths detected or none:
print("there should only be one path to FastCVApp/FastCVApp.py, check your env", solution, flush=True)
for solutionitem in solution:
sys.path.append(solutionitem)
# print("appended solution!",sys.path)
try:
import pathlib
#in pyinstaller
if hasattr(sys, "_MEIPASS"):
#using fcva as a project
if len(list(pathlib.Path(pathstr).rglob(os.path.join("fcvautils.py")))) > 0:
from fcvautils import FCVA_update_resources
else:
#importing using fastcvapp as a module
from fastcvapp.fcvautils import FCVA_update_resources
else:
# NOT in pyinstaller, using fcva as a module
from fastcvapp.fcvautils import FCVA_update_resources
except:
#importing from fastcvapp project (using fcva as a project)
from fcvautils import FCVA_update_resources
#udpate paths here
# FCVA_update_resources(sourcelocationVAR = ["examples", "creativecommonsmedia", "Elephants Dream charstart2FULL_265.mp4"], destlocationVAR = ["examples"]) #this has the sys.path.append(sys._MEIPASS)
FCVA_update_resources(sourcelocationVAR = ["examples"], destlocationVAR = ["examples"]) #this has the sys.path.append(sys._MEIPASS)
import cv2
from collections import deque
def canny_filter(*args):
try:
# reference: https://docs.opencv.org/4.x/da/d5c/tutorial_canny_detector.html
inputdeque = args[0]
# FCVAWidget_shared_metadata_dictVAR3 = args[1]
bufferlenVAR = args[2]
answerdeque = deque(maxlen=bufferlenVAR)
landmarkerVAR = args[3]
raw_dequeKEYSVAR = args[4]
force_monotonic_increasingVAR = args[5]
low_threshold = 50
ratio = 3
kernel_size = 3
while len(inputdeque) > 0:
image = inputdeque.popleft()
src_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_blur = cv2.blur(src_gray, (3, 3))
detected_edges = cv2.Canny(
img_blur, low_threshold, low_threshold * ratio, kernel_size
)
mask = detected_edges != 0
image = image * (mask[:, :, None].astype(image.dtype))
answerdeque.append(image)
return answerdeque
except Exception as e:
print("canny_filter subprocess died! ", e, flush=True)
if __name__ == "__main__":
import multiprocessing
multiprocessing.freeze_support()
import fastcvapp
try:
#as a module
# print("paths??", sys.path)
app = fastcvapp.FCVA()
except:
#in the project
# print("listdir", os.listdir(os.path.dirname(fastcvapp.__file__)))
from fastcvapp.fastcvapp import FCVA
app = FCVA()
app.appliedcv = canny_filter
# / and \ works on windows, only / on mac tho
app.source = sourcelocation
app.title = "Canny edge example by Pengindoramu"
app.run()
except Exception as e:
print("example_cannyedge died!", e)
import traceback
print("full exception", "".join(traceback.format_exception(*sys.exc_info())))
import time
time.sleep(300)