Skip to content

Commit

Permalink
Merge pull request #181 from alexlib/python3
Browse files Browse the repository at this point in the history
Python 3 compatibility
  • Loading branch information
alexlib authored Apr 18, 2019
2 parents 84742d0 + e640780 commit 0e75f36
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 104 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ py_bind/optv/*.c
py_bind/dist
py_bind/build
py_bind/liboptv
py_bind/optv.egg-info/

liboptv/config.h.in~
liboptv/tests/check_fb.trs
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ before_install:

install:
- conda update --yes conda
- conda create -n testenv --yes $DEPS numpy cython pyyaml nose python=$TRAVIS_PYTHON_VERSION
- conda create -n testenv --yes $DEPS numpy cython pyyaml nose six python=$TRAVIS_PYTHON_VERSION
- source activate testenv

# for debugging...
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions docker/Dockerfile.python3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# A Dockerfile for an Ubuntu machine that can compile liboptv and the Cython extensions

FROM python:3.7-stretch

RUN apt-get update
RUN apt-get --assume-yes install cmake
RUN apt-get --assume-yes install g++
# RUN apt-get --assume-yes install python-pip
# RUN apt-get --assume-yes install python3
# RUN pip install virtualenv
RUN python3 -m venv /env
8 changes: 5 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Set up the Ubuntu container so we can develop the Cython code on it
version: '3'
services:
optv-dev:
image: ptv-ubuntu:1
build: ./
optv-dev-python3:
image: ptv-ubuntu:python3
build:
context: ./
dockerfile: Dockerfile.python3
volumes:
- ../:/src
command: tail -f /dev/null
Expand Down
2 changes: 1 addition & 1 deletion py_bind/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Testing the installation
Regardless of the system you installed on, the shell commands are the same:

cd tests/
nosetests .
nosetests *

Usage example
-------------
Expand Down
3 changes: 1 addition & 2 deletions py_bind/optv/calibration.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import numpy
cimport numpy as cnp

from optv.calibration import Calibration
from mhlib import isnumeric

cdef extern from "optv/calibration.h":
calibration *read_calibration(char *ori_file, char *add_file,
Expand Down Expand Up @@ -160,7 +159,7 @@ cdef class Calibration:
of point from sensor middle and sensor-point distance, in this
order.
"""
if (object>prim_point_pos).shape != (3,):
if (<object>prim_point_pos).shape != (3,):
raise ValueError("Expected a 3-element array")

self._calibration[0].int_par.xh = prim_point_pos[0]
Expand Down
5 changes: 3 additions & 2 deletions py_bind/optv/image_processing.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from optv.parameters cimport ControlParams, control_par
import numpy as np
cimport numpy as np
from six import string_types

def preprocess_image(np.ndarray[ndim=2, dtype=np.uint8_t] input_img,
int filter_hp,
Expand Down Expand Up @@ -41,10 +42,10 @@ def preprocess_image(np.ndarray[ndim=2, dtype=np.uint8_t] input_img,
output_img = np.empty_like(input_img)

if filter_hp == 2:
if filter_file == None or not isinstance(filter_file, basestring):
if filter_file == None or not isinstance(filter_file, string_types):
raise ValueError("Expecting a filter file name, received None or non-string.")
else:
filter_file=""
filter_file=b""

for arr in (input_img, output_img):
if not arr.flags['C_CONTIGUOUS']:
Expand Down
6 changes: 3 additions & 3 deletions py_bind/optv/tracker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ from optv.orientation cimport cal_list2arr
from optv.tracking_framebuf cimport fb_free

default_naming = {
'corres': 'res/rt_is',
'linkage': 'res/ptv_is',
'prio': 'res/added'
'corres': b'res/rt_is',
'linkage': b'res/ptv_is',
'prio': b'res/added'
}

cdef class Tracker:
Expand Down
7 changes: 4 additions & 3 deletions py_bind/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Cython==0.29.5
Cython==0.29.6
nose==1.3.7
numpy==1.16.1
PyYAML>=4.2b1
numpy==1.16.2
PyYAML==5.1
six
6 changes: 3 additions & 3 deletions py_bind/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def mk_ext(name, files):
package_data={
'optv': ['*.pxd', '*.c', '*.h'],
},
version='0.2.4',
version='0.2.5',
install_requires=[
'numpy==1.16.1',
'numpy>=1.16.1',
'pyyaml',
],
setup_requires=['numpy==1.16.1'],
setup_requires=['numpy>=1.16.1'],
)
10 changes: 5 additions & 5 deletions py_bind/test/test_calibration_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class Test_Calibration(unittest.TestCase):
def setUp(self):
self.input_ori_file_name = "testing_fodder/calibration/cam1.tif.ori"
self.input_add_file_name = "testing_fodder/calibration/cam2.tif.addpar"
self.output_directory = "testing_fodder/calibration/testing_output/"
self.input_ori_file_name = b"testing_fodder/calibration/cam1.tif.ori"
self.input_add_file_name = b"testing_fodder/calibration/cam2.tif.addpar"
self.output_directory = b"testing_fodder/calibration/testing_output/"

# create a temporary output directory (will be deleted by the end of test)
if not os.path.exists(self.output_directory):
Expand Down Expand Up @@ -37,8 +37,8 @@ def test_full_instantiate(self):

def test_Calibration_instantiation(self):
"""Filling a calibration object by reading ori files"""
self.output_ori_file_name = self.output_directory + "output_ori"
self.output_add_file_name = self.output_directory + "output_add"
self.output_ori_file_name = self.output_directory + b"output_ori"
self.output_add_file_name = self.output_directory + b"output_add"

# Using a round-trip test.
self.cal.from_file(self.input_ori_file_name, self.input_add_file_name)
Expand Down
33 changes: 20 additions & 13 deletions py_bind/test/test_corresp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def test_instantiate(self):
"""Creating a MatchedCoords object"""
cal = Calibration()
cpar = ControlParams(4)

cal.from_file(
"testing_fodder/calibration/cam1.tif.ori",
"testing_fodder/calibration/cam2.tif.addpar")
cpar.read_control_par("testing_fodder/corresp/control.par")
b"testing_fodder/calibration/cam1.tif.ori",
b"testing_fodder/calibration/cam2.tif.addpar")
cpar.read_control_par(b"testing_fodder/corresp/control.par")
targs = read_targets("testing_fodder/frame/cam1.", 333)

mc = MatchedCoords(targs, cpar, cal)
Expand All @@ -39,9 +39,9 @@ class TestCorresp(unittest.TestCase):
def test_full_corresp(self):
"""Full scene correspondences"""
cpar = ControlParams(4)
cpar.read_control_par(r"testing_fodder/corresp/control.par")
cpar.read_control_par(b"testing_fodder/corresp/control.par")
vpar = VolumeParams()
vpar.read_volume_par(r"testing_fodder/corresp/criteria.par")
vpar.read_volume_par(b"testing_fodder/corresp/criteria.par")

# Cameras are at so high angles that opposing cameras don't see each
# other in the normal air-glass-water setting.
Expand All @@ -54,8 +54,8 @@ def test_full_corresp(self):
for c in range(4):
cal = Calibration()
cal.from_file(
"testing_fodder/calibration/sym_cam%d.tif.ori" % (c + 1),
"testing_fodder/calibration/cam1.tif.addpar")
b"testing_fodder/calibration/sym_cam%d.tif.ori" % (c + 1),
b"testing_fodder/calibration/cam1.tif.addpar")
cals.append(cal)

# Generate test targets.
Expand Down Expand Up @@ -87,9 +87,9 @@ def test_full_corresp(self):
def test_single_cam_corresp(self):
"""Single camera correspondence"""
cpar = ControlParams(1)
cpar.read_control_par("testing_fodder/single_cam/parameters/ptv.par")
cpar.read_control_par(b"testing_fodder/single_cam/parameters/ptv.par")
vpar = VolumeParams()
vpar.read_volume_par("testing_fodder/single_cam/parameters/criteria.par")
vpar.read_volume_par(b"testing_fodder/single_cam/parameters/criteria.par")

# Cameras are at so high angles that opposing cameras don't see each
# other in the normal air-glass-water setting.
Expand All @@ -101,8 +101,8 @@ def test_single_cam_corresp(self):
corrected = []
cal = Calibration()
cal.from_file(
"testing_fodder/single_cam/calibration/cam_1.tif.ori",
"testing_fodder/single_cam/calibration/cam_1.tif.addpar")
b"testing_fodder/single_cam/calibration/cam_1.tif.ori",
b"testing_fodder/single_cam/calibration/cam_1.tif.addpar")
cals.append(cal)

# Generate test targets.
Expand All @@ -127,4 +127,11 @@ def test_single_cam_corresp(self):
_, _, num_targs = correspondences(
img_pts, corrected, cals, vpar, cpar)

self.failUnlessEqual(num_targs, 9)
self.failUnlessEqual(num_targs, 9)



if __name__ == "__main__":
import sys, os
print(os.path.abspath(os.curdir))
unittest.main()
12 changes: 6 additions & 6 deletions py_bind/test/test_epipolar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@

class TestEpipolarCurve(unittest.TestCase):
def test_two_cameras(self):
ori_tmpl = r'testing_fodder/calibration/sym_cam{cam_num}.tif.ori'
add_file = r'testing_fodder/calibration/cam1.tif.addpar'
ori_tmpl = "testing_fodder/calibration/sym_cam{cam_num}.tif.ori"
add_file = "testing_fodder/calibration/cam1.tif.addpar"

orig_cal = Calibration()
orig_cal.from_file(ori_tmpl.format(cam_num=1), add_file)
orig_cal.from_file(ori_tmpl.format(cam_num=1).encode(), add_file.encode())
proj_cal = Calibration()
proj_cal.from_file(ori_tmpl.format(cam_num=3), add_file)
proj_cal.from_file(ori_tmpl.format(cam_num=3).encode(), add_file.encode())

# reorient cams:
orig_cal.set_angles(np.r_[0., -np.pi/4., 0.])
proj_cal.set_angles(np.r_[0., 3*np.pi/4., 0.])

cpar = ControlParams(4)
cpar.read_control_par("testing_fodder/corresp/control.par")
cpar.read_control_par(b"testing_fodder/corresp/control.par")
sens_size = cpar.get_image_size()

vpar = VolumeParams()
vpar.read_volume_par("testing_fodder/corresp/criteria.par")
vpar.read_volume_par(b"testing_fodder/corresp/criteria.par")
vpar.set_Zmin_lay([-10, -10])
vpar.set_Zmax_lay([10, 10])

Expand Down
8 changes: 4 additions & 4 deletions py_bind/test/test_framebuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_sort_y(self):
def test_write_targets(self):
"""Round-trip test of writing targets."""
targs = read_targets("../../liboptv/tests/testing_fodder/sample_", 42)
targs.write("testing_fodder/round_trip.", 1)
targs.write(b"testing_fodder/round_trip.", 1)
tback = read_targets("testing_fodder/round_trip.", 1)

self.failUnlessEqual(len(targs), len(tback))
Expand All @@ -67,9 +67,9 @@ def tearDown(self):
class TestFrame(unittest.TestCase):
def test_read_frame(self):
"""reading a frame"""
targ_files = ["testing_fodder/frame/cam%d." % c for c in xrange(1, 5)]
frm = Frame(4, corres_file_base="testing_fodder/frame/rt_is",
linkage_file_base="testing_fodder/frame/ptv_is",
targ_files = ["testing_fodder/frame/cam%d.".encode() % c for c in range(1, 5)]
frm = Frame(4, corres_file_base=b"testing_fodder/frame/rt_is",
linkage_file_base=b"testing_fodder/frame/ptv_is",
target_file_base=targ_files, frame_num=333)

pos = frm.positions()
Expand Down
Loading

0 comments on commit 0e75f36

Please sign in to comment.