-
Notifications
You must be signed in to change notification settings - Fork 4
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
Handle smaller cr3 pdf page size #1529
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
adb1a4b
handle smaller cr3 pdf page size
johnclary 7afe88a
handle new and legacy cr3 versions in both small and large
johnclary 812aaec
fix typo and clean up bbox values
johnclary fc04990
log instead of raise csv <> pdf count mismatch
johnclary 891400c
update log entry after csvs are done
johnclary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import time | ||
|
||
from pdf2image import convert_from_path | ||
from pdf2image import convert_from_path, pdfinfo_from_path | ||
|
||
from utils.graphql import UPDATE_CRASH_CR3_FIELDS, make_hasura_request | ||
from utils.logging import get_logger | ||
|
@@ -19,42 +19,57 @@ | |
logger = get_logger() | ||
|
||
|
||
def is_new_cr3_form(page): | ||
"""Determine if the CR3 is following the older or newer format. | ||
def get_cr3_version(page, page_width): | ||
"""Determine the CR3 form version. | ||
|
||
The check is conducted by sampling if various pixels are black. | ||
|
||
On August 27, 2024 CRIS started delivering all CR3s using a | ||
smaller page size. This function was adapted to handle the | ||
legacy large format page and the new smaller page size. | ||
|
||
Args: | ||
page (PIL image): the pdf page as an image | ||
page_width (int): the width of the PDF in points | ||
|
||
Returns: | ||
bool: true if the provided page passes the sampling tests | ||
str: 'v1_small', 'v1_large','v2_large', or 'v2_small' | ||
""" | ||
new_cr3_form = True | ||
for pixel in NEW_CR3_FORM_TEST_PIXELS: | ||
page_size = "small" if page_width < 700 else "large" | ||
test_pixels = NEW_CR3_FORM_TEST_PIXELS[page_size] | ||
|
||
for pixel in test_pixels: | ||
rgb_pixel = page.getpixel(pixel) | ||
if rgb_pixel[0] != 0 or rgb_pixel[1] != 0 or rgb_pixel[2] != 0: | ||
new_cr3_form = False | ||
break | ||
return new_cr3_form | ||
if rgb_pixel[0] > 5 or rgb_pixel[1] > 5 or rgb_pixel[2] > 5: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was having trouble hitting pure black pixels in the smaller format, so i added this narrow tolerance of |
||
# the PDF fails our pixel checks, so assume it's the | ||
# earliest version | ||
return f"v1_{page_size}" | ||
|
||
return f"v2_{page_size}" | ||
|
||
|
||
def get_pdf_width(pdf_path): | ||
"""Return the width of the pdf in points""" | ||
pdf_info = pdfinfo_from_path(pdf_path) | ||
# parse width from a string that looks like '612 x 792 pts (letter)' | ||
return int(pdf_info["Page size"].split(" ")[0]) | ||
|
||
|
||
def crop_and_save_diagram(page, cris_crash_id, is_new_cr3_form, extract_dir): | ||
def crop_and_save_diagram(page, cris_crash_id, bbox, extract_dir): | ||
"""Crop out the crash diagram and save it to the local directory. | ||
|
||
The diagram is saved to <extract_dir>/crash_diagrams/<cris_crash_id>.jpeg | ||
|
||
Args: | ||
page (PIL image): the CR3 pdf page as an image | ||
cris_crash_id (int): the CRIS crash ID | ||
is_new_cr3_form (bool): if the CR3 is in the 'new' format | ||
bbox (tuple[int]): the bounding box pixels to crop | ||
extract_dir (str): the local directory in which to save the file | ||
|
||
Returns: | ||
str: diagram_full_path - the full path to the saved diagram, including it's name | ||
str: diagram_filename - the name of diagram file, .e.g 12345678.jpeg | ||
""" | ||
bbox = DIAGRAM_BBOX_PIXELS["new"] if is_new_cr3_form else DIAGRAM_BBOX_PIXELS["old"] | ||
diagram_image = page.crop(bbox) | ||
diagram_filename = f"{cris_crash_id}.jpeg" | ||
diagram_full_path = os.path.join(extract_dir, "crash_diagrams", diagram_filename) | ||
|
@@ -79,7 +94,10 @@ def process_pdf(extract_dir, filename, s3_upload, index): | |
logger.info(f"Processing {filename} ({index})") | ||
cris_crash_id = int(filename.replace(".pdf", "")) | ||
pdf_path = os.path.join(extract_dir, "crashReports", filename) | ||
page_width = get_pdf_width(pdf_path) | ||
|
||
logger.debug("Converting PDF to image...") | ||
|
||
page = convert_from_path( | ||
pdf_path, | ||
fmt="jpeg", # jpeg is much faster than the default ppm fmt | ||
|
@@ -88,9 +106,13 @@ def process_pdf(extract_dir, filename, s3_upload, index): | |
dpi=150, | ||
)[0] | ||
|
||
cr3_version = get_cr3_version(page, page_width) | ||
bbox = DIAGRAM_BBOX_PIXELS[cr3_version] | ||
|
||
logger.debug("Cropping crash diagram...") | ||
|
||
diagram_full_path, diagram_filename = crop_and_save_diagram( | ||
page, cris_crash_id, is_new_cr3_form(page), extract_dir | ||
page, cris_crash_id, bbox, extract_dir | ||
) | ||
|
||
if s3_upload: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i added this so that the number of CSVs processed will be saved in the import log even if the PDF import fails. the
records_processed
will look something like this:and the
completed_at
value will be null, indicating that the import is not complete.