gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Issue using gocv 0.35.0 in combination with opencv 4.8.1 in docker

Open tojaroslaw opened this issue 5 months ago • 1 comments

when I upgraded from opencv 4.5.5 to opencv 4.8.1, I suddenly needed to change my runtime environment to use opencv-dev in order to work with my gocv application and I am trying to understand why.

Description

I tried to run a docker image which used to build off of an old builder and runtime that used go 1.18.10, alpine 3.16.2, gocv 0.27.0, and opencv 4.5.5.

I upgraded to using these:

OpenCV version: 4.8.1
Alpine version: 3.19.0
Go version: 1.21.6
go-cv version: 0.35.0

When I upgraded everything to use the versions listed above, I got this error:

Error loading shared library libopencv_photo.so.408: No such file or directory (needed by ./main)
Error loading shared library libopencv_video.so.408: No such file or directory (needed by ./main)
Error relocating ./main: _ZN2cv13seamlessCloneERKNS_11_InputArrayES2_S2_NS_6Point_IiEERKNS_12_OutputArrayEi: symbol not found
Error relocating ./main: _ZN2cv27fastNlMeansDenoisingColoredERKNS_11_InputArrayERKNS_12_OutputArrayEffii: symbol not found
Error relocating ./main: _ZN2cv10TrackerMIL6createERKNS0_6ParamsE: symbol not found
Error relocating ./main: _ZN2cv12KalmanFilter4initEiiii: symbol not found
Error relocating ./main: _ZN2cv16findTransformECCERKNS_11_InputArrayES2_RKNS_17_InputOutputArrayEiNS_12TermCriteriaES2_i: symbol not found
Error relocating ./main: _ZN2cv11colorChangeERKNS_11_InputArrayES2_RKNS_12_OutputArrayEfff: symbol not found
Error relocating ./main: _ZN2cv29createBackgroundSubtractorKNNEidb: symbol not found
Error relocating ./main: _ZN2cv18illuminationChangeERKNS_11_InputArrayES2_RKNS_12_OutputArrayEff: symbol not found
Error relocating ./main: _ZN2cv13detailEnhanceERKNS_11_InputArrayERKNS_12_OutputArrayEff: symbol not found
Error relocating ./main: _ZN2cv30createBackgroundSubtractorMOG2Eidb: symbol not found
Error relocating ./main: _ZN2cv10TrackerMIL6ParamsC1Ev: symbol not found
Error relocating ./main: _ZN2cv18createMergeMertensEfff: symbol not found
Error relocating ./main: _ZN2cv20calcOpticalFlowPyrLKERKNS_11_InputArrayES2_S2_RKNS_17_InputOutputArrayERKNS_12_OutputArrayES8_NS_5Size_IiEEiNS_12TermCriteriaEid: symbol not found
Error relocating ./main: _ZN2cv17textureFlatteningERKNS_11_InputArrayES2_RKNS_12_OutputArrayEffi: symbol not found
Error relocating ./main: _ZN2cv12KalmanFilter7predictERKNS_3MatE: symbol not found
Error relocating ./main: _ZN2cv12KalmanFilter7correctERKNS_3MatE: symbol not found
Error relocating ./main: _ZN2cv20fastNlMeansDenoisingERKNS_11_InputArrayERKNS_12_OutputArrayEfii: symbol not found
Error relocating ./main: _ZN2cv14createAlignMTBEiib: symbol not found
Error relocating ./main: _ZN2cv12KalmanFilterC1Eiiii: symbol not found
Error relocating ./main: _ZN2cv32fastNlMeansDenoisingColoredMultiERKNS_11_InputArrayERKNS_12_OutputArrayEiiffii: symbol not found
Error relocating ./main: _ZN2cv7inpaintERKNS_11_InputArrayES2_RKNS_12_OutputArrayEdi: symbol not found
Error relocating ./main: _ZN2cv11stylizationERKNS_11_InputArrayERKNS_12_OutputArrayEff: symbol not found
Error relocating ./main: _ZN2cv24calcOpticalFlowFarnebackERKNS_11_InputArrayES2_RKNS_17_InputOutputArrayEdiiiidi: symbol not found
Error relocating ./main: _ZN2cv20edgePreservingFilterERKNS_11_InputArrayERKNS_12_OutputArrayEiff: symbol not found
Error relocating ./main: _ZN2cv12pencilSketchERKNS_11_InputArrayERKNS_12_OutputArrayES5_fff: symbol not found

I was able to fix this by changing the apk installed version of go-cv in the runtime from opencv=4.8.1 to opencv-dev=4.8.1. I'm not sure what changed, but I'm wondering why I now need to install the dev version.

Steps to Reproduce

I have a go service which uses gocv 0.35.0 and build it in a docker image which looks like this:

# Example taken from: https://github.com/confluentinc/confluent-kafka-go/issues/461#issuecomment-617591791
# Start from the latest golang base image
FROM public.ecr.aws/p3a4b6b2/arthurai/gocv-alpine-builder:go1.21.6-alpine3.19-opencv4.8.1-r0-arthur-1 as builder

ARG PRIVATE_TOKEN
ENV GO111MODULE=on
ENV GOPRIVATE

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app
# amd64 = x86-64
# ldfalgs -w -s remove debug and pproff tools
# -a forces a rebuild on every run
# installsuffix remove local directory names from exceptions
# CGO_ENABLED=1 enables cross compiling
# librdkafka dependency:
#   When building your application for Alpine Linux (musl libc) you must pass -tags musl to go get, go build, etc.
#   CGO_ENABLED must NOT be set to 0 since the Go client is based on the C library librdkafka.
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -tags musl -a -ldflags="-w -s" -installsuffix cgo -o main ./cmd/api


######## Start a new stage from scratch #######
FROM public.ecr.aws/arthur/arthurai/gocv-alpine-runtime:alpine3.19.0-opencv4.8.1-r0-arthur-1

# Change to root to copy files
USER 0

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/main .
EXPOSE 8082

USER 1000
# Command to run the executable, be sure to use exec form for proper signal passing
# https://docs.docker.com/engine/reference/builder/#entrypoint
ENTRYPOINT ["./main"]

The runtime image looks like this:

FROM alpine:3.19.0

RUN apk update
RUN apk add git opencv=4.8.1-r0

This caused the error I posted. I was able to fix it by switching to use opencv-dev, but I'm trying to understand why.

FROM alpine:3.19.0

RUN apk update
RUN apk add git opencv-dev=4.8.1-r0

I didn't need this before when I was using go1.18.10, alpine 3.16.2, and open-cv 4.5.5

Your Environment

OpenCV version: 4.8.1 Alpine version: 3.19.0 Go version: 1.21.6 go-cv version: 0.35.0

tojaroslaw avatar Jan 30 '24 23:01 tojaroslaw