<<

NAME

ProductOpener::Images - adds, processes, manages and displays product photos

DESCRIPTION

ProductOpener::Images is used to: - upload product images - select and crop product images - run OCR on images - display product images

Product images on disk

Product images are stored in html/images/products/[product barcode split with slashes]/

For each product, this directory contains:

[image number].[extension].orig (e.g. 1.jpg.orig, 2.jpg.orig etc.)

Original images uploaded by users or imported

[image number].jpg

Same image saved as JPEG with specific settings, and after some minimal processing (auto orientation, removing EXIF data, flattening PNG images to remove transparency).

Those images are not displayed on the web site (except on the product edit form), but can be selected and cropped.

[image number].[100|400].jpg

Same image saved with a maximum width and height of 100 and 400 pixels. Those thumbnails are used in the product edit form to show the available images.

[image number].json

OCR output from Google Cloud Vision.

When a new image is uploaded, a symbolic link to it is created in /new_images. This triggers a script to generate and save the OCR: run_cloud_vision_ocr.pl.

[front|ingredients|nutrition|packaging]_[2 letter language code].[product revision].[full|100|200|400].jpg

Cropped and selected image for the front of the product, the ingredients list, the nutrition facts table, and the packaging information / recycling instructions, in 4 different sizes (full size, 100 / 200 / 400 pixels maximum width or height).

The product revision is a number that is incremented for each change to the product (each image upload and each image selection are also individual changes that create a new revision).

The selected images are shown on the website, in the app etc.

When a new image is selected for a given field (e.g. ingredients) and language (e.g. French), the existing selected images are kept. (e.g. we can have ingredients_fr.21.100.jpg and a new ingredients_fr.28.100.jpg).

Previously selected images are shown only when people access old product revisions.

Cropping coordinates for all revisions are stored in the "images" field of the product, so we could regenerate old selected and cropped images on demand.

SUPPORTED IMAGE TYPES

gif, jpeg, jpf, png, heic

FUNCTIONS

get_code_and_imagefield_from_file_name ( $l $filename )

This function is used to guess if an image is the front of the product, its list of ingredients, or the nutrition facts table, based on the filename.

It is used in particular for bulk upload of photos sent by manufacturers. The file names have many different formats, but they very often include the barcode of the product, and sometimes an indication of what the image is.

Producers are advised to use the [front|ingredients|nutrition|packaging]_[language code] format, but in practice we receive many other names.

The %file_names_to_imagefield_regexps structure below contains some patterns used to guess what the image is about.

process_image_upload ( $product_id, $imagefield, $user_id, $time, $comment, $imgid_ref, $debug_string_ref )

Process an image uploaded to a product (from the web site, from the API, or from an import):

- Read the image - Create a JPEG version - Create resized versions - Store the image in the product data

Arguments

Product id $product_id

Image field $imagefield

Indicates what the image is and its language, or indicate a path to the image file (for imports and when uploading an image with a barcode)

Format: [front|ingredients|nutrition|packaging|other]_[2 letter language code]

User id $user_id

Timestamp of the image $time

Comment $comment

Reference to an image id $img_id

Used to return the number identifying the image to the caller.

Debug string reference $debug_string_ref

Used to return some debug information to the caller.

extract_text_from_image( $product_ref, $id, $field, $ocr_engine, $results_ref )

Perform OCR for a specific image (either a source image, or a selected image) and return the results.

OCR can be performed with a locally installed Tesseract, or through Google Cloud Vision.

In the case of Google Cloud Vision, we also store the results of the OCR as a JSON file (requested through HTTP by Robotoff).

Arguments

product reference $product_ref

id of the image $id

Either a number like 1, 2 etc. to perform the OCR on a source image (1.jpg, 2.jpg) or a field name in the form of [front|ingredients|nutrition|packaging]_[2 letter language code].

If $id is a field name, the last selected image for that field is used.

OCR engine $ocr_engine

Either "tesseract" or "google_cloud_vision"

Results reference $results_ref

A hash reference to store the results.

send_image_to_cloud_vision ($image_path, $json_file, $features_ref, $gv_logs)

Call to Google Cloud vision API

Arguments

$image_path - str path to image

$json_file - str path to the file where we will store OCR result as gzipped JSON

$features_ref - hash reference - the "features" parameter of Google Cloud Vision

This determine which detection will be performed. Remember each feature is a cost.

@CLOUD_VISION_FEATURES_FULL and @CLOUD_VISION_FEATURES_TEXT are two constant you can use.

$gv_logs - file handle

A file where we write additional logs, specific to the service.

Response

Return JSON content of the response.

<<