Skip to content

Commit

Permalink
Merge pull request #3 from ACornuIGN/camera
Browse files Browse the repository at this point in the history
Add reading and writing camera file
  • Loading branch information
ACornuIGN authored Dec 22, 2023
2 parents 97acdec + d009bfe commit 1c94db9
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 122 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ jobs:
- name: Analysing the code with pylint
run: |
pylint pink_lady.py src/reader/manage_reader.py src/reader/reader_opk.py src/shot.py src/worksite.py src/writer/writer.py
continue-on-error: true
pylint pink_lady.py src/reader/camera/reader_camera.py src/reader/orientation/manage_reader.py src/reader/orientation/reader_opk.py src/datastruct/shot.py src/datastruct/worksite.py src/datastruct/camera.py src/writer/writer_opk.py
- name: Analysing the code with flake8
run: |
flake8 --max-line-length 100 pink_lady.py src/reader/manage_reader.py src/reader/reader_opk.py src/shot.py src/worksite.py src/writer/writer.py
flake8 --max-line-length 100 pink_lady.py src/reader/camera/reader_camera.py src/reader/orientation/manage_reader.py src/reader/orientation/reader_opk.py src/datastruct/shot.py src/datastruct/worksite.py src/datastruct/camera.py src/writer/writer_opk.py
12 changes: 5 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: unitary_test

on:
push:
branches: [main]
pull_request:
branches:
- main

jobs:
test:
Expand All @@ -15,13 +15,11 @@ jobs:
steps:
- name: Checkout branch
uses: actions/checkout@v4

- name: Install dependencies
- name: Install environment
run: |
python -m pip install --upgrade pip
pip install pytest pytest-shutil
pip install -r requirements.txt
- name: Run tests with pytest
with:
environment-file: environment.yml
run: python3 -m pytest -s ./test -v
File renamed without changes.
1 change: 1 addition & 0 deletions ci/test_reader.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 -m pytest -s ./test/test_reader.py -v
1 change: 1 addition & 0 deletions ci/test_struct.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 -m pytest -s ./test/test_struct.py -v
1 change: 1 addition & 0 deletions ci/test_writer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 -m pytest -s ./test/test_writer.py -v
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: pink_lady
dependencies:
- python
- numpy
- pip
- pip:
- -r requirements.txt
36 changes: 23 additions & 13 deletions pink_lady.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@
pink lady launch module
"""
import argparse

from src.reader.manage_reader import reader_orientation
from src.writer.writer import to_opk
import importlib
from src.reader.orientation.manage_reader import reader_orientation
from src.reader.camera.reader_camera import read_camera

parser = argparse.ArgumentParser(description='photogrammetric site conversion'
+ ' and manipulation software')
parser.add_argument('-f', '--filepath',
type=str, default="", nargs=1,
type=str, default='', nargs=1,
help='File path of the workfile')
parser.add_argument('-skip', '--skip',
type=int, default=None, nargs=1,
help='Number of lines to be skipped before reading the file')
parser.add_argument('-w', '--writer',
type=str, choices=['opk'],
type=str, default='',
help='Worksite output file format')
parser.add_argument('-pr', '--pathreturn',
type=str, default="test/tmp/", nargs=1,
type=str, default='test/tmp/', nargs=1,
help='Conversion path ex:"test/tmp/"')
parser.add_argument('-c', '--camera',
type=str, default='', nargs='*',
help='Files paths of cameras')

args = parser.parse_args()


if args.filepath[0] != "":
# Readind data
if args.filepath[0] != '':
work = reader_orientation(args.filepath[0], args.skip)
print("File reading done")
print("Orientation file reading done")
else:
print("The access road to the photogrammetric site is missing")

print(args.writer)
# Reading camera file
if args.camera != '':
read_camera(args.camera, work)
print("Camera file reading done")

if args.writer == 'opk':
to_opk(args.pathreturn[0], work)
print('file save well')
# Writing data
if args.writer != '':
try:
my_module = importlib.import_module("src.writer.writer_" + args.writer.lower())
work = my_module.write(args.pathreturn, work)
except ModuleNotFoundError as e:
raise ValueError(f"{args.writer} file is not taken into account !!!") from e
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
numpy==1.26.2
pylint==0.3.3
pytest==7.43
pytest-shutil==1.7.0
flake8==6.1.0
numpy
pylint
pytest
pytest-shutil
flake8
sphinx
furo
importlib
Expand Down
Empty file added src/datastruct/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions src/datastruct/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Camera data class module
"""
from dataclasses import dataclass


@dataclass
class Camera:
"""
Shot class definition
Args:
name_camera (str): Name of the camera.
ppax (float): Center of distortion in x.
ppay (float): Center of distortion in y.
focal (float): Focal of the camera.
"""
name_camera: str
ppax: float
ppay: float
focal: float
File renamed without changes.
19 changes: 15 additions & 4 deletions src/worksite.py → src/datastruct/worksite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Worksite data class module
"""
import numpy as np
from src.shot import Shot
from src.datastruct.shot import Shot
from src.datastruct.camera import Camera


class Worksite:
Expand All @@ -15,10 +16,10 @@ def __init__(self, name: str) -> None:
Args:
name (str): Name of the worksite.
shots (list): Shots list of the worksite, list<Shot>.
"""
self.name = name
self.shots = []
self.cameras = []

def add_shot(self, name_shot: str, pos_shot: np.array,
ori_shot: np.array, name_cam: str) -> None:
Expand All @@ -36,8 +37,18 @@ def add_shot(self, name_shot: str, pos_shot: np.array,
ori_shot=ori_shot,
name_cam=name_cam))

