bounty将在3天内到期。回答此问题有资格获得+50声望奖励。Asif khAn正在寻找来自信誉良好的来源的答案:指导我如何克服这个问题,这对我很有帮助。
我做了一个简单的scala应用程序,它使用OpenCV-4.8.0通过Haar-Cascade(haarcascade_frontalface_default.xml
)从图像中提取人脸。
以上功能在本地机器上运行正常。
现在我尝试通过dockerization来实现相同的功能,为此我遵循了以下步骤。
这里是build.sbt
,我们通过sbt-native-packager创建一个Image,然后通过dockerCommands
安装OpenCV-4.8.0。
import com.typesafe.sbt.packager.docker._
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.4"
lazy val root = (project in file("."))
.settings(
name := "human-verification-scala-service"
)
unmanagedBase := file("opencv4")
enablePlugins(JavaAppPackaging, DockerPlugin)
Docker / packageName := "human-verification-test"
Universal / javaOptions += "-Djava.library.path=/opencv4"
Docker / version := "0.0.1"
dockerExposedPorts := Seq(8083)
dockerChmodType := DockerChmodType.UserGroupWriteExecute
dockerPermissionStrategy := DockerPermissionStrategy.CopyChown
dockerAdditionalPermissions += (DockerChmodType.UserGroupPlusExecute, "/var/run/")
Docker / daemonUserUid := None
Docker / daemonUser := "root"
dockerBaseImage := "amazoncorretto:11-alpine3.18-jdk"
dockerCommands ++= Seq(
Cmd("RUN", "apk", "add", "--no-cache", "bash make cmake g++ wget unzip"),
Cmd("RUN", "apk", "add", "--no-cache", "linux-headers"),
Cmd("RUN", "wget", "-O", "opencv.zip", "https://github.com/opencv/opencv/archive/4.8.0.zip"),
Cmd("RUN", "unzip", "opencv.zip && rm opencv.zip"),
Cmd("RUN", "mkdir", "-p", "build && cd build && cmake ../opencv-4.8.0 && make -j4 && make install")
)
fork := true
libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.4.2",
"com.typesafe.akka" %% "akka-actor" % "2.6.0",
"com.typesafe.akka" %% "akka-stream" % "2.7.0",
"com.typesafe.akka" %% "akka-http" % "10.5.2",
"ch.qos.logback" % "logback-classic" % "1.4.11",
"com.typesafe.akka" %% "akka-http-spray-json" % "10.5.2",
"io.netty" % "netty-transport-native-epoll" % "4.1.99.Final" classifier "linux-x86_64"
)
字符串
->在opencv 4目录中,我们有OpenCV-4.8.0的.jar和**.so**文件用于编译代码,我正在创建它的docker卷。
**->**OpenCV在docker中运行良好,我在另一个项目中也测试过。
下面是代码:
object CropImage extends App {
def getFace(imagePath: String): Mat = {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
val faceCascade = new CascadeClassifier("/opt/docker/opencv-4.8.0/data/haarcascades/haarcascade_frontalface_default.xml")
val image = Imgcodecs.imread(imagePath)
val gray = new Mat()
Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY)
val faces = new MatOfRect()
faceCascade.detectMultiScale(gray, faces)
if (!faces.empty()) {
val faceRectArray = faces.toArray
val faceRect = faceRectArray(0)
val padding = 220
val startY = Math.max(0, faceRect.y - padding)
val endY = Math.min(image.rows(), faceRect.y + faceRect.height + padding)
val startX = Math.max(0, faceRect.x)
val endX = Math.min(image.cols(), faceRect.x + faceRect.width)
new Mat(image, new Rect(new Point(startX, startY), new Point(endX, endY)))
}
else {
new Mat()
}
}
getFace("/src/main/resources/MyImage.png")
}
型
->/opt/docker/opencv-4.8.0/data/haarcascades/haarcascade_frontalface_default.xml
在Docker镜像中安装OpenCV后可用。
在sbt-shell中执行docker:publishLocal
之后,它下载了OpenCV并创建了docker镜像。
然后我使用docker-compose up
运行图像,这里是docker-compose.yml
:
version: '3'
services:
scala-service:
image: human-verification-test:0.0.1
volumes:
- ./src/main/resources:/src/main/resources
- ./opencv4:/opencv4
ports:
- "8083:8083"
型
我得到了一个错误:
scala-service_1 | # A fatal error has been detected by the Java Runtime Environment:
scala-service_1 | #
scala-service_1 | # SIGSEGV (0xb) at pc=0x00007f6a21b60ca9, pid=1, tid=81
scala-service_1 | #
scala-service_1 | # JRE version: OpenJDK Runtime Environment Corretto-11.0.21.9.1 (11.0.21+9) (build 11.0.21+9-LTS)
scala-service_1 | # Java VM: OpenJDK 64-Bit Server VM Corretto-11.0.21.9.1 (11.0.21+9-LTS, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
scala-service_1 | # Problematic frame:
scala-service_1 | # C [libopencv_java480.so+0x141ca9] Java_org_opencv_objdetect_CascadeClassifier_CascadeClassifier_11+0xf9
scala-service_1 | #
scala-service_1 | # Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport -p%p -s%s -c%c -d%d -P%P -u%u -g%g -- %E" (or dumping to /opt/docker/core.1)
scala-service_1 | #
scala-service_1 | # An error report file with more information is saved as:
scala-service_1 | # /opt/docker/hs_err_pid1.log
humanverificationscalaservice_scala-service_1 exited with code 139
型
以下是hs_err_pid1.log
:
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j org.opencv.objdetect.CascadeClassifier.CascadeClassifier_1(Ljava/lang/String;)J+0
j org.opencv.objdetect.CascadeClassifier.<init>(Ljava/lang/String;)V+6
j blurriness.CheckBlurry$.getFace(Ljava/lang/String;Ljava/lang/String;)Lorg/opencv/core/Mat;+11
j blurriness.CheckBlurry$.isImageBlurry(Ljava/lang/String;Ljava/lang/String;)Lscala/Tuple2;+9
j faceverification.FaceVerification$.$anonfun$routes$7(Lscala/util/Try;)Lscala/Function1;+222
j faceverification.FaceVerification$$$Lambda$684.apply(Ljava/lang/Object;)Ljava/lang/Object;+4
j akka.http.scaladsl.server.util.ApplyConverterInstances$$anon$1.$anonfun$apply$1(Lscala/Function1;Lscala/Tuple1;)Lscala/Function1;+15
j akka.http.scaladsl.server.util.ApplyConverterInstances$$anon$1$$Lambda$325.apply(Ljava/lang/Object;)Ljava/lang/Object;+8
j akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$3(Lscala/Function1;Lakka/http/scaladsl/server/RequestContext;Lscala/util/Try;)Lscala/concurrent/Future;+9
型
指导我如何解决上述错误,并在docker container
中使用haar-cascade
。
1条答案
按热度按时间uqdfh47h1#
阅读“Build OpenCV.js {#tutorial_js_setup}“,你可能会考虑,作为一种替代方法,使用Docker将OpenCV.js构建成一个multi-stage build。
在第一阶段创建一个编译OpenCV.js的Dockerfile,然后将必要的文件复制到第二阶段。这种方法将构建环境与最终镜像分离,从而减少最终Docker镜像的大小和复杂性。
Dockerfile的最后阶段将基于适合运行Scala应用程序的映像。如果您在Scala项目中使用sbt (Scala Build Tool),则可以从Java映像开始,因为Scala运行在JVM上。
字符串
该Dockerfile首先在第一阶段构建OpenCV.js,然后在第二阶段为Scala项目使用sbt设置Java环境。它将编译后的OpenCV.js文件复制到Scala应用程序阶段,然后编译Scala应用程序。
要使用此Dockerfile:
docker build -t your-scala-app .
构建Docker镜像docker run -it --rm your-scala-app
在Docker容器中运行Scala应用程序。这将假设您的Scala应用程序已正确配置为使用OpenCV.js,可能是通过JavaScript或WebAssembly运行时环境。