1 /*
2 Copyright (c) 2019 Ferhat Kurtulmuş
3 Boost Software License - Version 1.0 - August 17th, 2003
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 The copyright notices in the Software and this entire statement, including
11 the above license grant, this restriction and the following disclaimer,
12 must be included in all copies of the Software, in whole or in part, and
13 all derivative works of the Software, unless such copies or derivative
14 works are solely in the form of machine-executable object code generated by
15 a source language processor.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
19 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
20 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24 
25 module opencvd.objdetect;
26 
27 import std.string;
28 
29 import opencvd.cvcore;
30 
31 private extern (C){
32     CascadeClassifier CascadeClassifier_New();
33     void CascadeClassifier_Close(CascadeClassifier cs);
34     int CascadeClassifier_Load(CascadeClassifier cs, const char* name);
35     Rects CascadeClassifier_DetectMultiScale(CascadeClassifier cs, Mat img);
36     Rects CascadeClassifier_DetectMultiScaleWithParams(CascadeClassifier cs, Mat img,
37             double scale, int minNeighbors, int flags, Size minSize, Size maxSize);
38 
39     HOGDescriptor HOGDescriptor_New();
40     void HOGDescriptor_Close(HOGDescriptor hog);
41     int HOGDescriptor_Load(HOGDescriptor hog, const char* name);
42     Rects HOGDescriptor_DetectMultiScale(HOGDescriptor hog, Mat img);
43     Rects HOGDescriptor_DetectMultiScaleWithParams(HOGDescriptor hog, Mat img,
44             double hitThresh, Size winStride, Size padding, double scale, double finalThreshold,
45             bool useMeanshiftGrouping);
46     Mat HOG_GetDefaultPeopleDetector();
47     void HOGDescriptor_SetSVMDetector(HOGDescriptor hog, Mat det);
48 
49     Rects GroupRectangles(Rects rects, int groupThreshold, double eps);
50 }
51 
52 struct _CascadeClassifier {
53     void* p;
54     
55     void close(){
56         CascadeClassifier_Close(&this);
57     }
58     
59     int load(string name){
60         return CascadeClassifier_Load(&this, toStringz(name));
61     }
62     
63     Rects detectMultiScale(Mat img){
64         return CascadeClassifier_DetectMultiScale(&this, img);
65     }
66     
67     Rects detectMultiScaleWithParams(Mat img, double scale, int minNeighbors,
68                                     int flags, Size minSize, Size maxSize){
69         return CascadeClassifier_DetectMultiScaleWithParams(&this, img, scale, 
70             minNeighbors, flags, minSize, maxSize);
71     }
72 }
73 
74 alias CascadeClassifier = _CascadeClassifier*;
75 
76 CascadeClassifier newCascadeClassifier(){
77     return CascadeClassifier_New();
78 }
79 
80 struct _HOGDescriptor {
81     void* p;
82     
83     void close(){
84         HOGDescriptor_Close(&this);
85     }
86     
87     int load(string name){
88         return HOGDescriptor_Load(&this, toStringz(name));
89     }
90     
91     Rects detectMultiScale(Mat img){
92         return HOGDescriptor_DetectMultiScale(&this, img);
93     }
94     
95     Rects detectMultiScaleWithParams(Mat img, double hitThresh, Size winStride,
96                 Size padding, double scale, double finalThreshold, bool useMeanshiftGrouping){
97         return HOGDescriptor_DetectMultiScaleWithParams(&this, img, hitThresh, 
98                             winStride, padding, scale, finalThreshold, useMeanshiftGrouping);
99     }
100     
101     void setSVMDetector(Mat det){
102         HOGDescriptor_SetSVMDetector(&this, det);
103     }
104 }
105 
106 alias HOGDescriptor = _HOGDescriptor*;
107 
108 HOGDescriptor newHOGDescriptor(){
109     return HOGDescriptor_New();
110 }
111 
112 Mat getDefaultPeopleDetectorHOG(){
113     return HOG_GetDefaultPeopleDetector();
114 }
115 
116 Rects groupRectangles(Rects rects, int groupThreshold, double eps){
117     return GroupRectangles(rects, groupThreshold, eps);
118 }