def add_camera(self):
def add_camera(self, name_camera: str, ppax: float,
ppay: float, focal: float) -> None:
"""
Add data camera in the Worksite
Args:
name_camera (str): Name of the camera.
ppax (float): Center of distortion in x.
ppay (float): Center of distortion in y.
focal (float): Focal of the camera.
"""
return 0
self.cameras.append(Camera(name_camera=name_camera,
ppax=ppax,
ppay=ppay,
focal=focal))
Empty file added src/reader/camera/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions src/reader/camera/reader_camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Script to read camera file txt or xml
"""
import xml.etree.ElementTree as ET
from src.datastruct.worksite import Worksite


def read_camera(files: list, work: Worksite) -> None:
"""
Manage file in list files to read
Args:
files (list): path list of files cameras
work (Worksite): Worksite which needs camera data
"""
for file in files:
ext = file.split('.')[-1].lower()
if ext == 'xml':
camera_xml(file, work)
if ext == 'txt':
camera_txt(file, work)


def camera_xml(file: str, work: Worksite) -> None:
"""
Read xml file camera
Args:
files (list): path list of files cameras
work (Worksite): Worksite which needs camera data
"""
projet = ET.parse(file).getroot()
focal = projet.find("focal").find("pt3d")
work.add_camera(projet.find("name").text.strip(),
float(focal.find("x").text.strip()),
float(focal.find("y").text.strip()),
float(focal.find("z").text.strip()))


def camera_txt(file: str, work: Worksite) -> None:
"""
Read txt file camera
Args:
files (list): path list of files cameras
work (Worksite): Worksite which needs camera data
"""
with open(file, 'r', encoding="utf-8") as file_cam:
# Recover the info line and take name camera
name_cam, o_info = file_cam.readlines()[0].split(" / ")
# Take information ppa focal
o_info = o_info.split(" : ")[-1].split(" ")
# Add to worksite
work.add_camera(name_cam, float(o_info[2]), float(o_info[5]), float(o_info[8]))
file_cam.close()
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Photogrammetry site file reader module
"""
import importlib
from src.worksite import Worksite
from src.datastruct.worksite import Worksite


def reader_orientation(file: str, skip: int = None) -> Worksite:
Expand All @@ -20,7 +20,7 @@ def reader_orientation(file: str, skip: int = None) -> Worksite:
# Attention management of files with the same extension but different formats
ext = file.split(".")[-1]
try:
my_module = importlib.import_module("src.reader.reader_" + ext.lower())
my_module = importlib.import_module("src.reader.orientation.reader_" + ext.lower())
work = my_module.read(file, skip)
except ModuleNotFoundError as e:
raise ValueError(f"{ext} file is not taken into account !!!") from e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A script to read opk file
"""
import numpy as np
from src.worksite import Worksite
from src.datastruct.worksite import Worksite


def read(file: str, skip: int) -> Worksite:
Expand Down Expand Up @@ -40,6 +40,7 @@ def read(file: str, skip: int) -> Worksite:
float(item_shot[5]),
float(item_shot[6])], dtype=float),
item_shot[7])
file_opk.close()
except FileNotFoundError as e:
raise ValueError("The path to the .opk file is incorrect !!!") from e

Expand Down
13 changes: 7 additions & 6 deletions src/writer/writer.py → src/writer/writer_opk.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
"""
Photogrammetry site file reader module
"""
from src.worksite import Worksite
import os
from src.datastruct.worksite import Worksite


def to_opk(path: str, work: Worksite) -> None:
def write(path_opk: str, work: Worksite) -> None:
"""
Write function, to save a photogrammetric site in .opk format
Args:
path (str): Path of registration file.
path_opk (str): Path of registration file .opk.
work (Worksite): The site to be recorded.
"""
path = path + work.name + ".opk"
path_opk = os.path.join(path_opk, f"{work.name}.opk")

try:
with open(path, "w", encoding="utf-8") as file:
with open(path_opk, "w", encoding="utf-8") as file:
file.write("NOM X Y Z O P K CAMERA")
for shot in work.shots:
file.write("\n")
Expand All @@ -29,4 +30,4 @@ def to_opk(path: str, work: Worksite) -> None:
shot.name_cam)
file.close()
except FileNotFoundError as e:
raise ValueError("The path doesn't exist !!!") from e
raise ValueError("The path doesn't exist !!!", e) from e
1 change: 1 addition & 0 deletions test/data/Camera.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UCE-M3-f120-s06 / 431S61699X311363-f120-s06 : PPAx = 13210.00 PPAy = 8502.00 f = 30975.00
33 changes: 33 additions & 0 deletions test/data/s07_UC_Eagle_M3_120.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<sensor>
<name> UCE-M3-f120-s07 </name>
<objectif> 123.9mm </objectif>
<origine> UCE-M3-f120-s07 </origine>
<argentique> 0 </argentique>
<calibration-date> 06-07-2020 </calibration-date>
<serial-number> 431S72800X219308-f120 </serial-number>
<usefull-frame>
<rect>
<x> 0 </x>
<y> 0 </y>
<w> 26460 </w>
<h> 17004 </h>
</rect>
</usefull-frame>
<dark-frame>
<rect>
<x> 0 </x>
<y> 0 </y>
<w> 0 </w>
<h> 0 </h>
</rect>
</dark-frame>
<focal>
<pt3d>
<x> 13230 </x>
<y> 8502 </y>
<z> 30975 </z>
</pt3d>
</focal>
<pixel_size> 0.000004 </pixel_size>
<orientation> 0 </orientation>
</sensor>
Loading

0 comments on commit 1c94db9

Please sign in to comment.