peterbraden / node-opencv

OpenCV Bindings for node.js
MIT License
4.37k stars 858 forks source link

npm install fails with OpenCV 4.0.1 #648

Closed jasonpolites closed 4 years ago

jasonpolites commented 5 years ago

Describe the bug Documentation suggests "You'll need OpenCV 2.3.1 or newer installed before installing node-opencv" but find-opencv.js fails with opencv4 installed.

The following error emerges during npm install

npm WARN deprecated istanbul@0.4.5: This module is no longer maintained, try this instead:
npm WARN deprecated   npm i nyc
npm WARN deprecated Visit https://istanbul.js.org/integrations for other alternatives.

> opencv@6.2.0 install /usr/src/app/node_modules/opencv
> node-gyp rebuild

/usr/src/app/node_modules/opencv/utils/find-opencv.js:28
              throw new Error("ERROR: failed to run: pkg-config" + opencv + " " + flag + " - Is OpenCV installed?");
              ^

Error: ERROR: failed to run: pkg-config "opencv >= 2.3.1" --cflags - Is OpenCV installed?
    at /usr/src/app/node_modules/opencv/utils/find-opencv.js:28:21
    at ChildProcess.exithandler (child_process.js:301:5)
    at ChildProcess.emit (events.js:189:13)
    at maybeClose (internal/child_process.js:970:16)
    at Socket.stream.socket.on (internal/child_process.js:389:11)
    at Socket.emit (events.js:189:13)
    at Pipe._handle.close (net.js:597:12)

To Reproduce Steps to reproduce the behavior:

I am building open CV from source on linux:

(the following is from a Docker file, but could be turned to shell commands)

RUN git clone --progress --verbose https://github.com/opencv/opencv.git
WORKDIR /usr/src/app/opencv/
RUN git checkout tags/4.0.1
WORKDIR /usr/src/app/opencv/build

# Make sure to generate a pkg-config so node-opencv can find us
RUN cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..
RUN make install

Manually running pkg-config yields:

pkg-config --cflags -- opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4

Expected behavior node-opencv should work with opencv 4 (feature request), or perhaps the docs should state that it only works up to 3.x (bug)

OS / Libraries Ubuntu 10.04 OpenCV 4.0.1

johcam12 commented 5 years ago

Doc did say that, but the remarks was removed at commit aaece6921d7368577511f06c94c99dd4e9653563

"-You'll need OpenCV 2.3.1 or newer installed before installing node-opencv. Note -that OpenCV 3.x is not yet fully supported."

peterbraden commented 5 years ago

PR would be welcome to add opencv4 support to find-opencv.js

Things may break - I assume we'll need macros to change header files across the library.

mcfly3001 commented 5 years ago

Hi, I also just tried to get opencv running and stumbled across the same issue (of course trying latest and greatest OpenCV 4.0.1). I would propose to simply update the readme telling which versions are precisely supported. This could save a lot of time and frustration when trying to get started :-) Thanks!

jasonpolites commented 5 years ago

FWIW I gave up on 4.x. This is the Dockerfile I ultimately used (also has ffmpeg, but you can ignore that)

# Use the official Node.js 10 image.
# https://hub.docker.com/_/node
FROM node:10

# Install open cv and ffmpeg
# opencv install taken from https://docs.opencv.org/3.4/d7/d9f/tutorial_linux_install.html
RUN apt-get update && apt-get install -y \
    ffmpeg \
    cmake \
    git \
    libgtk2.0-dev \
    pkg-config \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    imagemagick

# Create and change to the app directory.
WORKDIR /usr/src/app

# build & install open cv
# taken from https://docs.opencv.org/3.4/d7/d9f/tutorial_linux_install.html
RUN git clone --progress --verbose https://github.com/opencv/opencv.git
WORKDIR /usr/src/app/opencv/

# node-opencv only works with 2.x or 3.x
RUN git checkout tags/3.4.5
WORKDIR /usr/src/app/opencv/build

# Make sure to generate a pkg-config so node-opencv can find us
RUN cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..
RUN make install

# Move back to workdir
WORKDIR /usr/src/app

# Ensure library references are updated
# (https://stackoverflow.com/questions/55267662/error-libopencv-dnn-so-3-4-cannot-open-shared-object-file-when-using-node-open)
RUN ldconfig -v

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./

# Install production dependencies.
RUN npm install --only=production

# Copy local code to the container image.
COPY . .

# Ensure there's an output folder for video extraction
RUN mkdir /tmp

# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080

# Run the web service on container startup.
CMD [ "npm", "start" ]
guimard commented 4 years ago

@peterbraden: do you plan to release a new node-opencv compatible with last opencv ?

peterbraden commented 4 years ago

Hopefully works now?

f4z3k4s commented 3 years ago

Sadly, it doesn't. At least, on Mac. Works only with 2.x.

igoraugustynski commented 1 year ago

The "Porting to OpenCV4" issue has been merged. Is this working now?

ohpyupi commented 1 year ago

It doesn't seem to work.