Skip to content

Commit

Permalink
Speed up HEIC plate extraction by using sharp to convert to JPEG
Browse files Browse the repository at this point in the history
This drops the latency from 6 seconds down to 1. However, it requires
libheif and libvips to be installed, so I need to figure out how to do
that on Heroku before this can be merged. On MacOS, this made it work
(see lovell/sharp#2004 (comment)):

```
brew install libheif
brew install libvips
yarn remove sharp && yarn add sharp
```

Fixes #265:

> See #264:
>
> > This adds about 6 seconds of latency since `heic-convert` is written in
> > JS, but it's better than nothing. Maybe I can get `sharp` working at
> > some point.
> > Fixes #263:
> > > Here's a test image [share.icloud.com/photos/0-5_Dy6WhkGw_7G-mlzadci-w#New_York](https://share.icloud.com/photos/0-5_Dy6WhkGw_7G-mlzadci-w#New_York)
> > > I suspect openalpr doesn't support HEIC, and we might need to convert to JPG on the server before sending to openalpr, see [lovell/sharp#2004](lovell/sharp#2004)
> > > If we can't get sharp working, try [npmjs.com/package/heic-convert](https://www.npmjs.com/package/heic-convert)
  • Loading branch information
josephfrazier committed Feb 11, 2021
1 parent 63c7328 commit 51f0761
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"geodist": "^0.2.1",
"geolib": "^2.0.24",
"global": "^4.3.2",
"heic-convert": "^1.2.4",
"heroku-self-ping": "^1.1.4",
"history": "^4.7.2",
"http-delayed-response": "^0.0.4",
Expand Down
17 changes: 9 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import multer from 'multer';
import stringify from 'json-stringify-safe';
import DelayedResponse from 'http-delayed-response';
import { JSDOM } from 'jsdom';
import heicConvert from 'heic-convert';

import { isImage, isVideo } from './isImage.js';
import { validateLocation, processValidation } from './geoclient.js';
Expand Down Expand Up @@ -491,6 +490,12 @@ app.use('/submit', (req, res) => {
});
});

function heicConvert({ buffer }) {
return sharp(buffer)
.toFormat('jpeg')
.toBuffer();
}

// adapted from https://github.com/openalpr/cloudapi/tree/8141c1ba57f03df4f53430c6e5e389b39714d0e0/javascript#getting-started
app.use('/openalpr', upload.single('attachmentFile'), async (req, res) => {
const country = 'us';
Expand All @@ -508,17 +513,13 @@ app.use('/openalpr', upload.single('attachmentFile'), async (req, res) => {
const secretKey = OPENALPR_SECRET_KEY; // {String} The secret key used to authenticate your account. You can view your secret key by visiting https://cloud.openalpr.com/

try {
console.time('heic-convert'); // eslint-disable-line no-console
attachmentBuffer = await heicConvert({
buffer: attachmentBuffer,
format: 'JPEG',
quality: 1,
});
console.time('heicConvert'); // eslint-disable-line no-console
attachmentBuffer = await heicConvert({ buffer: attachmentBuffer });
} catch (e) {
console.error('could not convert file from heic to jpg');
console.error(e);
} finally {
console.timeEnd('heic-convert'); // eslint-disable-line no-console
console.timeEnd('heicConvert'); // eslint-disable-line no-console
}

orientImageBuffer({ attachmentBuffer })
Expand Down
31 changes: 0 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5743,22 +5743,6 @@ hawk@~6.0.2:
hoek "4.x.x"
sntp "2.x.x"

heic-convert@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/heic-convert/-/heic-convert-1.2.4.tgz#605820f98ace3949a40fc7b263ee0bc573a0176b"
integrity sha512-klJHyv+BqbgKiCQvCqI9IKIvweCcohDuDl0Jphearj8+16+v8eff2piVevHqq4dW9TK0r1onTR6PKHP1I4hdbA==
dependencies:
heic-decode "^1.1.2"
jpeg-js "^0.4.1"
pngjs "^3.4.0"

heic-decode@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/heic-decode/-/heic-decode-1.1.2.tgz#974701666432e31ed64b2263a1ece7cff5218209"
integrity sha512-UF8teegxvzQPdSTcx5frIUhitNDliz/9Pui0JFdIqVRE00spVE33DcCYtZqaLNyd4y5RP/QQWZFIc1YWVKKm2A==
dependencies:
libheif-js "^1.10.0"

heroku-self-ping@^1.1.4:
version "1.1.5"
resolved "https://registry.yarnpkg.com/heroku-self-ping/-/heroku-self-ping-1.1.5.tgz#0547ca34c51c687347c8a1b7a5f278e6718f2bc6"
Expand Down Expand Up @@ -7080,11 +7064,6 @@ jmespath@0.15.0:
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.15.0.tgz#a3f222a9aae9f966f5d27c796510e28091764217"
integrity sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=

jpeg-js@^0.4.1:
version "0.4.3"
resolved "https://registry.yarnpkg.com/jpeg-js/-/jpeg-js-0.4.3.tgz#6158e09f1983ad773813704be80680550eff977b"
integrity sha512-ru1HWKek8octvUHFHvE5ZzQ1yAsJmIvRdGWvSoKV52XKyuyYA437QWDttXT8eZXDSbuMpHlLzPDZUPd6idIz+Q==

js-base64@^2.1.9:
version "2.4.5"
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.4.5.tgz#e293cd3c7c82f070d700fc7a1ca0a2e69f101f92"
Expand Down Expand Up @@ -7420,11 +7399,6 @@ levn@^0.3.0, levn@~0.3.0:
prelude-ls "~1.1.2"
type-check "~0.3.2"

libheif-js@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/libheif-js/-/libheif-js-1.10.0.tgz#414774cea65a783d603570967501c06a4d8bb885"
integrity sha512-o6lxGpy5RmO8aUMnDuHulkLd0g0QFWPWbZ5fjq2SM+E/xpeCTw5l+p//r8dmgKQiPzcXyitLr27gdGOLBIKBfg==

lie@*:
version "3.3.0"
resolved "https://registry.yarnpkg.com/lie/-/lie-3.3.0.tgz#dcf82dee545f46074daf200c7c1c5a08e0f40f6a"
Expand Down Expand Up @@ -9725,11 +9699,6 @@ pn@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"

pngjs@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-3.4.0.tgz#99ca7d725965fb655814eaf65f38f12bbdbf555f"
integrity sha512-NCrCHhWmnQklfH4MtJMRjZ2a8c80qXeMlQMv2uVp9ISJMTt562SbGd6n2oq0PaPgKm7Z6pL9E2UlLIhC+SHL3w==

point-in-polygon@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/point-in-polygon/-/point-in-polygon-1.0.1.tgz#d59b64e8fee41c49458aac82b56718c5957b2af7"
Expand Down

0 comments on commit 51f0761

Please sign in to comment.