Skip to content
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

Swift-使用opencv #58

Open
djk3000 opened this issue Dec 28, 2021 · 0 comments
Open

Swift-使用opencv #58

djk3000 opened this issue Dec 28, 2021 · 0 comments

Comments

@djk3000
Copy link
Owner

djk3000 commented Dec 28, 2021

写一个最简单demo看看能不能集成OpenCV,官网先下载
然后根据这个一步一步配置:https://blog.csdn.net/verybigbug/article/details/113588991
配置完成后就可以使用了(我这里使用了图片的转换,摄像头的读取,及人脸识别):
参考:https://github.com/opencv/opencv/tree/4.x/samples/swift/ios
这里swift可以直接调用mat函数转换图片。
var camera: CvVideoCamera2? 摄像头的转换的话就可以通过CvVideoCamera2来直接start,然后通过截取每一个画面:processImage这个回调可以拿到图片,然后就可以通过转换图片来对比xml中的数据进行人脸的识别,最后可以画框。(这个事参考官方demo的)

import UIKit
import opencv2

class ViewController: UIViewController, CvVideoCameraDelegate2 {
    var rgba: Mat? = nil
    var gray: Mat = Mat()
    var relativeFaceSize: Float = 0.2
    var absoluteFaceSize: Int32 = 0
    let FACE_RECT_COLOR = Scalar(0.0, 255.0, 0.0, 255.0)
    let FACE_RECT_THICKNESS: Int32 = 4
    let swiftDetector = CascadeClassifier(filename: Bundle(for: ViewController.self).path(forResource:"lbpcascade_frontalface", ofType:"xml")!)
    
    //摄像头画面的回调
    func processImage(_ image: Mat!) {
        let orientation = UIDevice.current.orientation
        switch orientation {
        case .landscapeLeft:
            rgba = Mat()
            Core.rotate(src: image, dst: rgba!, rotateCode: .ROTATE_90_COUNTERCLOCKWISE)
        case .landscapeRight:
            rgba = Mat()
            Core.rotate(src: image, dst: rgba!, rotateCode: .ROTATE_90_CLOCKWISE)
        default:
            rgba = image
        }
        //转灰色
        Imgproc.cvtColor(src: rgba!, dst: gray, code: .COLOR_RGB2GRAY)
        
        var faces = [Rect]()
        //人脸对比
        swiftDetector.detectMultiScale(image: gray, objects: &faces, scaleFactor: 1.1, minNeighbors: Int32(2), flags: Int32( 2), minSize: Size(width: absoluteFaceSize, height: absoluteFaceSize), maxSize: Size())
        //let facesArray = NSMutableArray()
        //nativeDetector!.detect(gray, faces: facesArray)
        //faces.append(contentsOf: facesArray)
        for face in faces {
            if orientation == .landscapeLeft {
                face.rotateClockwise(parentHeight: gray.rows())
            } else if orientation == .landscapeRight {
                face.rotateCounterclockwise(parentWidth: gray.cols())
            }
            //画框
            Imgproc.rectangle(img: image, pt1: face.tl(), pt2: face.br(), color: FACE_RECT_COLOR, thickness: FACE_RECT_THICKNESS)
        }
        
        DispatchQueue.main.sync {
            newImage.image = gray.toUIImage()
        }
    }
    
    @IBOutlet var oldImage: UIImageView!
    @IBOutlet var newImage: UIImageView!
    @IBOutlet var cameraHolder: UIView!
    @IBOutlet var changeImage: UIImageView!
    
    var camera: CvVideoCamera2? = nil
    
    //图片转换
    @IBAction func TestChange(_ sender: Any) {
        let src = Mat(uiImage:oldImage.image!)
        let gray = Mat()
        
        Imgproc.cvtColor(src: src, dst: gray, code: .COLOR_BGR2HLS)
        changeImage.image = gray.toUIImage()
        
        //        let video = VideoCapture.init(index: 0)
        //        while true {
        //            let frame = Mat()
        //            video.read(image: frame)
        //            newImage.image = frame.toUIImage()
        //
        //
        //        }
        
    }
    
    //摄像头CvVideoCamera2的开启
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        camera = CvVideoCamera2(parentView: cameraHolder)
        camera?.defaultAVCaptureDevicePosition = AVCaptureDevice.Position.front
        camera?.rotateVideo = true
        camera?.delegate = self
        camera?.start()
    }
}

extension Rect {
    func rotateClockwise(parentHeight:Int32) {
        let tmpX = self.x
        self.x = parentHeight - (self.y + self.height)
        self.y = tmpX
        swapDims()
    }
    
    func rotateCounterclockwise(parentWidth:Int32) {
        let tmpY = self.y
        self.y = parentWidth - (self.x + self.width)
        self.x = tmpY
        swapDims()
    }
    
    func swapDims() {
        let tmpWidth = self.width
        self.width = self.height
        self.height = tmpWidth
    }
}
@djk3000 djk3000 changed the title swift-使用opencv Swift-使用opencv Jan 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant