Author Topic: Building a QR code scanner script  (Read 2343 times)

Offline FairfieldPhoto

  • Full Member
  • ***
  • Posts: 236
    • View Profile
    • Fairfield Photography, LLC
Building a QR code scanner script
« on: February 11, 2023, 01:13:59 PM »
I would like to write a simple Ruby script that will scan an image for a 2D QR code and if one is found, allow me to decode the contents to I can put that into a specified metadata field.  If one is not found, I would use the value of the last 2D QR code found until I find a new QR code to decode.

I have found open source libraries that can be compiled into programs that will do the QR code scan and decoding, but not sure how to use those in PM since I am not really compiling the scripts (they are just being interpreted by PM).    I can figure out how to write the recursive script to scan the folder, but need help with getting the QR code data from the image.

Is there a way to call external libraries from a PM script or has anyone found a way to use the scripting engine in PM to identify and decode QR codes?

Thanks.

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 24815
    • View Profile
    • Camera Bits, Inc.
Re: Building a QR code scanner script
« Reply #1 on: February 13, 2023, 08:52:03 AM »
I would like to write a simple Ruby script that will scan an image for a 2D QR code and if one is found, allow me to decode the contents to I can put that into a specified metadata field.  If one is not found, I would use the value of the last 2D QR code found until I find a new QR code to decode.

I have found open source libraries that can be compiled into programs that will do the QR code scan and decoding, but not sure how to use those in PM since I am not really compiling the scripts (they are just being interpreted by PM).    I can figure out how to write the recursive script to scan the folder, but need help with getting the QR code data from the image.

Is there a way to call external libraries from a PM script or has anyone found a way to use the scripting engine in PM to identify and decode QR codes?

No.  The Ruby scripts that are run in the Uploader and Exporter features are run inside a "sandbox" which makes it impossible to access external libraries or scripts.  This is done to prevent any malicious Ruby scripts from being able to cause data loss on your computer.

That said, if you point us to these libraries, we would consider vetting them and including them in PM so that they could be used in Exporters or Uploaders.

-Kirk

Offline FairfieldPhoto

  • Full Member
  • ***
  • Posts: 236
    • View Profile
    • Fairfield Photography, LLC
Re: Building a QR code scanner script
« Reply #2 on: February 13, 2023, 10:39:23 AM »
This is the bar scanner library that Tether Tools "Smart Shooter 4" also uses.

https://zbar.sourceforge.net/

ZBar verson 0.10 implements support for 2D QR codes.

I am not very familiar with using libraries pulled from Source Forge, but a cursory view of the documentation for the ZbarImg function looks like it has the command parameters for the kind of functions I am looking (scan for QR code and return encoded value).

As you have time, could you at least do a quick review to see if these DLLs can be included/referenced?  I know there is a development timeline and we might not see it implemented for awhile, but curious if it is at least doable.  If not, I can keep look for other solutions.

Thanks!

-Mike

Offline FairfieldPhoto

  • Full Member
  • ***
  • Posts: 236
    • View Profile
    • Fairfield Photography, LLC
Re: Building a QR code scanner script
« Reply #3 on: February 26, 2023, 11:00:03 AM »
Any thoughts on the zbar DLL and whether it would be feasible to link it into PM and let me access it via a script?

-M

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 24815
    • View Profile
    • Camera Bits, Inc.
Re: Building a QR code scanner script
« Reply #4 on: February 28, 2023, 11:39:28 AM »
Any thoughts on the zbar DLL and whether it would be feasible to link it into PM and let me access it via a script?

If we were to add the library, we would make the data from discovered QR codes be accessible via a variable.  The only scripting that PM offers are Uploaders and Exporters and neither one of them are allowed to modify images.  Having the QR data available as a variable would allow you to use it in the Metadata Template which could then be used with code replacement/hot codes to do some very powerful things with metadata.

-Kirk

Offline FairfieldPhoto

  • Full Member
  • ***
  • Posts: 236
    • View Profile
    • Fairfield Photography, LLC
Re: Building a QR code scanner script
« Reply #5 on: February 28, 2023, 12:25:35 PM »
OMG YES!!!!!!

{QR} would be an incredible feature enhancement.  I didn't realize I wouldn't be able to insert the QR into metadata (I probably would have just tacked it onto the filename with some editing for valid characters).

Where do I send pizzas?  I would love this ability inside of PM and will advertise it to the volume photographer community when it is implemented.

-M

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 24815
    • View Profile
    • Camera Bits, Inc.
Re: Building a QR code scanner script
« Reply #6 on: February 28, 2023, 01:29:53 PM »
OMG YES!!!!!!

{QR} would be an incredible feature enhancement.  I didn't realize I wouldn't be able to insert the QR into metadata (I probably would have just tacked it onto the filename with some editing for valid characters).

Where do I send pizzas?  I would love this ability inside of PM and will advertise it to the volume photographer community when it is implemented.

We're thinking it over, no timeline for implementation at this time, the usual disclaimers.

Having a QR code recognized in an image is not particularly difficult given that the library you referenced does exactly that.  The difficulty comes in how such a thing would be integrated into the workflow without unduly affecting performance.  Given that you only shoot one of these QR codes just before a group of shots and the shots themselves do not have the QR code visible in the image, the variable would have to contain the last QR data for a selected sequence.  It may in the end, require you to the images you want to process, then right-click on an image containing a QR code, choose a command ('Set QR variable') from a contextual menu, and then use the Metadata Template on the selection.

