Skip to content

Commit

Permalink
migrate to PIL for image operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen490 committed Sep 8, 2020
1 parent bdf733d commit 98aca05
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ python test.py --dataset university --detection_threshold 0.99 --viz
3. matplotlib
4. h5py
5. tensorflow
6. [Deeplab V3](https://github.com/sthalles/deeplab_v3)
6. PIL
7. scikit-learn
8. [Deeplab V3](https://github.com/sthalles/deeplab_v3)

## Examples

Expand Down
14 changes: 8 additions & 6 deletions annotate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import matplotlib.pyplot as plt
import numpy
import scipy.misc
from PIL import Image
from sklearn.cluster import KMeans
import sys
import os

dataset = 'beach'
margin = 10
Expand All @@ -18,13 +19,14 @@

def next_img():
global image_np, image_display, labels, img_id, segment_id
try:
image_np = scipy.misc.imread('dataset/%s/%d.png'%(dataset,img_id))
except IOError:
image_filename = 'dataset/%s/%d.png'%(dataset,img_id)
if os.path.exists(image_filename):
image_np = numpy.array(Image.open(image_filename))
else:
sys.exit(0)
image_display = image_np.copy()
image_np = numpy.mean(image_np, axis=2)
#print(image_np.shape, image_display.shape)
# print(image_np.shape, image_display.shape)
labels = numpy.zeros(image_np.shape, dtype=int)
segment_id=0
plt.imshow(image_display)
Expand All @@ -35,7 +37,7 @@ def next_img():
def onkey(event):
if event.key==' ':
global img_id
scipy.misc.imsave('dataset/%s/label%d.png'%(dataset,img_id), labels)
Image.fromarray(labels, mode='L').save('dataset/%s/label%d.png'%(dataset,img_id))
img_id += 1
next_img()
elif event.key=='r':
Expand Down
20 changes: 11 additions & 9 deletions baselines.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import matplotlib.pyplot as plt
import numpy
import scipy.misc
from PIL import Image
import sys
import cv2
import time
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
import glob
import os

method = 'threshold'
#method = 'threshold_adp'
Expand Down Expand Up @@ -44,13 +45,14 @@
if method!='backSub' and image_id < test_idx:
image_id += 1
continue
try:
I = scipy.misc.imread('dataset/%s/%d.png' % (dataset,image_id))
image_filename = 'dataset/%s/%d.png' % (dataset,image_id)
if os.path.exists(image_filename):
I = numpy.array(Image.open(image_filename))
if len(I.shape)>2:
I = numpy.mean(I, axis=2)
except IOError:
else:
break
gt = scipy.misc.imread('dataset/%s/label%d.png' % (dataset, image_id))
gt = numpy.array(Image.open('dataset/%s/label%d.png'%(dataset,image_id)))
gt = gt > 0
dt = numpy.zeros(I.shape, dtype=bool)
image_np = I[ybound:ybound+imscale, xbound:xbound+imscale]
Expand All @@ -71,7 +73,7 @@
if dataset=='combined' and image_id in [97, 225, 249]:
backSub = cv2.createBackgroundSubtractorMOG2()
mask = backSub.apply(image_np)
scipy.misc.imsave('dataset/%s/backSub/%d.png'%(dataset,image_id), mask.astype(numpy.uint8))
Image.fromarray(mask.astype(numpy.uint8), mode='L').save('dataset/%s/backSub/%d.png'%(dataset,image_id))
elif method=='kmeans':
window_size = 15 if dataset=='beach' or dataset=='shore' else 100
margin = 10 if dataset=='beach' or dataset=='shore' else 100
Expand Down Expand Up @@ -227,9 +229,9 @@
# print('Image #%d Precision:%.2f/%.2f Recall:%.2f/%.2f (%.2fs)'%(image_id, prc,obj_prc,rcl,obj_rcl, t2-t1))

if image_id == save_frame:
scipy.misc.imsave('results/original_%d.png'%save_frame, image_np.astype(numpy.uint8))
scipy.misc.imsave('results/detected_%s_%d.png'%(method,save_frame), dt_viz)
scipy.misc.imsave('results/ground_truth_%d.png'%save_frame, gt_viz)
Image.fromarray(image_np.astype(numpy.uint8), mode='L').save('results/original_%d.png'%save_frame)
Image.fromarray(dt_viz, mode='RGB').save('results/detected_%s_%d.png'%(method, save_frame))
Image.fromarray(gt_viz, mode='RGB').save('results/ground_truth_%d.png'%save_frame)
print('save_frame',save_frame)
sys.exit(1)
if viz:
Expand Down
12 changes: 6 additions & 6 deletions process_record.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy
import scipy.misc
from PIL import Image
import h5py
import glob
import sys
Expand Down Expand Up @@ -34,29 +34,29 @@
previous_img = None

for i in range(1,num_samples+1):
image_np = scipy.misc.imread('dataset/%s/%d.png'%(dataset,i),mode='RGB')
image_np = numpy.array(Image.open('dataset/%s/%d.png'%(dataset,i)))
image_np = image_np[ybound:ybound+imscale,xbound:xbound+imscale].mean(axis=2)
if previous_img is None:
diff_img = numpy.zeros(image_np.shape, dtype=numpy.uint8)
backSub_img = numpy.zeros(image_np.shape, dtype=numpy.uint8)
else:
diff_img = ((image_np - previous_img)/2 + 128).astype(numpy.uint8)
if use_history:
backSub_img = scipy.misc.imread('dataset/%s/backSub/%d.png'%(dataset,i),mode='RGB').mean(axis=2)
backSub_img = numpy.array(Image.open('dataset/%s/backSub/%d.png'%(dataset,i))).mean(axis=2)
previous_img = image_np
image_h = image_np.shape[0]
image_w = image_np.shape[1]
if use_history:
image_np = numpy.dstack((image_np, diff_img, backSub_img)).astype(numpy.uint8)
else:
image_np = numpy.dstack((image_np, image_np, image_np)).astype(numpy.uint8)
annotation = scipy.misc.imread('dataset/%s/label%d.png'%(dataset,i), mode='L')
annotation = numpy.array(Image.open('dataset/%s/label%d.png'%(dataset,i)))
for p in numpy.array(numpy.nonzero(annotation)).T:
pos_idx.add(tuple(p))
annotation = annotation[ybound:ybound+imscale,xbound:xbound+imscale]
if imscale!=imsize:
image_np = scipy.misc.imresize(image_np, size=(imsize, imsize), interp='bilinear')
annotation = scipy.misc.imresize(annotation, size=(imsize, imsize), interp='bilinear')
image_np = numpy.array(Image.fromarray(image_np).resize((imsize, imsize), resample=Image.BILINEAR))
annotation = numpy.array(Image.fromarray(annotation).resize((imsize, imsize), resample=Image.BILINEAR))
annotation = numpy.array(annotation > 0, dtype=numpy.uint8)
print(i,image_np.shape,image_np.dtype)
if i-1 in train_samples:
Expand Down
21 changes: 11 additions & 10 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import sklearn.metrics
import glob
from PIL import Image

parser = argparse.ArgumentParser()
envarg = parser.add_argument_group('Training params')
Expand Down Expand Up @@ -108,28 +109,28 @@ def __init__(self):
previous_img = None
for image_idx in range(1,num_samples+1):
t1 = time.time()
original_image = scipy.misc.imread('dataset/%s/%d.png'%(args.dataset,image_idx),mode='RGB')
original_image = numpy.array(Image.open('dataset/%s/%d.png'%(args.dataset,image_idx)))
image_np = original_image[ybound:ybound+imscale,xbound:xbound+imscale].mean(axis=2)
if args.use_history:
if previous_img is None:
diff_img = numpy.zeros(image_np.shape, dtype=numpy.uint8)
backSub_img = numpy.zeros(image_np.shape, dtype=numpy.uint8)
else:
diff_img = ((image_np - previous_img)/2 + 128).astype(numpy.uint8)
backSub_img = scipy.misc.imread('dataset/%s/backSub/%d.png'%(args.dataset,image_idx),mode='RGB').mean(axis=2)
backSub_img = numpy.array(Image.open('dataset/%s/backSub/%d.png'%(args.dataset,image_idx))).mean(axis=2)
previous_img = image_np
if image_idx < test_idx:
continue
if args.use_history:
image_np = numpy.dstack((image_np, diff_img, backSub_img)).astype(numpy.uint8)
else:
image_np = numpy.dstack((image_np, image_np, image_np)).astype(numpy.uint8)
original_annotation = scipy.misc.imread('dataset/%s/label%d.png'%(args.dataset,image_idx), mode='L')
original_annotation = numpy.array(Image.open('dataset/%s/label%d.png'%(args.dataset,image_idx)))
annotation = original_annotation[ybound:ybound+imscale,xbound:xbound+imscale]
annotation = numpy.array(annotation > 0, dtype=numpy.uint8)
if imsize!=imscale:
input_images[:] = scipy.misc.imresize(image_np, size=(imsize, imsize), interp='bilinear')
input_labels[:] = scipy.misc.imresize(annotation, size=(imsize, imsize), interp='bilinear')
input_images[:] = numpy.array(Image.fromarray(image_np).resize((imsize, imsize), resample=Image.BILINEAR))
input_labels[:] = numpy.array(Image.fromarray(annotation).resize((imsize, imsize), resample=Image.BILINEAR))
else:
input_images[:] = image_np
input_labels[:] = annotation
Expand All @@ -142,7 +143,7 @@ def __init__(self):
# print('Loss %.2f(%.2f+%.2f) tp:%d fp:%d fn:%d prc:%.2f rcl:%.2f'%(ls,pl,nl,vtp,vfp,vfn,prc,rcl))
predicted_softmax = scipy.special.softmax(result[0,:,:,:],axis=-1)[:,:,1]
if imsize!=imscale:
predicted_softmax = scipy.misc.imresize(predicted_softmax, size=(imscale, imscale), interp='bilinear') / 255.0
predicted_softmax = numpy.array(Image.fromarray(predicted_softmax, mode='F').resize((imscale, imscale), resample=Image.BILINEAR))
ap_dt.extend(predicted_softmax.flatten())
ap_gt.extend(annotation.flatten())
predicted = predicted_softmax > detection_threshold
Expand Down Expand Up @@ -222,12 +223,12 @@ def __init__(self):
dt_viz[y1:y2, x2, :] = [255,255,0]

comp_time.append(t2 - t1)
# print('%d/%d images prc %.2f/%.2f rcl %.2f/%.2f (%.2fs)'%(image_idx,num_samples,prc,obj_prc,rcl,obj_rcl,t2-t1))
print('%d/%d images prc %.2f/%.2f rcl %.2f/%.2f (%.2fs)'%(image_idx,num_samples,prc,obj_prc,rcl,obj_rcl,t2-t1))

if image_idx == args.save_frame:
scipy.misc.imsave('results/original_%d.png'%args.save_frame, image_np[:,:,0].astype(numpy.uint8))
scipy.misc.imsave('results/detected_%s_%d.png'%('history' if args.use_history else 'cnn', args.save_frame), dt_viz)
scipy.misc.imsave('results/ground_truth_%d.png'%args.save_frame, gt_viz)
Image.fromarray(image_np[:,:,0].astype(numpy.uint8), mode='L').save('results/original_%d.png'%args.save_frame)
Image.fromarray(dt_viz, mode='RGB').save('results/detected_%s_%d.png'%('history' if args.use_history else 'cnn', args.save_frame))
Image.fromarray(gt_viz, mode='RGB').save('results/ground_truth_%d.png'%args.save_frame)
print('save_frame',args.save_frame)
sys.exit(1)
if viz:
Expand Down

0 comments on commit 98aca05

Please sign in to comment.