-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing_code.py
203 lines (140 loc) · 5.19 KB
/
testing_code.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
198
199
200
201
202
203
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import cv2
import sys
import h5py
import argparse
import numpy as np
from glob import glob
from tqdm import tqdm
import matplotlib.pyplot as plt
np.set_printoptions(threshold=sys.maxsize)
"""
This script stores image masks from a directory in a compressed hdf5 file.
Example:
$ python masks2hdf5.py dataset/subject/masks masks.hdf5
"""
# masks = h5py.File("../male/keypoints.hdf5", 'r')['keypoints']
# num_frames = masks.shape[0]
# keypoints = masks[0]
# count = 0
# blank_image = np.zeros((1300,1300,3), np.uint8)
# blank_image[:] = (0,0,0)
# for point in range(0, len(keypoints), 3):
# print(point)
# center = (int(keypoints[point]), int(keypoints[point+1]))
# color = (0,0,255)
# cv2.putText(blank_image, str(count), center, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1, cv2.LINE_AA)
# count += 1
# cv2.imwrite("image.jpg", blank_image)
# print(num_frames)
# print(len(keypoints))
# masks = h5py.File("../new/masks.hdf5", 'r')['masks']
# num_frames = masks.shape[0]
# keypoints = masks[0]
# blank_image = np.zeros((1080,1080,3), np.uint8)
# print(keypoints[0])
# #blank_image[:] = keypoints[:]
# #cv2.imwrite("image.jpg", blank_image)
# print(keypoints.shape)
# print(len(keypoints))
# #define the vertical filter
# vertical_filter = [[-1,-2,-1], [0,0,0], [1,2,1]]
# #define the horizontal filter
# horizontal_filter = [[-1,0,1], [-2,0,2], [-1,0,1]]
# #read in the pinwheel image
# img = plt.imread("../male/images/156.jpg")
# #get the dimensions of the image
# n,m,d = img.shape
# #initialize the edges image
# edges_img = img.copy()
# #loop over all pixels in the image
# for row in range(3, n-2):
# for col in range(3, m-2):
# #create little local 3x3 box
# local_pixels = img[row-1:row+2, col-1:col+2, 0]
# #apply the vertical filter
# vertical_transformed_pixels = vertical_filter*local_pixels
# #remap the vertical score
# vertical_score = vertical_transformed_pixels.sum()/4
# #apply the horizontal filter
# horizontal_transformed_pixels = horizontal_filter*local_pixels
# #remap the horizontal score
# horizontal_score = horizontal_transformed_pixels.sum()/4
# #combine the horizontal and vertical scores into a total edge score
# edge_score = (vertical_score**2 + horizontal_score**2)**.5
# #insert this edge score into the edges image
# edges_img[row, col] = [edge_score]*3
# #remap the values in the 0-1 range in case they went out of bounds
# edges_img = edges_img/edges_img.max()
# cv2.imwrite("testing3.jpg", edges_img)
# imgplot = plt.imshow(edges_img)
# plt.show()
# exit()
masks = h5py.File("../male2/masks.hdf5", 'r')['masks']
num_frames = masks.shape[0]
keypoints = masks[0]
blank_image = np.zeros((1080,1080), np.uint8)
keypoints[keypoints == 0] = 100
blank_image[:] = keypoints[:]
cv2.imwrite("image.jpg", blank_image)
# # print(keypoints.shape)
# # print(len(keypoints))
# keypoint = np.copy(keypoints)
# #print(type(keypoint))
# keypoint[keypoint == 0] = 100
# #print(keypoint[579])
# # img = cv2.imread("../male/images/156.jpg") #load rgb image
# # hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #convert it to hsv
# # hsv[:,:,2] += 30
# # img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
# # cv2.imwrite("image_processed.jpg", img)
# # exit()
# cv2.imwrite("testing.jpg", keypoint)
# print("here")
# exit()
# with h5py.File("test.hdf5", 'w') as f:
# dset = f.create_dataset("masks", (2, 1080, 1080), 'b', chunks=True, compression="lzf")
# # silh = cv2.imread("../male/images/156.png", cv2.IMREAD_GRAYSCALE)
# # #print(silh[579])
# # a, silh = cv2.threshold(silh, 93, 255, cv2.THRESH_BINARY)
# # dset[0] = silh.astype(np.bool)
# # #print(dset[0][579])
# # keypoint = np.copy(dset[0])
# # keypoint[keypoint == 0] = 100
# # cv2.imwrite("testing3.png", keypoint)
# silh = cv2.imread("../male2/images/156.jpg")
# for i in range(1080):
# for j in range(1080):
# if silh[i][j][0] < 80 and silh[i][j][0] > 28 \
# and silh[i][j][1] > 75 and silh[i][j][1] < 130 \
# and silh[i][j][2] < 80 and silh[i][j][2] > 28:
# silh[i][j][0] = 0
# silh[i][j][1] = 0
# silh[i][j][2] = 0
# cv2.imwrite("testing3.png", silh)
# #print(silh[100])
silh = cv2.imread("mask_data/0.jpg", cv2.IMREAD_GRAYSCALE)
a, silh = cv2.threshold(silh, 10, 255, cv2.THRESH_BINARY)
silh[silh == 0] = 100
silh[silh == 255] = 0
cv2.imwrite("0.jpg", silh)
print(silh[500])
exit()
parser = argparse.ArgumentParser()
parser.add_argument('src', type=str)
parser.add_argument('target', type=str)
args = parser.parse_args()
out_file = args.target
mask_dir = args.src
mask_files = sorted(glob(os.path.join(mask_dir, '*.png')) + glob(os.path.join(mask_dir, '*.jpg')))
with h5py.File(out_file, 'w') as f:
dset = None
for i, silh_file in enumerate(tqdm(mask_files)):
silh = cv2.imread(silh_file, cv2.IMREAD_GRAYSCALE)
if dset is None:
dset = f.create_dataset("masks", (len(mask_files), silh.shape[0], silh.shape[1]), 'b', chunks=True, compression="lzf")
_, silh = cv2.threshold(silh, 100, 255, cv2.THRESH_BINARY)
dset[i] = silh.astype(np.bool)
#print(dset[i])