Link to home
Start Free TrialLog in
Avatar of xenium
xenium

asked on

Using a google script to process a jpeg image

hi,

I'm looking to setup a trigger alert service based on images from an ip security camera. I want to alert if some objects are present during a specific time window. The objects are bins, the time window is during the night, and i have put reflective material on the bins so you can easily see the marks on the IR of the camera, see attached.

User generated image
I could use iSpy to set this up, but i'd like to use a google script running the check. i can pickup the images with a google script, so i "just" need to be able to perform a check on the image. In my case the average brightness within a specified rectangle of the image would be enough info. I guess i need to call some kind of image processing tool for this, as i'm probably not looking to do all the coding from scratch.

Any suggestions? I'm new to scripting with google so any example code would be great.

Many thanks
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi xenium,
Following up on your "any example code would be great" comment and Scott's recommendation of ImageMagick, I'll offer up these Experts Exchange articles on GraphicsMagick that have some example code:

Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method

Create a PDF file with Contact Sheets (montage of thumbnails) for all JPG files in a folder and each of its subfolders using an automated, batch method

Create an image (BMP, GIF, JPG, PNG, TIF, etc.) from a multi-page PDF

Convert a multi-page PDF file into multiple image files

I prefer GM over IM, but both are excellent (GM is a fork of IM), and, although I prefer GM, IM is easier to use in programs/scripts because its command line executable is self-contained, whereas the GM one has dependencies. Although the products have diverged since the fork, their command line calls remain very similar, so the code in those GM articles should be helpful even when using IM.

Here's another idea. I'm a big fan of the AutoHotkey language, which has a command called PixelSearch. Some simple testing shows these RGB ranges for the four bright spots in your photo:

Red: F0-F4
Green: F5-FB
Blue: F3-F7

I don't think that it would be very difficult to write an AutoHotkey script that uses the PixelSearch command to find those bright spots (but I must admit that I'm not sure about that). Fascinating project! Regards, Joe
Joe, can you show some examples of how to find the white spots in a general area using autohot key or GraphicsMagick?
Hi Scott,
Here's a working AutoHotkey script that finds one of the bright pixels:

