Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in gcp_check.py, check if the input file is existing and is a real csv file. #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified GUI-Find-GCP.py
100644 → 100755
Empty file.
Empty file modified aruco_type.py
100644 → 100755
Empty file.
47 changes: 38 additions & 9 deletions gcp_check.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import argparse
from os import path
import os
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
Expand Down Expand Up @@ -40,7 +42,7 @@ class GcpCheck(tk.Tk):
""" class to check found GCPs visually """

def __init__(self, gcp_style, gcp_file=None, separator=" ", width=700, height=550,
img_path=''):
img_path='./'):
""" initialize

:param gcp_file: output file of gcp_find.py
Expand Down Expand Up @@ -123,6 +125,31 @@ def LoadGcps(self):
"""
retry = True
skiprows =0

# Check if the file exists
if not os.path.isfile(self.gcp_file):
print(f"The file {self.gcp_file} does not exist.")
sys.exit()

# Attempt to read the first line to verify CSV structure
try:
# Open the file and read a sample to check for valid CSV structure
with open(self.gcp_file, 'r', encoding='utf-8') as csv_file:
# Check if the first row can be read as a CSV line
sample = csv_file.readline()
if not sample: # Empty file check
raise argparse.ArgumentTypeError(f"The file '{self.gcp_file}' is empty.")
if self.separator not in sample: # Delimiter check
raise argparse.ArgumentTypeError(f"The file '{self.gcp_file}' does not use {self.separator} as a separator.")
except UnicodeDecodeError:
print(f"The file '{self.gcp_file}' is not a valid UTF-8 encoded CSV file.")
sys.exit()
except Exception as e:
print(f"Error reading {self.gcp_file}: {e}")
sys.exit()



while retry:
try:
self.gcps = pd.read_csv(self.gcp_file, sep=self.separator,
Expand All @@ -142,7 +169,7 @@ def LoadGcps(self):
return False
if len(self.gcps.columns) == 4:
self.gcps.columns = ["col", "row", "img", "id"]
if len(self.gcps.columns) == 5:
elif len(self.gcps.columns) == 5:
self.gcps.columns = ["col", "row", "img", "id", "size"]
elif len(self.gcps.columns) == 7:
self.gcps.columns = ["east", "north", "elev", "col", "row", "img", "id"]
Expand Down Expand Up @@ -203,6 +230,8 @@ def ShowImage(self, event=None):
name =self.imgs[self.img_no]
if self.title() != name: # new image to display
self.title(name) # show image name in title
if path.exists(name):
img_path = name
if not path.exists(name):
img_path = path.join(self.img_path, name)
if not path.exists(img_path):
Expand All @@ -223,7 +252,7 @@ def ShowImage(self, event=None):
stroke_width=20)
self.container = self.canvas.create_rectangle(0, 0, self.width, self.height, width=0)
self.imscale = 1.0

canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
bbox1 = self.canvas.bbox(self.container) # get image area
Expand Down Expand Up @@ -300,11 +329,11 @@ def cmd_params(parser):
def_fontsize = 200 # text height pixel
def_fontcolor = 'red' # inner color for GP ID annotation

parser.add_argument('name', metavar='file_name', type=str, nargs='?',
parser.add_argument('gcp_file_name', metavar='file_name', type=str, nargs='?',
help='GCP file to process')
parser.add_argument('--command', type=str, default='all',
help='command all/ID show all images/images with GCP ID')
parser.add_argument('--path', type=str, default='',
parser.add_argument('--path', type=str, default='./',
help='input path for images')
parser.add_argument('-s', '--separator', type=str, default=def_separator,
help=f'input file separator, default {def_separator}')
Expand All @@ -325,17 +354,17 @@ def cmd_params(parser):
parser = argparse.ArgumentParser()
cmd_params(parser)
args = parser.parse_args()
if args.name:
name = args.name
if args.gcp_file_name:
gcp_file_name = args.gcp_file_name
else:
name = None
gcp_file_name = None
style = {'markersize': args.markersize,
'edgecolor': args.edgecolor,
'edgewidth': args.edgewidth,
'fontsize': args.fontsize,
'fontcolor': args.fontcolor,
}
gcp_c = GcpCheck(style, name, args.separator, img_path=args.path)
gcp_c = GcpCheck(style, gcp_file_name, args.separator, img_path=args.path)
gcp_c.mainloop()
#if args.command == "all":
# gcp_c.ShowAll()
Expand Down
2 changes: 1 addition & 1 deletion gcp_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def process_image(self, image_name):
# calculate center of aruco code
x = int(round(np.average(corners[i][0][:, 0])))
y = int(round(np.average(corners[i][0][:, 1])))
self.gcps.append((x, y, os.path.basename(image_name), j, corners[i][0]))
self.gcps.append((x, y, os.path.abspath(image_name), j, corners[i][0]))
if self.args.debug:
if j in self.coords:
plt.plot(x, y, args.markerstyle, markersize=self.args.markersize,
Expand Down