/// <summary> /// Detect objects using Yolo model /// </summary> /// <param name="image">The input image</param> /// <param name="confThreshold">The confident threshold. Only detection with confident larger than this will be returned.</param> /// <param name="nmsThreshold">If positive, will perform non-maximum suppression using the threshold value. If less than or equals to 0, will not perform Non-maximum suppression.</param> /// <returns>The detected objects</returns> public DetectedObject[] Detect(Mat image, double confThreshold = 0.5, double nmsThreshold = 0.5) { if (_yoloDetectionModel == null) { throw new Exception("Please initialize the model first"); } using (VectorOfInt classIds = new VectorOfInt()) using (VectorOfFloat confidents = new VectorOfFloat()) using (VectorOfRect regions = new VectorOfRect()) { _yoloDetectionModel.Detect(image, classIds, confidents, regions, (float)confThreshold, (float)nmsThreshold); var classIdArr = classIds.ToArray(); var confidentArr = confidents.ToArray(); var regionArr = regions.ToArray(); List <DetectedObject> nmsResults = new List <DetectedObject>(); for (int i = 0; i < classIdArr.Length; i++) { DetectedObject o = new DetectedObject(); o.ClassId = classIdArr[i]; o.Confident = confidentArr[i]; o.Region = regionArr[i]; o.Label = _labels[o.ClassId]; nmsResults.Add(o); } return(nmsResults.ToArray()); } }
/// <summary> /// Detect objects using Yolo model /// </summary> /// <param name="image">The input image</param> /// <param name="confThreshold">The confident threshold. Only detection with confident larger than this will be returned.</param> /// <param name="nmsThreshold">If positive, will perform non-maximum suppression using the threshold value. If less than or equals to 0, will not perform Non-maximum suppression.</param> /// <returns>The detected objects</returns> public DetectedObject[] Detect(IInputArray image, double confThreshold = 0.5, double nmsThreshold = 0.5) { if (_yoloDetectionModel == null) { throw new Exception("Please initialize the model first"); } return(_yoloDetectionModel.Detect(image, (float)confThreshold, (float)nmsThreshold, _labels)); }
/// <summary> /// Detect faces on the image /// </summary> /// <param name="image">The image.</param> /// <param name="fullFaceRegions">The faces where a full facial region is detected. These images can be send to facial landmark recognition for further processing.</param> /// <param name="partialFaceRegions">The face region of which is close to the edge of the images. Because if may not contains all the facial landmarks, it is not recommended to send these regions to facial landmark detection.</param> /// <param name="confidenceThreshold">The confident threshold for face detection</param> /// <param name="nmsThreshold">The non maximum suppression threshold for face detection.</param> public void Detect(IInputArray image, List <DetectedObject> fullFaceRegions, List <DetectedObject> partialFaceRegions, float confidenceThreshold = 0.5f, float nmsThreshold = 0.0f) { using (InputArray iaImage = image.GetInputArray()) { DetectedObject[] detectedFaces = _faceDetectionModel.Detect(image, confidenceThreshold, nmsThreshold); Rectangle imageRegion = new Rectangle(Point.Empty, iaImage.GetSize()); foreach (DetectedObject face in detectedFaces) { if (imageRegion.Contains(face.Region)) { fullFaceRegions.Add(face); } else { partialFaceRegions.Add(face); } } } }
/// <summary> /// Detect vehicle from the given image /// </summary> /// <param name="image">The image</param> /// <returns>The detected vehicles.</returns> public Vehicle[] Detect(IInputArray image) { float vehicleConfidenceThreshold = 0.5f; float licensePlateConfidenceThreshold = 0.5f; double scale = 1.0; MCvScalar meanVal = new MCvScalar(); List <Vehicle> vehicles = new List <Vehicle>(); List <LicensePlate> plates = new List <LicensePlate>(); using (InputArray iaImage = image.GetInputArray()) using (Mat iaImageMat = iaImage.GetMat()) foreach (DetectedObject vehicleOrPlate in _vehicleLicensePlateDetectionModel.Detect(image, 0.0f, 0.0f)) { Rectangle region = vehicleOrPlate.Region; if (vehicleOrPlate.ClassId == 1 && vehicleOrPlate.Confident > vehicleConfidenceThreshold) { //this is a vehicle Vehicle v = new Vehicle(); v.Region = region; #region find out the type and color of the vehicle using (Mat vehicle = new Mat(iaImageMat, region)) using (VectorOfMat vm = new VectorOfMat(2)) { _vehicleAttrRecognizerModel.Predict(vehicle, vm); //_vehicleAttrRecognizer.Forward(vm, new string[] { "color", "type" }); using (Mat vehicleColorMat = vm[0]) using (Mat vehicleTypeMat = vm[1]) { float[] vehicleColorData = vehicleColorMat.GetData(false) as float[]; float maxProbColor = vehicleColorData.Max(); int maxIdxColor = Array.IndexOf(vehicleColorData, maxProbColor); v.Color = _colorName[maxIdxColor]; float[] vehicleTypeData = vehicleTypeMat.GetData(false) as float[]; float maxProbType = vehicleTypeData.Max(); int maxIdxType = Array.IndexOf(vehicleTypeData, maxProbType); v.Type = _vehicleType[maxIdxType]; } } #endregion vehicles.Add(v); } else if (vehicleOrPlate.ClassId == 2 && vehicleOrPlate.Confident > licensePlateConfidenceThreshold) { //this is a license plate LicensePlate p = new LicensePlate(); p.Region = region; #region OCR on license plate using (Mat plate = new Mat(iaImageMat, region)) { using (Mat inputBlob = DnnInvoke.BlobFromImage( plate, scale, new Size(94, 24), meanVal, false, false, DepthType.Cv32F)) { _ocr.SetInput(inputBlob, "data"); using (Mat output = _ocr.Forward("decode")) { float[] plateValue = output.GetData(false) as float[]; StringBuilder licensePlateStringBuilder = new StringBuilder(); foreach (int j in plateValue) { if (j >= 0) { licensePlateStringBuilder.Append(_plateText[j]); } } p.Text = licensePlateStringBuilder.ToString(); } } } #endregion plates.Add(p); } } foreach (LicensePlate p in plates) { foreach (Vehicle v in vehicles) { if (v.ContainsPlate(p)) { v.LicensePlate = p; break; } } } return(vehicles.ToArray()); }