Skip to content

Commit

Permalink
refine code.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGong2013 committed Jan 27, 2018
1 parent 0d75e6e commit e453718
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
26 changes: 16 additions & 10 deletions Source/Preprocessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public extension Preprocessor where T: CIImage {
///
/// - parameter result: resize分割后单个字的size
/// - parameter adjustment: 是否对调整分割后的图片
/// - parameter debugger: 返回每一步的处理结果
/// - parameter debugger: 返回每一步的处理结果
///
/// - returns: 返回分割结果
///
Expand All @@ -208,12 +208,17 @@ public extension Preprocessor where T: CIImage {
let rect = c.boundingBox.scaled(to: ciImage.extent.size)

var image = ciImage.cropped(to: rect)
.transformed(by: CGAffineTransform(translationX: -rect.origin.x, y: -rect.origin.y))
.transformed(by: CGAffineTransform(translationX: -rect.origin.x, y: -rect.origin.y))
if let size = resize {
// 将文字切割出来 缩放到`size`
image = image.resize(size)
}

// if adjustment {
// image = SmoothThresholdFilter(image, inputEdgeO: 0.15, inputEdge1: 0.9).outputImage ?? image
// debugger?(image)
// image = AdaptiveThresholdFilter(image).outputImage ?? image
// debugger?(image)
// }
debugger?(image)
results.append(Value(image, rect))
}
Expand All @@ -226,7 +231,7 @@ public extension Preprocessor where T: CIImage {
///
///
public func croppedMaxRectangle() -> CorpMaxRectangleResult {

let request = VNDetectRectanglesRequest()
let handler = VNImageRequestHandler(ciImage: image, options: [:])

Expand All @@ -247,12 +252,12 @@ public extension Preprocessor where T: CIImage {
}

return image.preprocessor.perspectiveCorrection(boundingBox: maxObservation.boundingBox,
topLeft: maxObservation.topLeft,
topRight: maxObservation.topRight,
bottomLeft: maxObservation.bottomLeft,
bottomRight: maxObservation.bottomRight)
topLeft: maxObservation.topLeft,
topRight: maxObservation.topRight,
bottomLeft: maxObservation.bottomLeft,
bottomRight: maxObservation.bottomRight)
}

/// 根据脸部信息矫正图片,确认脸部正面向上👆
///
///
Expand All @@ -276,7 +281,7 @@ public extension Preprocessor where T: CIImage {
orientation = .left
}
}
}
}
} else if orientation == .up && faceFeatures.count == 0 {
orientation = .down
faceFeatures = detector.features(in: image, options: [CIDetectorImageOrientation: orientation.rawValue])
Expand Down Expand Up @@ -326,3 +331,4 @@ public extension Preprocessor where T: CIImage {
return .failure(.notFound)
}
}

16 changes: 11 additions & 5 deletions Source/Recognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,20 @@ public extension Valueable where T == Value {
guard let image = value?.image else {
return .failure(.notFound)
}
return .success(image.cropChineseIDCardNumberArea())
}
}

public extension CIImage {
public func cropChineseIDCardNumberArea() -> Value {
// 截取 数字区
// 按照真实比例截取,身份证号码区
let x = image.extent.width * 0.33
let y = image.extent.height * 0
let w = image.extent.width * 0.63
let h = image.extent.height * 0.25
let x = extent.width * 0.33
let y = extent.height * 0
let w = extent.width * 0.63
let h = extent.height * 0.25
let rect = CGRect(x: x, y: y, width: w, height: h)
return .success(Value(image.cropped(to: rect).transformed(by: CGAffineTransform(translationX: -x, y: -y)), rect))
return Value(cropped(to: rect).transformed(by: CGAffineTransform(translationX: -x, y: -y)), rect)
}
}

0 comments on commit e453718

Please sign in to comment.