Feels kind of clunky to me.  Another option would be to have a checkbox in the Metadata Template (Analyze QR codes) that when on, analyzes each image processed for a QR code (this would require decoding the image and running it through the library), which when found would set the value for the {QR} variable, which would then stay current until a new one was found.  That feels better, but the Metadata Template does not ever hit up a photo for its image data, just metadata.

Anyhow, there's lots to think about before we could go down the path of implementing such a feature.

-Kirk

Offline FairfieldPhoto

  • Full Member
  • ***
  • Posts: 236
    • View Profile
    • Fairfield Photography, LLC
Re: Building a QR code scanner script
« Reply #7 on: February 28, 2023, 02:11:58 PM »
You are thinking right along the lines of usability - scan the QR code and then have it available for images where a QR doesn't exist.  Both scenarios you outline are feasible.  Scenario 1 is very similar to what I do today to batches of images with voice recorder memos.  I select the first image in the series, do the shortcut to play the audio and while it is playing, I select all the images from that one up to the next one with an audio memo and use the IPTC Metadata template to set the value(s) I want.  I agree with you that this is clunky, but it is "feasible."

Your second scenario has a lot more appeal.  Select a range of images including one or more images containing a QR code.  Let PM chug along and discover a QR and then populate those values to desired metadata field(s).  Would we be limited to one value in the QR code or could there be multiple values separated by a delimiter that could then be parsed during processing into QRField1, QRField2, QRField3, etc.?   One constraint on this scenario is that there has to be "mission rule" for shooting that the QR shot comes first.  Any QR code shot AFTER the stream of images would require the QR value to be decoded as a single selected image and then the results replicated manually backwards in time one batch at a time with the Metadata Template panel.


So I think you have considered these Use Cases:

Image contains a QR code:
Variable {QR} is set to the value of the QR code in the image.

Image does not contain a QR code nor has any QR code been processed so far in this batch:
Variable {QR} blank or null.

Image does not contain a QR code but at least one image with a QR code has been scanned:
Variable {QR} contains the value from the last image with a QR code

I was considering a variable to identify those images containing a QR code faster.  Something like {HasQR} with a value of True or False.  This would let me identify/filter on the QR images for later processing if I needed to look at just those images showing a QR code.  It would also let me do a filter where I can exclude/hide those with identified QR code values and then do a visual scan of the contact sheet to see if there were any images displaying a partial QR code that were missed (like a subject holding their finger over part of the QR code image to prevent decoding).  Would make problem research/resolution very fast.  I wouldn't be able to do that filtering with just the {QR} value after processing the folder.

-Mike


Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 24815
    • View Profile
    • Camera Bits, Inc.
Re: Building a QR code scanner script
« Reply #8 on: February 28, 2023, 02:33:09 PM »
Mike,

Your second scenario has a lot more appeal.  Select a range of images including one or more images containing a QR code.  Let PM chug along and discover a QR and then populate those values to desired metadata field(s).  Would we be limited to one value in the QR code or could there be multiple values separated by a delimiter that could then be parsed during processing into QRField1, QRField2, QRField3, etc.?

I'd prefer to build on top of functionality that PM already offers, like code replacement.  So if your QR data resolved to a 'code' then you could have a custom replacement for each field important to you.  You'd have to create the code replacement/hot code data beforehand, but given that, the workflow would be fully automated, one pass filling in all fields with exact data.

One constraint on this scenario is that there has to be "mission rule" for shooting that the QR shot comes first.  Any QR code shot AFTER the stream of images would require the QR value to be decoded as a single selected image and then the results replicated manually backwards in time one batch at a time with the Metadata Template panel.

You could always do one of the following:

Use the Arrangement feature to move the QR image before the group you want it to affect.
Use Filename sort and rename the QR image such that it sorts before the group you want it to affect.

I was considering a variable to identify those images containing a QR code faster.  Something like {HasQR} with a value of True or False.  This would let me identify/filter on the QR images for later processing if I needed to look at just those images showing a QR code.  It would also let me do a filter where I can exclude/hide those with identified QR code values and then do a visual scan of the contact sheet to see if there were any images displaying a partial QR code that were missed (like a subject holding their finger over part of the QR code image to prevent decoding).  Would make problem research/resolution very fast.  I wouldn't be able to do that filtering with just the {QR} value after processing the folder.

Unfortunately, this capability would affect the overall performance of PM.  To evaluate that variable would require decoding the image at sufficient resolution, running it through the library and then maintaining the result.  There's no such thing as a free lunch on this one.

-Kirk

Offline FairfieldPhoto

  • Full Member
  • ***
  • Posts: 236
    • View Profile
    • Fairfield Photography, LLC
Re: Building a QR code scanner script
« Reply #9 on: February 28, 2023, 02:42:12 PM »
"Unfortunately, this capability would affect the overall performance of PM.  To evaluate that variable would require decoding the image at sufficient resolution, running it through the library and then maintaining the result.  There's no such thing as a free lunch on this one."

Understand about the "No Free Lunch".  I would have evaluated at the same time I was doing Scenario 2 and scanning the whole contact sheet.  My responsibility to to capture {HasQR} when the folder is being processed and put it somewhere in IPTC that I could reference later.  I agree that interrogating {HasQR} after the fact would require reprocessing all the images just for that value would be a terrible drain on performance. 

One pass, get any QR related value.