1
0
mirror of https://github.com/opencv/opencv_contrib.git synced 2025-10-19 19:44:14 +08:00
Files
opencv_contrib/modules/dnn_objdetect/scripts/pascal_preprocess.py
Kv Manohar 41a5a5eaf5 Merge pull request #1253 from kvmanohar22:GSoC17_dnn_objdetect
GSoC'17 Learning compact models for object detection (#1253)

* Final solver and model for SqueezeNet model

* update README

* update dependencies and CMakeLists

* add global pooling

* Add training scripts

* fix typo

* fix dependency of caffe

* fix whitespace

* Add squeezedet architecture

* Pascal pre process script

* Adding pre process scripts

* Generate the graph of the model

* more readable

* fix some bugs in the graph

* Post process class implementation

* Complete minimal post processing and standalone running

* Complete the base class

* remove c++11 features and fix bugs

* Complete example

* fix bugs

* Adding final scripts

* Classification scripts

* Update README.md

* Add example code and results

* Update README.md

* Re-order and fix some bugs

* fix build failure

* Document classes and functions

* Add instructions on how to use samples

* update instructionos

* fix docs failure

* fix conversion types

* fix type conversion warning

* Change examples to sample directoryu

* restructure directories

* add more references

* fix whitespace

* retain aspect ratio

* Add more examples

* fix docs warnings

* update with links to trained weights

* threshold update

* png -> jpg

* fix tutorial

* model files

* precomp.hpp , fix readme links, module dependencies

* copyrights

- no copyright in samples
- use new style OpenCV copyright header
- precomp.hpp
2018-01-29 12:08:32 +03:00

48 lines
1.6 KiB
Python

from skimage import io, transform
from multiprocessing.dummy import Pool as ThreadPool
def rescale(root_new, root_old, img_path, ann_path, out_shape):
try:
img = io.imread(root_old+"/"+img_path)
except Exception as E:
print E
h, w, _ = img.shape
f_h, f_w = float(out_shape)/h, float(out_shape)/w
trans_img = transform.rescale(img, (f_h, f_w))
num_objs = 0
with open(root_old+"/"+ann_path, 'r') as f:
ann = f.readline()
ann = ann.rstrip()
ann = ann.split(' ')
ann = [float(i) for i in ann]
num_objs = len(ann) / 5
for idx in xrange(num_objs):
ann[idx * 5 + 0] = int(f_w * ann[idx * 5 + 0])
ann[idx * 5 + 1] = int(f_h * ann[idx * 5 + 1])
ann[idx * 5 + 2] = int(f_w * ann[idx * 5 + 2])
ann[idx * 5 + 3] = int(f_h * ann[idx * 5 + 3])
# Write the new annotations to file
with open(root_new+"/"+ann_path, 'w') as f_new:
for val in ann:
f_new.write(str(val)+' ')
# Save the new image
io.imwrite(root_new+"/"+img_path, trans_img)
def preprocess():
source = '/users2/Datasets/PASCAL_VOC/VOCdevkit/VOC2012_Resize/source.txt'
root_old = '/users2/Datasets/PASCAL_VOC/VOCdevkit/VOC2012'
root_new = '/users2/Datasets/PASCAL_VOC/VOCdevkit/VOC2012_Resize'
out_shape = 416
with open(source, 'r') as src:
lines = src.readlines()
print 'Processing {} images and annotations'.format(len(lines))
for line in lines:
line = line.rstrip()
line = line.split(' ')
img_path = line[0]
ann_path = line[1]
rescale(root_new, root_old, img_path, ann_path, out_shape)
if __name__ == '__main__':
preprocess()