Ver Fonte

修改模板匹配算法,提高精度。 增加设计类型工时。

seyason há 5 anos atrás
pai
commit
7bdad0d217

+ 1 - 0
fhKeeper/formulahousekeeper/management-platform/build_package.bat

@@ -0,0 +1 @@
+mvn package

+ 63 - 36
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ScreenshotServiceImpl.java

@@ -57,6 +57,8 @@ public class ScreenshotServiceImpl extends ServiceImpl<ScreenshotMapper, Screens
     private String developFolder;
     @Value(value = "${picrecongnize.im}")
     private String imFolder;
+    @Value(value = "${picrecongnize.design}")
+    private String designFolder;
 
     @Value("classpath:novel_words.data")
     private org.springframework.core.io.Resource novelWords;
@@ -299,47 +301,48 @@ public class ScreenshotServiceImpl extends ServiceImpl<ScreenshotMapper, Screens
                 textContents = (List<String>) picResultMap.get("wordsList");
                 screenshot.setPicContext((String) picResultMap.get("picContent"));
             }
-        }
-        if (isNovel(textContents)) {
-            screenshot.setPicType(6);
-        } else if (isDocument(textContents)) {
-            screenshot.setPicType(2);//看文档
-        } else if (isDevelop(picFile) != null) {//开发
-            screenshot.setPicType(0);
-        }
-        screenshot.setIsHandle(1);
-        if (screenshot.getPicType() == null && isEntertainmentColorMode(picFile)) {
-            //判断是否是娱乐:看电影, 打游戏
-            String uid = screenshotvo.getUid();
-            Screenshot preShot = screenshotMapper.selectOne(new QueryWrapper<Screenshot>().eq("uid", uid).orderByDesc("indate").last("limit 1"));
-            if (preShot != null) {
-                String prePath = path + preShot.getPicUrl().substring(preShot.getPicUrl().lastIndexOf("/"));
-                File f = new File(prePath);
-                if (f.exists()) {
-                    ImageCompare comp = new ImageCompare();
-                    if (comp.isMoviePlay((String)fileMap.get("newFile"), prePath)) {
-                        screenshot.setPicType(7);
-                        //前面那条也更新
-                        if (preShot.getPicType() != null && preShot.getPicType() != 7) {
-                            preShot.setPicType(7);
-                            screenshotMapper.updateById(preShot);
+            if (isNovel(textContents)) {
+                screenshot.setPicType(6);
+            } else if (isDocument(textContents)) {
+                screenshot.setPicType(2);//看文档
+            } else if (isDevelop(picFile) != null) {//开发
+                screenshot.setPicType(0);
+            } else if (isDesign(picFile) != null) {//设计
+                screenshot.setPicType(3);
+            }
+            screenshot.setIsHandle(1);
+            if (screenshot.getPicType() == null && isEntertainmentColorMode(picFile)) {
+                //判断是否是娱乐:看电影, 打游戏
+                String uid = screenshotvo.getUid();
+                Screenshot preShot = screenshotMapper.selectOne(new QueryWrapper<Screenshot>().eq("uid", uid).orderByDesc("indate").last("limit 1"));
+                if (preShot != null) {
+                    String prePath = path + preShot.getPicUrl().substring(preShot.getPicUrl().lastIndexOf("/"));
+                    File f = new File(prePath);
+                    if (f.exists()) {
+                        ImageCompare comp = new ImageCompare();
+                        if (comp.isMoviePlay((String)fileMap.get("newFile"), prePath)) {
+                            screenshot.setPicType(7);
+                            //前面那条也更新
+                            if (preShot.getPicType() != null && preShot.getPicType() != 7) {
+                                preShot.setPicType(7);
+                                screenshotMapper.updateById(preShot);
+                            }
                         }
                     }
                 }
             }
-        }
-        if (screenshot.getPicType() == null) {
-            //默认设置为查资料
-            try {
-                String browserName = isBrowser(picFile);
-                if (browserName != null) {
-                    screenshot.setPicType(1);
+            if (screenshot.getPicType() == null) {
+                try {
+                    String browserName = isBrowser(picFile);
+                    if (browserName != null) {
+                        screenshot.setPicType(1);
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
                 }
-            } catch (Exception e) {
-                e.printStackTrace();
             }
-
         }
+
         //将获取到的截图进行时间计算
         calculateTime(screenshot);
         screenshotMapper.insert(screenshot);
@@ -456,7 +459,7 @@ public class ScreenshotServiceImpl extends ServiceImpl<ScreenshotMapper, Screens
     }
 
     public static void main(String[] args) throws Exception {
-//        String b = isDevelop(new File("C:\\Users\\seya\\Desktop\\sqlyog_pt.jpg"));
+//        String b = isDesign(new File("C:\\Users\\seya\\Desktop\\mock.jpg"));
 //        System.out.println("结果:"+b);
     }
 
@@ -552,7 +555,31 @@ public class ScreenshotServiceImpl extends ServiceImpl<ScreenshotMapper, Screens
         }
         return null;
     }
-
+    private String isDesign(File pic) {
+        File folder = new File(designFolder);
+//        File folder = new File("C:\\picrecongnize\\design\\");
+        if (!folder.exists()) {
+            try {
+                throw new Exception("没有设置图片上传的设计工具比对模板库");
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        } else {
+            File[] files = folder.listFiles();
+            String toolName = null;
+            boolean isMatch = false;
+            for (File targetPic : files) {
+//                System.out.println("targetPic==" + targetPic.getAbsolutePath());
+                boolean matchPic = ImageReconizeUtil.isTemplateMatch(pic.getAbsolutePath(), targetPic.getAbsolutePath());
+                if (matchPic) {
+                    toolName = targetPic.getName();
+                    break;
+                }
+            }
+            return toolName;
+        }
+        return null;
+    }
     //判断是否是聊天
     private String isIM(File pic) {
         File folder = new File(imFolder);

+ 24 - 29
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/ImageReconizeUtil.java

@@ -1,6 +1,7 @@
 package com.management.platform.util;
 
 import org.opencv.core.*;
+import org.opencv.features2d.ORB;
 import org.opencv.highgui.HighGui;
 import org.opencv.imgcodecs.Imgcodecs;
 import org.opencv.imgproc.Imgproc;
@@ -14,30 +15,23 @@ import static java.lang.Math.E;
 import static org.opencv.imgproc.Imgproc.*;
 
 public class ImageReconizeUtil {
-    public static final double YUZHI_HIGH = 2*Math.pow(0.1, 11);
-    public static final double YUZHI_NORMAL = 1*Math.pow(0.1, 10);
+//    public static final double YUZHI_HIGH = 2*Math.pow(0.1, 11);
+//    public static final double YUZHI_NORMAL = 1*Math.pow(0.1, 10);
+    public static final double YUZHI = 0.89;
     static {
         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
         //注意程序运行的时候需要在VM option添加该行 指明opencv的dll文件所在路径
         //-Djava.library.path=$PROJECT_DIR$\opencv\x64
     }
     public static void main(String[] args) {
-        boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\my.jpg",
-                "C:\\picrecongnize\\develop\\idea_black.jpg");
-//        match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome_gray.jpg",
-//                "C:\\\\picrecongnize\\\\Chrome\\\\4-125.jpg");
-//        match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome_gray.jpg",
-//                "C:\\\\picrecongnize\\\\Chrome\\\\2-125.jpg");
-//        match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome_gray.jpg",
-//                "C:\\\\picrecongnize\\\\Chrome\\\\3-125.jpg");
+        boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\cc.jpg",
+                "C:\\picrecongnize\\browser\\CHrome\\chrome.jpg");
 
-//        boolean match = isTemplateMatch("C:\\Users\\seya\\Desktop\\chrome2.jpg",
-//                "C:\\picrecongnize\\Chrome\\bar125.jpg");
     }
 
     public static boolean isTemplateMatch(String sourcePic, String targetPic) {
-        double matchVal = templete(Imgproc.TM_SQDIFF_NORMED, sourcePic, targetPic,true);
-        if (Math.abs(matchVal) > 0 && Math.abs(matchVal) < YUZHI_HIGH) {
+        double matchVal = templete(TM_CCOEFF_NORMED, sourcePic, targetPic,true);
+        if (matchVal >= YUZHI) {
             System.out.println("找到啦");
             return true;
         } else {
@@ -47,8 +41,8 @@ public class ImageReconizeUtil {
     }
 
     public static boolean isWholeTemplateMatch(String sourcePic, String targetPic) {
-        double matchVal = templete(Imgproc.TM_SQDIFF_NORMED, sourcePic, targetPic, false);
-        if (Math.abs(matchVal) > 0 && Math.abs(matchVal) < YUZHI_HIGH) {
+        double matchVal = templete(Imgproc.TM_CCOEFF_NORMED, sourcePic, targetPic, false);
+        if (matchVal >= YUZHI) {
             System.out.println("找到啦"+targetPic);
             return true;
         } else {
@@ -71,25 +65,25 @@ public class ImageReconizeUtil {
      * @return: void
      * @date: 2019年5月7日12:16:55
      */
-    public static double templete(int method, String sourcePic, String targetPic, boolean cutMatch) {
+    public static double templete(int method, String inFile, String templateFile, boolean cutMatch) {
         // 1 获取待匹配图片
 //        System.out.println("sourcePic="+sourcePic+", targetPic="+targetPic);
-        Mat templete=Imgcodecs.imread(sourcePic);
+        Mat srcMat = Imgcodecs.imread(inFile);
         if (cutMatch) {
             //先裁减,左上角1/4的截图,提高比对速度。
-            Rect rect = new Rect(0,0,templete.width()/4, templete.height()/4);
-            Mat subMat = new Mat(templete, rect);
-            subMat.copyTo(templete);
+            Rect rect = new Rect(0,0,srcMat.width()/4, srcMat.height()/4);
+            Mat subMat = new Mat(srcMat, rect);
+            subMat.copyTo(srcMat);
         }
 
         //将rgb灰化处理
 //        Imgproc.cvtColor(templete, templete,Imgproc.COLOR_BGR2GRAY);
 //
         // 2 获取匹配模板
-        Mat demo=Imgcodecs.imread(targetPic);
+        Mat demo = Imgcodecs.imread(templateFile);
 //        Imgproc.cvtColor(demo, demo,Imgproc.COLOR_BGR2GRAY);
 
-        Core.MinMaxLocResult mmr = getLocResult(method, templete, demo);
+        Core.MinMaxLocResult mmr = getLocResult(method, srcMat, demo);
         // 7 绘制匹配到的结果
         double x,y;
         double matchVal = 0;
@@ -103,15 +97,15 @@ public class ImageReconizeUtil {
             matchVal = mmr.maxVal;
         }
 
-        System.out.println("匹配度=="+matchVal+", target="+targetPic);
+        System.out.println("匹配度=="+matchVal+", target="+templateFile.substring(templateFile.lastIndexOf(File.separator)-10)+", inFile="+inFile.substring(inFile.lastIndexOf(File.separator)));
         System.out.println("x="+x+", y=" + y);
         //我们匹配的图像在左上角,考虑到用户可能拖动窗口,但是不应该偏差很大。 这里增加判断标准:坐标处在左上方。
 
-//        if (Math.abs(matchVal) < YUZHI_NORMAL) {
-//            Imgproc.rectangle(templete,new Point(x,y),new Point(x+demo.cols(),y+demo.rows()),new Scalar( 0, 0, 255),2,Imgproc.LINE_AA);
-//            Imgproc.putText(templete,"Match Success",new Point(x,y),Imgproc.FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA);
+//        if (matchVal >= YUZHI) {
+//            Imgproc.rectangle(srcMat,new Point(x,y),new Point(x+demo.cols(),y+demo.rows()),new Scalar( 0, 0, 255),2,Imgproc.LINE_AA);
+//            Imgproc.putText(srcMat,"Match Success",new Point(x,y),Imgproc.FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, new Scalar(0, 255, 0),1,Imgproc.LINE_AA);
 //            // 8 显示结果
-//            HighGui.imshow("模板匹配", templete);
+//            HighGui.imshow("模板匹配", srcMat);
 //            HighGui.waitKey(0);
 //        }
 
@@ -126,9 +120,10 @@ public class ImageReconizeUtil {
         // 4 调用 模板匹配函数
         Imgproc.matchTemplate(templete, demo, result, method);
         // 5 归一化
-        Core.normalize(result, result,0, 1, Core.NORM_MINMAX, -1, new Mat());
+//        Core.normalize(result, result,0, 1, Core.NORM_MINMAX, -1, new Mat());
         // 6 获取模板匹配结果
         Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
         return mmr;
     }
+
 }

+ 1 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/resources/application-dev.yml

@@ -84,6 +84,7 @@ picrecongnize:
   browser: E:/picrecongnize/browser/
   develop: E:/picrecongnize/develop/
   im: E:/picrecongnize/im/
+  design: E:/picrecongnize/design/
 
 
 

+ 1 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/resources/application-prod.yml

@@ -85,6 +85,7 @@ picrecongnize:
   browser: /www/webapps/worktime/picrecongnize/browser/
   develop: /www/webapps/worktime/picrecongnize/develop/
   im: /www/webapps/worktime/picrecongnize/im/
+  design: /www/webapps/worktime/picrecongnize/design/
 
 
 

+ 1 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/resources/application.yml

@@ -85,5 +85,6 @@ picrecongnize:
   browser: C:/picrecongnize/browser/
   develop: C:/picrecongnize/develop/
   im: C:/picrecongnize/im/
+  design: C:/picrecongnize/design/