-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.py
197 lines (162 loc) · 5.08 KB
/
processor.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
190
191
192
193
194
195
196
197
#!/bin/usr/python3 -B
# -*- coding: utf-8 -*-
'''This is the frontend for an experimental version of the analysis
Run on a normal computer, not the RasPi. If you choose to run this
on the RasPi, make sure that USEGUI in ./__init__.py is False.
'''
from __future__ import print_function
#import argparse
#import csv
#from datetime import datetime
#import itertools
#import math
import os
#import pickle
import sys
#import time
#import ephem
#import numpy as np
#from scipy import stats
import cv2
from LunCV import Manipulations, RingBuffer
#kalman = cv2.KalmanFilter(2, 1, 0)
#state = 0.1 * np.random.randn(2, 1)
#kalman.transitionMatrix = np.array([[1., 1.], [0., 1.]])
#kalman.measurementMatrix = 1. * np.ones((1, 2))
#kalman.processNoiseCov = 1e-5 * np.eye(2)
#kalman.measurementNoiseCov = 1e-1 * np.ones((1, 1))
#kalman.errorCovPost = 1. * np.ones((2, 2))
#kalman.statePost = 0.1 * np.random.randn(2, 1)
def main(the_file, gui, pos_frame, procpath):
'''main function
'''
firstframe = pos_frame
file_datetime = str(the_file)
file_datetime = (file_datetime.split('/')[-1].split('outA.'))[0]
lcv = Manipulations.Manipulations()
rbf = RingBuffer.RingBufferClass(procpath)
while True:
#while pos_frame < 1050:
rbf.set_pos_frame(pos_frame)
print(pos_frame)
cap = cv2.VideoCapture(the_file)
#cap = cv2.VideoCapture('/scratch/whoneyc/1535181028stabilized.mp4')
#cap = cv2.VideoCapture('/home/wes/Pictures/Demobird/videoout.mp4')
cap.set(cv2.CAP_PROP_POS_FRAMES, pos_frame)
ret, frame = cap.read()
if ret:
#Necessary cleanup of rbf class
rbf.re_init()
# Make image gray
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Blur Image
frame = cv2.GaussianBlur(frame, (5, 5), 0)
# Get thresholds
lower_thresh, upper_thresh = lcv.magic_thresh(frame)
# Get contours
contours = lcv.cv_contour(frame, lower_thresh, upper_thresh)
# Center Moon
rett, ellipse, frame = lcv.center_moon(frame, contours)
if rett:
with open(procpath + '/outputellipse.csv', 'a') as fff:
outstring = str(pos_frame) + ',' + str(ellipse[0][0]) + ',' + \
str(ellipse[0][1]) + ',' + str(ellipse[1][0]) + ',' + str(ellipse[1][1])\
+ ',' + str(ellipse[2]) + '\n'
fff.write(outstring)
# Subtract Background
img = lcv.subtract_background(frame)
temp = img
# Remove Halo Noise
img = lcv.halo_noise(ellipse, img)
# Get Contours Again
contours = lcv.cv_contour(img, 0, 255)
if contours:
rbf.get_centers(contours)
##Info for debugging
#with open('/scratch/whoneyc/contours_minus_cont_0.p', 'wb') as fff:
#pickle.dump(contours, fff)
# Dirty conversion to binary b/w
img[img > 0] = 1
# Deal with ringbuffer on frames
rbf.ringbuffer_cycle(img)
if pos_frame - 5 > firstframe:
img = rbf.ringbuffer_process(img)
goodlist = rbf.pull_list()
# Number of contours limiter
if goodlist.size > 0 and goodlist.size < 300:
img = rbf.bird_range(img, frame, temp, goodlist)
if gui:
#img[img < 0] = 255
cv2.imshow('image', img)
cv2.waitKey(1)
else:
break
pos_frame += 1
def is_valid_file(parser, arg):
'''
Check if arg is a valid file that already exists on the file system.
Parameters
----------
parser : argparse object
arg : str
Returns
-------
arg
'''
arg = os.path.abspath(arg)
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
sys.exit(1)
else:
return arg
def get_parser():
'''Get parser object for script processor.py
'''
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
parser = ArgumentParser(description=__doc__, formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-f", "--file", dest="filename", required=True,\
type=lambda x: is_valid_file(parser, x), help="write report to FILE", metavar="FILE")
parser.add_argument("-g", "--gui", dest="gui", action="store_true", default=False,\
help="show the slides as you are processing them.")
parser.add_argument("-n", "--nthframe", dest="pos_frame", type=int, default=0,\
help="set starting frame number; defaults 0\n *NOTE: This changes the basis set/early results!")
#parser.add_argument("-q", "--quiet",
#action="store_false",
#dest="verbose",
#default=True,
#help="don't print status messages to stdout")
return parser
def read_proc_number():
'''Look for deleteme file.
If it exists, parse it for the number we need
Else, die
'''
arg = os.path.abspath("./deleteme")
if not os.path.exists(arg):
print("the deleteme file does not exist")
sys.exit(1)
else:
with open("./deleteme", 'r') as fff:
procpath = fff.read()
working = True
addon = 1
while working:
if os.path.exists(procpath):
procpath = procpath + "_" + str(addon)
addon += 1
else:
working = False
return procpath
if __name__ == '__main__':
ARGS = get_parser().parse_args()
print(ARGS.filename)
try:
main(ARGS.filename, ARGS.gui, ARGS.pos_frame, read_proc_number())
except KeyboardInterrupt:
print("keyboard task kill")
finally:
cv2.destroyAllWindows()
try:
os.remove("./deleteme")
except FileNotFoundError:
pass