-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimg_processing.py
More file actions
124 lines (103 loc) · 3.48 KB
/
img_processing.py
File metadata and controls
124 lines (103 loc) · 3.48 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sys
#sys.path.append('/usr/local/Cellar/opencv3/3.2.0/lib/python2.7/site-packages')
sys.path.append("/usr/local/Cellar/opencv3/3.2.0/lib/python3.5/site-packages")
import cv2
import numpy as np
import os
import random
def show_image(im):
height, width = im.shape[:2]
res = cv2.resize(im,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
cv2.imshow("Image", res)
def show_imageOrig(im):
height, width = im.shape[:2]
res = cv2.resize(im,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
cv2.imshow("ImageOrig", res)
#kernel = np.ones((3,3),np.uint8)
#cap = cv2.VideoCapture("dogs.mp4")
#ret, frame1 = cap.read()
#prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
#hsv = np.zeros_like(frame1)
#hsv[...,1] = 255
#fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)
#fgbg.setBackgroundRatio(0.8) # frames before object becomes foreground
#fgbg.setVarInit(500) # speed of adaption of new components
#i = 0
#while(1):
# ret, frame2 = cap.read()
# ret, frame2 = cap.read()
# frame2 = cv2.GaussianBlur(frame2,(9,9),0)
# fgmask = fgbg.apply(frame2)
# fgmask = fgbg.apply(frame2,fgmask, 0)
#fgmask = cv2.dilate(fgmask,kernel,iterations = 5)
#fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)
'''
^^ Line above may or may not be good
'''
#if (i > 10 and i % 2 == 0):
#cv2.imwrite(str(i) + ".png",fgmask)
# show_image(fgmask)
# k = cv2.waitKey(30) & 0xff
# if k == 27:
# break
#i += 1
#cap.release()
#cv2.destroyAllWindows()
#errorCount = 0
np.random.seed(133)
numLabels = 101
image_size_x = 240
image_size_y = 320
dataRoot = "./UCF-101/"
def processFolder(folder):
#tick = 0
#global errorCount
print(dataRoot + folder)
try:
videoFileNames = os.listdir(dataRoot + folder)
except:
print("Not a directory, moving along.")
return None, None
#i = 0
#data = np.zeros(shape=(len(videoFileNames)*1, image_size_x, image_size_y), dtype=np.float32)
#labels = np.zeros(shape=(len(videoFileNames)*1, 101), dtype=np.float32)
for videoName in videoFileNames:
#if tick < 2:
# tick = tick + 1
# continue
#tick = 0
if random.random() < 0.98:
continue
try:
print(videoName)
cap = cv2.VideoCapture(dataRoot + folder + "/" + videoName)
#ret, frame1 = cap.read()
#prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
#hsv = np.zeros_like(frame1)
#hsv[...,1] = 255
fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)
fgbg.setBackgroundRatio(0.8) # frames before object becomes foreground
fgbg.setVarInit(500) # speed of adaption of new components
i = 0
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
while(cap.get(cv2.CAP_PROP_POS_FRAMES) < frames - 3):
#ret, frame2 = cap.read()
ret, frame2 = cap.read()
if ret == False:
continue
show_imageOrig(frame2)
frame2 = cv2.GaussianBlur(frame2,(9,9),0)
fgmask = fgbg.apply(frame2)
fgmask = fgbg.apply(frame2,fgmask, 0)
show_image(fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
except IOError as e:
print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
#return data, labels
def iterData(folder):
labelNames = os.listdir(folder)
for i in range(len(labelNames)):#len(labelNames)
processFolder(labelNames[i])
iterData(dataRoot)