Fixed detected_text to use image orientation if available

This commit is contained in:
Rhet Turnbull
2021-09-15 20:18:18 -07:00
parent 77103193c0
commit dd08c7f701
4 changed files with 32 additions and 17 deletions

View File

@@ -1702,7 +1702,7 @@ Substitution Description
{lf} A line feed: '\n', alias for {newline} {lf} A line feed: '\n', alias for {newline}
{cr} A carriage return: '\r' {cr} A carriage return: '\r'
{crlf} a carriage return + line feed: '\r\n' {crlf} a carriage return + line feed: '\r\n'
{osxphotos_version} The osxphotos version, e.g. '0.42.82' {osxphotos_version} The osxphotos version, e.g. '0.42.83'
{osxphotos_cmd_line} The full command line used to run osxphotos {osxphotos_cmd_line} The full command line used to run osxphotos
The following substitutions may result in multiple values. Thus if specified for The following substitutions may result in multiple values. Thus if specified for
@@ -3561,7 +3561,7 @@ The following template field substitutions are availabe for use the templating s
|{lf}|A line feed: '\n', alias for {newline}| |{lf}|A line feed: '\n', alias for {newline}|
|{cr}|A carriage return: '\r'| |{cr}|A carriage return: '\r'|
|{crlf}|a carriage return + line feed: '\r\n'| |{crlf}|a carriage return + line feed: '\r\n'|
|{osxphotos_version}|The osxphotos version, e.g. '0.42.82'| |{osxphotos_version}|The osxphotos version, e.g. '0.42.83'|
|{osxphotos_cmd_line}|The full command line used to run osxphotos| |{osxphotos_cmd_line}|The full command line used to run osxphotos|
|{album}|Album(s) photo is contained in| |{album}|Album(s) photo is contained in|
|{folder_album}|Folder path + album photo is contained in. e.g. 'Folder/Subfolder/Album' or just 'Album' if no enclosing folder| |{folder_album}|Folder path + album photo is contained in. e.g. 'Folder/Subfolder/Album' or just 'Album' if no enclosing folder|

View File

@@ -1,3 +1,3 @@
""" version info """ """ version info """
__version__ = "0.42.82" __version__ = "0.42.83"

View File

@@ -1054,14 +1054,14 @@ class PhotoInfo:
return self._info["orientation"] return self._info["orientation"]
# For Photos 5+, try to get the adjusted orientation # For Photos 5+, try to get the adjusted orientation
if self.hasadjustments: if not self.hasadjustments:
return self._info["orientation"]
if self.adjustments: if self.adjustments:
return self.adjustments.adj_orientation return self.adjustments.adj_orientation
else: else:
# can't reliably determine orientation for edited photo if adjustmentinfo not available # can't reliably determine orientation for edited photo if adjustmentinfo not available
return 0 return 0
else:
return self._info["orientation"]
@property @property
def original_height(self): def original_height(self):
@@ -1156,7 +1156,8 @@ class PhotoInfo:
md = OSXMetaData(path) md = OSXMetaData(path)
detected_text = md.get_attribute("osxphotos_detected_text") detected_text = md.get_attribute("osxphotos_detected_text")
if detected_text is None: if detected_text is None:
detected_text = detect_text(path) orientation = self.orientation or None
detected_text = detect_text(path, orientation)
md.set_attribute("osxphotos_detected_text", detected_text) md.set_attribute("osxphotos_detected_text", detected_text)
return detected_text return detected_text

View File

@@ -1,7 +1,7 @@
""" Use Apple's Vision Framework via PyObjC to perform text detection on images (macOS 10.15+ only) """ """ Use Apple's Vision Framework via PyObjC to perform text detection on images (macOS 10.15+ only) """
import logging import logging
from typing import List from typing import List, Optional
import objc import objc
import Quartz import Quartz
@@ -22,8 +22,13 @@ else:
vision = True vision = True
def detect_text(img_path: str) -> List: def detect_text(img_path: str, orientation: Optional[int] = None) -> List:
"""process image at img_path with VNRecognizeTextRequest and return list of results""" """process image at img_path with VNRecognizeTextRequest and return list of results
Args:
img_path: path to the image file
orientation: optional EXIF orientation (if known, passing orientation may improve quality of results)
"""
if not vision: if not vision:
logging.warning(f"detect_text not implemented for this version of macOS") logging.warning(f"detect_text not implemented for this version of macOS")
return [] return []
@@ -40,9 +45,18 @@ def detect_text(img_path: str) -> List:
input_image = Quartz.CIImage.imageWithContentsOfURL_(input_url) input_image = Quartz.CIImage.imageWithContentsOfURL_(input_url)
vision_options = NSDictionary.dictionaryWithDictionary_({}) vision_options = NSDictionary.dictionaryWithDictionary_({})
vision_handler = Vision.VNImageRequestHandler.alloc().initWithCIImage_options_( if orientation is not None:
if not 1 <= orientation <= 8:
raise ValueError("orientation must be between 1 and 8")
vision_handler = Vision.VNImageRequestHandler.alloc().initWithCIImage_orientation_options_(
input_image, orientation, vision_options
)
else:
vision_handler = (
Vision.VNImageRequestHandler.alloc().initWithCIImage_options_(
input_image, vision_options input_image, vision_options
) )
)
results = [] results = []
handler = make_request_handler(results) handler = make_request_handler(results)
vision_request = ( vision_request = (