File: face_detect.jl

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (59 lines) | stat: -rw-r--r-- 1,656 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using OpenCV

function detect(img::OpenCV.InputArray, cascade)
    rects = OpenCV.detectMultiScale(cascade, img, scaleFactor=1.3, minNeighbors=Int32(4), minSize=OpenCV.Size{Int32}(30, 30), flags=OpenCV.CASCADE_SCALE_IMAGE)
    processed_rects = []
    for rect in rects
        push!(processed_rects, (rect.x, rect.y, rect.width+rect.x, rect.height+rect.y))
    end
    return processed_rects
end

function draw_rects(img, rects, color)
    for x in rects
        OpenCV.rectangle(img, OpenCV.Point{Int32}(x[1], x[2]), OpenCV.Point{Int32}(x[3], x[4]), color, thickness = Int32(2))
    end
end

cap = OpenCV.VideoCapture(Int32(0))

# Replace the paths for the classifiers before running

cascade = OpenCV.CascadeClassifier("haarcascade_frontalface_alt.xml")
nested = OpenCV.CascadeClassifier("haarcascade_eye.xml")

OpenCV.namedWindow("facedetect")

while true
    ret, img = OpenCV.read(cap)
    if ret==false
        print("Webcam stopped")
        break
    end
    gray = OpenCV.cvtColor(img, OpenCV.COLOR_BGR2GRAY)
    gray = OpenCV.equalizeHist(gray)

    rects = detect(gray, cascade)
    vis = copy(img)
    draw_rects(vis, rects, (0.0, 255.0, 0.0))

    if ~OpenCV.empty(nested)
        for x in rects
            roi = view(gray, :, Int(x[1]):Int(x[3]), Int(x[2]):Int(x[4]))
            subrects = detect(roi, nested)
            draw_view = view(vis, :, Int(x[1]):Int(x[3]), Int(x[2]):Int(x[4]))
            draw_rects(draw_view, subrects, (255.0, 0.0, 0.0))
        end
    end

    OpenCV.imshow("facedetect", vis)
    if OpenCV.waitKey(Int32(5))==27
        break
    end
end

OpenCV.release(cap)

OpenCV.destroyAllWindows()

print("Stopped")