UpperLeftX:=380
UpperLeftY:=100
LowerRightX:=575
LowerRightY:=325
BGRsearch:=0xF5F8F2
ColorVariation:=3
CoordMode,Pixel,Window
SetTitleMatchMode,1 
WindowWithImageTitle:="webcam-Capture.JPG"
WinActivate,%WindowWithImageTitle%
PixelSearch,OutputX,OutputY,%UpperLeftX%,%UpperLeftY%,%LowerRightX%,%LowerRightY%,%BGRsearch%,%ColorVariation%
If (ErrorLevel=0)
{
  PixelGetColor,ColorFound,%OutputX%,%OutputY%
  MsgBox,4160,Found bright pixel,BGR=%ColorFound%  Location(X`,Y)=%OutputX%`,%OutputY%
}
Else
  MsgBox,4112,Could not find bright pixel,Error Level %ErrorLevel% trying to find bright pixel
ExitApp

Open in new window

Based on the photo that xenium posted, I set the variables in the script accordingly, giving it a fairly wide range on the X and Y coordinates and a BGR color variation of 3. I loaded the image in IrfanView. Running the script gives this result:

User generated image
That location is in the lower left of the four bright areas in his photo. Regards, Joe

Edit#1: I ran another test with the Fast parameter. The line of code for that is:

PixelSearch,OutputX,OutputY,%UpperLeftX%,%UpperLeftY%,%LowerRightX%,%LowerRightY%,%BGRsearch%,%ColorVariation%,Fast

Open in new window

It lives up to its name by being significantly faster, but it finds a different bright pixel:

User generated image
That one is in the upper left of the four bright areas in his photo. The reason for the difference is that fast mode searches the screen by rows, while slow mode searches by columns, so they can find different pixels if there is more than one matching pixel, which is true in this case. This is explained at the PixelSearch documentation. Regards, Joe

Edit#2:
the average brightness within a specified rectangle of the image would be enough info
That comment spurred another idea — finding the average BGR colors within a specified rectangle. Here's an AutoHotkey script that does it:

UpperLeftX:=400
UpperLeftY:=310
LowerRightX:=420
LowerRightY:=320
CoordMode,Pixel,Window
SetTitleMatchMode,1
WindowWithImageTitle:="webcam-Capture.JPG"
WinActivate,%WindowWithImageTitle%
NumPixelsX:=LowerRightX-UpperLeftX+1
NumPixelsY:=LowerRightY-UpperLeftY+1
NumPixelsTotal:=NumPixelsX*NumPixelsY
BlueTotal:=GreenTotal:=RedTotal:=0
Loop,%NumPixelsY%
{
  CurrentY:=UpperLeftY+A_Index-1
  Loop,%NumPixelsX%
  {
    CurrentX:=UpperLeftX+A_Index-1
    PixelGetColor,ColorFound,%CurrentX%,%CurrentY%
    If (ErrorLevel<>0)
    {
      MsgBox,4112,Could not get pixel color,Error Level %ErrorLevel% trying to get color at Location(X`,Y)=%CurrentX%`,%CurrentY%
      ExitApp
    }
    BlueFound:="0x" . SubStr(ColorFound,3,2)
    BlueTotal:=BlueTotal+BlueFound
    GreenFound:="0x" . SubStr(ColorFound,5,2)
    GreenTotal:=GreenTotal+GreenFound
    RedFound:="0x" . SubStr(ColorFound,7,2)
    RedTotal:=RedTotal+RedFound
  }
}
BlueAvg:=Round(BlueTotal/NumPixelsTotal)
GreenAvg:=Round(GreenTotal/NumPixelsTotal)
RedAvg:=Round(RedTotal/NumPixelsTotal)
MsgBox,4160,Average Colors Decimal,Blue=%BlueAvg%  Green=%GreenAvg%  Red=%RedAvg%

Open in new window

Based on the photo that xenium posted, I picked upper and lower X/Y coordinates that are in the middle of the lower left bright spot. Running the script with the image in IrfanView gives this result:

User generated image
That shows the average "brightness", i.e., "whiteness", within the specified rectangle, where pure white-hex=0xFFFFFF (pure white-decimal=255,255,255). Regards, Joe
xenium, I realize there are some general principles here but does any of this help?
Avatar of xenium
xenium

ASKER

hi all, thanks for all the great input. There seems to be quite a few potential approaches. I'll take a closer look at each, but ImageMagick looks like a good option and it's something I've used effectively in the past (on Windows). If there's no pixel averaging function per se, it should be possible to crop and downscale an image to a few pixels only which effectively performs an averaging function.

The AutoHotKey system also sounds useful for a separate question I have but unlikely I will use it here as it seems to be Windows only.

Thanks again
> AutoHotKey system also sounds useful for a separate question

Happy to try to help on anything AutoHotkey related. Let me know the link to the question.

> it seems to be Windows only

Correct.
Avatar of xenium

ASKER

Thanks Joe, the other question is about web scraping, low volume, daily. https://www.experts-exchange.com/questions/29096037/How-to-scrape-non-page-source-data-client-side-on-a-PC.html
Thanks for the link. In addition to my AutoHotkey - Getting Started article, you may find this site to be helpful:

http://the-automator.com/web-scraping-with-autohotkey/

Regards, Joe
Hi xenium,
Just curious...which of the numerous ideas in this thread wound up being the solution for you? Thanks, Joe
Avatar of xenium

ASKER

Joe, following on my comments in a related question, once i have this implemented i will review all associated questions and if appropriate then i'll request a re-assignment. It could be that i closed this question with the latest version of ee which does not seem to allow points splitting, whereas the classic view does, so this may need looking at next time. In this case as AutoHotKey is windows only i would unlikely use it as preference is for a server side solution. Thanks again for your input.
> once i have this implemented i will review all associated questions and if appropriate then i'll request a re-assignment

Fair enough.

> It could be that i closed this question with the latest version of ee which does not seem to allow points splitting

Yes, it does, but the technique is different from before, as explained in this updated Experts Exchange support article:
How do I close my question?

> In this case as AutoHotKey is windows only i would unlikely use it as preference is for a server side solution.

Yes, AutoHotkey is a client-side, Windows-only tool, but it can communicate with a server via HTTP calls (GET and POST), as it has native support for Component Object Model (COM) calls, such as WinHttpRequest.

> Thanks again for your input.

You're welcome. Regards, Joe