mirror of
https://github.com/Xevion/phototag.git
synced 2025-12-15 12:12:39 -06:00
change to named loggers
This commit is contained in:
4
main.py
4
main.py
@@ -1,7 +1,9 @@
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
from package import log, app
|
||||
from package import app
|
||||
|
||||
log = logging.getLogger('main')
|
||||
|
||||
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(
|
||||
sys.path[0], 'package', 'key', 'photo_tagging_service.json')
|
||||
|
||||
@@ -6,11 +6,16 @@ import progressbar
|
||||
# Logging and Progressbar work
|
||||
progressbar.streams.wrap_stderr()
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
log = logging.getLogger(__name__)
|
||||
log = logging.getLogger('init')
|
||||
log.info('Progressbar/Logging ready.')
|
||||
|
||||
|
||||
# Path Constants
|
||||
# ROOT = ''
|
||||
ROOT = sys.path[0]
|
||||
# PROCESSING_PATH = ROOT
|
||||
PROCESSING_PATH = os.path.join(ROOT, 'package', 'processing')
|
||||
# INPUT_PATH = PROCESSING_PATH
|
||||
INPUT_PATH = os.path.join(PROCESSING_PATH, 'input')
|
||||
TEMP_PATH = os.path.join(PROCESSING_PATH, 'temp')
|
||||
OUTPUT_PATH = os.path.join(PROCESSING_PATH, 'output')
|
||||
|
||||
@@ -15,43 +15,38 @@ from .process import FileProcessor
|
||||
from . import INPUT_PATH, TEMP_PATH, OUTPUT_PATH, PROCESSING_PATH
|
||||
from . import RAW_EXTS, LOSSY_EXTS
|
||||
|
||||
# Process a single file in these steps:
|
||||
# 1) Create a temporary file
|
||||
# 2) Send it to GoogleAPI
|
||||
# 3) Read XMP, then write new tags to it
|
||||
# 4) Delete temporary file, move NEF/JPEG and XMP
|
||||
|
||||
|
||||
log = logging.getLogger('app')
|
||||
|
||||
def run():
|
||||
client = vision.ImageAnnotatorClient()
|
||||
|
||||
# Find files we want to process based on if they have a corresponding .XMP
|
||||
logging.info('Locating processable files...')
|
||||
log.info('Locating processable files...')
|
||||
files = os.listdir(INPUT_PATH)
|
||||
select = [file for file in files if os.path.splitext(file)[1] != '.xmp']
|
||||
logging.info(f'Found {len(files)} valid files')
|
||||
log.info(f'Found {len(files)} valid files')
|
||||
|
||||
# Create the 'temp' directory
|
||||
logging.info('Creating temporary processing directory')
|
||||
log.info('Creating temporary processing directory')
|
||||
os.makedirs(TEMP_PATH)
|
||||
|
||||
os.makedirs(OUTPUT_PATH)
|
||||
|
||||
try:
|
||||
# Process files
|
||||
for index, file in progressbar.progressbar(list(enumerate(select)), redirect_stdout=True, term_width=110):
|
||||
_, ext = os.path.splitext(file)
|
||||
ext = ext[1:]
|
||||
ext = ext[1:].lower()
|
||||
if ext in LOSSY_EXTS or ext in RAW_EXTS:
|
||||
process = FileProcessor(file)
|
||||
logging.info(f"Processing file '{file}'...")
|
||||
log.info(f"Processing file '{file}'...")
|
||||
process.run(client)
|
||||
except Exception as error:
|
||||
logging.error(str(error))
|
||||
logging.warning(
|
||||
log.error(str(error))
|
||||
log.warning(
|
||||
'Removing temporary directory before raising exception.')
|
||||
os.rmdir(TEMP_PATH)
|
||||
raise
|
||||
|
||||
# Remove the directory, we are done here
|
||||
logging.info('Removing temporary directory.')
|
||||
log.info('Removing temporary directory.')
|
||||
os.rmdir(TEMP_PATH)
|
||||
|
||||
@@ -12,6 +12,7 @@ from google.cloud import vision
|
||||
from . import TEMP_PATH, INPUT_PATH, OUTPUT_PATH, RAW_EXTS, LOSSY_EXTS
|
||||
from .xmp import XMPParser
|
||||
|
||||
log = logging.getLogger('process')
|
||||
|
||||
class FileProcessor(object):
|
||||
def __init__(self, file_name: str):
|
||||
@@ -63,21 +64,21 @@ class FileProcessor(object):
|
||||
# Performs label detection on the image file
|
||||
response = client.label_detection(image=image)
|
||||
labels = [label.description for label in response.label_annotations]
|
||||
logging.info('Keywords Identified: {}'.format(', '.join(labels)))
|
||||
log.info('Keywords Identified: {}'.format(', '.join(labels)))
|
||||
|
||||
# XMP sidecar file specified, write to it using XML module
|
||||
if self.xmp:
|
||||
logging.info('Writing {} tags to output XMP.'.format(len(labels)))
|
||||
log.info('Writing {} tags to output XMP.'.format(len(labels)))
|
||||
parser = XMPParser(self.input_xmp)
|
||||
parser.add_keywords(labels)
|
||||
# Save the new XMP file
|
||||
logging.debug('Saving to new XMP file.')
|
||||
log.debug('Saving to new XMP file.')
|
||||
parser.save(self.output_xmp)
|
||||
logging.debug('Removing old XMP file.')
|
||||
log.debug('Removing old XMP file.')
|
||||
os.remove(self.input_xmp)
|
||||
# No XMP file is specified, using IPTC tagging
|
||||
else:
|
||||
logging.info('Writing {} tags to image IPTC'.format(len(labels)))
|
||||
log.info('Writing {} tags to image IPTC'.format(len(labels)))
|
||||
info = iptcinfo3.IPTCInfo(os.path.join(INPUT_PATH, self.file_name))
|
||||
info['keywords'].extend(labels)
|
||||
info.save()
|
||||
|
||||
Reference in New Issue
Block a user