You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

RootViewController.mm 6.7 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // RootViewController.m
  3. // lpr
  4. //
  5. // Created by baotim on 2018/10/26.
  6. // Copyright © 2018 lprSample. All rights reserved.
  7. //
  8. #import "RootViewController.h"
  9. #import <Masonry/Masonry.h>
  10. #import "Utility.h"
  11. #import "Pipeline.h"
  12. #import "CameraViewController.h"
  13. @interface RootViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  14. @property (nonatomic, strong) UIButton* albumButton;
  15. @property (nonatomic, strong) UIButton* cameraButton;
  16. @property (nonatomic, strong) UIImageView* imageView;
  17. @property (nonatomic, strong) UILabel* resultLabel;
  18. @end
  19. @implementation RootViewController
  20. {
  21. cv::Mat source_image;
  22. }
  23. #pragma mark - Lazy Initialize
  24. - (UIButton *)albumButton
  25. {
  26. if (!_albumButton) {
  27. _albumButton = [UIButton buttonWithType:UIButtonTypeCustom];
  28. [_albumButton setTitle:@"打开相册" forState:UIControlStateNormal];
  29. [_albumButton setBackgroundColor:[UIColor redColor]];
  30. [_albumButton addTarget:self action:@selector(openAlbum) forControlEvents:UIControlEventTouchUpInside];
  31. }
  32. return _albumButton;
  33. }
  34. - (UIButton *)cameraButton
  35. {
  36. if (!_cameraButton) {
  37. _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
  38. [_cameraButton setBackgroundColor:[UIColor redColor]];
  39. [_cameraButton setTitle:@"实时拍照" forState:UIControlStateNormal];
  40. [_cameraButton addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
  41. }
  42. return _cameraButton;
  43. }
  44. - (UIImageView *)imageView
  45. {
  46. if (!_imageView) {
  47. _imageView = [[UIImageView alloc] init];
  48. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  49. }
  50. return _imageView;
  51. }
  52. - (UILabel *)resultLabel
  53. {
  54. if (!_resultLabel) {
  55. _resultLabel = [UILabel new];
  56. _resultLabel.textColor = [UIColor redColor];
  57. _resultLabel.textAlignment = NSTextAlignmentLeft;
  58. _resultLabel.font = [UIFont systemFontOfSize:15];
  59. }
  60. return _resultLabel;
  61. }
  62. #pragma mark - Life Cycle
  63. - (void)viewDidLoad {
  64. [super viewDidLoad];
  65. // Do any additional setup after loading the view.
  66. self.view.backgroundColor = [UIColor whiteColor];
  67. self.navigationItem.title = @"车牌识别Demo";
  68. [self.view addSubview:self.albumButton];
  69. [self.albumButton mas_makeConstraints:^(MASConstraintMaker* make) {
  70. make.centerX.equalTo(self.view).offset(-80);
  71. make.bottom.equalTo(self.mas_bottomLayoutGuideTop).offset(-20);
  72. make.width.mas_equalTo(100);
  73. make.height.mas_equalTo(50);
  74. }];
  75. [self.view addSubview:self.cameraButton];
  76. [self.cameraButton mas_makeConstraints:^(MASConstraintMaker* make) {
  77. make.centerX.equalTo(self.view).offset(80);
  78. make.bottom.equalTo(self.albumButton);
  79. make.width.mas_equalTo(100);
  80. make.height.mas_equalTo(50);
  81. }];
  82. [self.view addSubview:self.resultLabel];
  83. [self.resultLabel mas_makeConstraints:^(MASConstraintMaker* make) {
  84. make.left.right.equalTo(self.view);
  85. make.bottom.lessThanOrEqualTo(self.albumButton.mas_top);
  86. }];
  87. [self.view addSubview:self.imageView];
  88. [self.imageView mas_makeConstraints:^(MASConstraintMaker* make) {
  89. make.top.left.right.equalTo(self.view);
  90. make.bottom.lessThanOrEqualTo(self.resultLabel.mas_top);
  91. }];
  92. }
  93. #pragma mark - Actions
  94. - (void)openAlbum
  95. {
  96. UIImagePickerController* picker = [[UIImagePickerController alloc] init];
  97. picker.delegate = self;
  98. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
  99. return;
  100. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  101. [self presentViewController:picker animated:YES completion:nil];
  102. }
  103. - (void)openCamera
  104. {
  105. CameraViewController* video = [CameraViewController new];
  106. video.resultCB = ^(NSString* text, UIImage* image) {
  107. self.imageView.image = image;
  108. self.resultLabel.text = text;
  109. };
  110. [self presentViewController:video animated:YES completion:nil];
  111. }
  112. #pragma mark - Private Methods
  113. - (NSString *)getPath:(NSString*)fileName
  114. {
  115. NSString *bundlePath = [NSBundle mainBundle].bundlePath;
  116. NSString *path = [bundlePath stringByAppendingPathComponent:fileName];
  117. return path;
  118. }
  119. - (void)simpleRecognition:(cv::Mat&)src
  120. {
  121. NSString *path_1 = [self getPath:@"cascade.xml"];
  122. NSString *path_2 = [self getPath:@"HorizonalFinemapping.prototxt"];
  123. NSString *path_3 = [self getPath:@"HorizonalFinemapping.caffemodel"];
  124. NSString *path_4 = [self getPath:@"Segmentation.prototxt"];
  125. NSString *path_5 = [self getPath:@"Segmentation.caffemodel"];
  126. NSString *path_6 = [self getPath:@"CharacterRecognization.prototxt"];
  127. NSString *path_7 = [self getPath:@"CharacterRecognization.caffemodel"];
  128. std::string *cpath_1 = new std::string([path_1 UTF8String]);
  129. std::string *cpath_2 = new std::string([path_2 UTF8String]);
  130. std::string *cpath_3 = new std::string([path_3 UTF8String]);
  131. std::string *cpath_4 = new std::string([path_4 UTF8String]);
  132. std::string *cpath_5 = new std::string([path_5 UTF8String]);
  133. std::string *cpath_6 = new std::string([path_6 UTF8String]);
  134. std::string *cpath_7 = new std::string([path_7 UTF8String]);
  135. pr::PipelinePR pr2 = pr::PipelinePR(*cpath_1, *cpath_2, *cpath_3, *cpath_4, *cpath_5, *cpath_6, *cpath_7);
  136. std::vector<pr::PlateInfo> list_res = pr2.RunPiplineAsImage(src);
  137. std::string concat_results = "";
  138. for(auto one:list_res) {
  139. if(one.confidence>0.7) {
  140. concat_results += one.getPlateName()+",";
  141. }
  142. }
  143. NSString *str = [NSString stringWithCString:concat_results.c_str() encoding:NSUTF8StringEncoding];
  144. if (str.length > 0) {
  145. str = [str substringToIndex:str.length-1];
  146. str = [NSString stringWithFormat:@"识别结果: %@",str];
  147. } else {
  148. str = [NSString stringWithFormat:@"识别结果: 未识别成功"];
  149. }
  150. [self.resultLabel setText:str];
  151. }
  152. #pragma mark - UIImagePickerControllerDelegate
  153. - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  154. {
  155. [picker dismissViewControllerAnimated:YES completion:nil];
  156. UIImage* temp = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  157. UIImage *temp_image = [Utility scaleAndRotateImageBackCamera:temp];
  158. source_image = [Utility cvMatFromUIImage:temp_image];
  159. [self simpleRecognition:source_image];
  160. self.imageView.image = temp;
  161. }
  162. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  163. {
  164. [picker dismissViewControllerAnimated:YES completion:nil];
  165. }
  166. @end