-
Notifications
You must be signed in to change notification settings - Fork 11
add opencv #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yinjun622
wants to merge
11
commits into
QuadFlask:master
Choose a base branch
from
yinjun622:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add opencv #3
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9cb42c1
add opencv
e60ab7b
add boost
yinjun622 bb509a0
add boost
yinjun622 50fbc57
add opencv
6e059dd
add opencv
12d17f2
add opencv
aaf38f3
update version
8d81626
update version
d64eb23
update version
yinjun622 2f6d79c
improve crop face
yinjun622 cdb0d91
improve crop face
yinjun622 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/main/java/com/github/quadflask/smartcrop/OpencvDetect.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package com.github.quadflask.smartcrop; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.opencv.core.*; | ||
| import org.opencv.imgcodecs.Imgcodecs; | ||
| import org.opencv.objdetect.CascadeClassifier; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
| import java.awt.image.BufferedImage; | ||
| import java.awt.image.DataBufferByte; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| public class OpencvDetect { | ||
|
|
||
| private static OpencvDetect instance = new OpencvDetect(); | ||
| private static CascadeClassifier cascadeClassifier; | ||
| private static String frontalfacePath; | ||
|
|
||
| static { | ||
| try { | ||
| String osName = System.getProperty("os.name"); | ||
| if (osName.startsWith("Windows")) { | ||
| System.loadLibrary("lib" + Core.NATIVE_LIBRARY_NAME); | ||
| } else if (osName.startsWith("Mac")) { | ||
| System.load("/usr/local/lib/lib" + Core.NATIVE_LIBRARY_NAME + ".dylib"); | ||
|
|
||
| } else if (osName.startsWith("Linux")) { | ||
| System.load("/usr/local/lib/lib" + Core.NATIVE_LIBRARY_NAME + ".so"); | ||
| } | ||
| } catch (Throwable t) { | ||
| log.error("Load OpenCV lib error.", t); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static OpencvDetect getInstance() { | ||
| return instance; | ||
| } | ||
|
|
||
|
|
||
| public Crop[] detectFace(String imagePath) { | ||
| Mat image = Imgcodecs.imread(imagePath); | ||
| MatOfRect faceDetections = new MatOfRect(); | ||
| cascadeClassifier.detectMultiScale(image, faceDetections); | ||
| Rect[] rects = faceDetections.toArray(); | ||
| return toCrop(rects); | ||
| } | ||
|
|
||
| public Crop[] detectFace(byte[] imageBase64) { | ||
|
|
||
| try { | ||
| BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imageBase64)); | ||
| return detectFace(bi); | ||
|
|
||
| } catch (Exception e) { | ||
| log.error("dateface error.", e); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
|
|
||
| public Crop[] detectFace(BufferedImage bi) { | ||
|
|
||
| try { | ||
| Mat image = img2Mat(bi); | ||
| MatOfRect faceDetections = new MatOfRect(); | ||
| cascadeClassifier.detectMultiScale(image, faceDetections); | ||
| Rect[] rects = faceDetections.toArray(); | ||
| return toCrop(rects); | ||
| } catch (Throwable t) { | ||
|
|
||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * the file is '~/opencvXX/haarcascades/haarcascade_frontalface_alt.xml"' | ||
| * | ||
| * @param frontalfacePath | ||
| */ | ||
| public void SetFrontalFacePath(String frontalfacePath) { | ||
| this.frontalfacePath = frontalfacePath; | ||
| cascadeClassifier = new CascadeClassifier(frontalfacePath); | ||
| } | ||
|
|
||
|
|
||
| private Crop[] toCrop(Rect[] rect) { | ||
|
|
||
| List<Crop> cropList = new ArrayList<>(); | ||
| if (rect != null) { | ||
|
|
||
| Arrays.stream(rect).forEach(r -> cropList.add(new Crop(r.x, r.y, r.width, r.height))); | ||
|
|
||
| } | ||
|
|
||
| Crop[] crops = new Crop[cropList.size()]; | ||
| cropList.toArray(crops); | ||
| return crops; | ||
| } | ||
|
|
||
|
|
||
| private Mat img2Mat(BufferedImage im) { | ||
| // Convert INT to BYTE | ||
| //im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR); | ||
| // Convert bufferedimage to byte array | ||
| byte[] pixels = ((DataBufferByte) im.getRaster().getDataBuffer()).getData(); | ||
|
|
||
| // Create a Matrix the same size of image | ||
| Mat image = new Mat(im.getHeight(), im.getWidth(), CvType.CV_8UC3); | ||
| // Fill Matrix with image values | ||
| image.put(0, 0, pixels); | ||
|
|
||
| return image; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this classifier use a grey scale image?