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.

ViewController.mm 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // ViewController.m
  3. // lp
  4. //
  5. // Created by xiaojun on 2017/12/2.
  6. // Copyright © 2017年 xiaojun. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "UIImageCVMatConverter.h"
  10. #import "Pipeline.h"
  11. using namespace pr;
  12. @interface ViewController ()
  13. @end
  14. @implementation ViewController
  15. @synthesize imageView;
  16. @synthesize textView;
  17. @synthesize toolbar;
  18. @synthesize textLabel;
  19. -(NSString *)getPath:(NSString*)fileName{
  20. NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"model.bundle"];
  21. NSString *path = [bundlePath stringByAppendingPathComponent:fileName];
  22. return path;
  23. }
  24. -(void)simpleRecognition:(cv::Mat&)src{
  25. NSString *path_1 = [self getPath:@"cascade.xml"];
  26. NSString *path_7 = [self getPath:@"CharacterRecognization.caffemodel"];
  27. NSString *path_6 = [self getPath:@"CharacterRecognization.prototxt"];
  28. NSString *path_3 = [self getPath:@"HorizonalFinemapping.caffemodel"];
  29. NSString *path_2 = [self getPath:@"HorizonalFinemapping.prototxt"];
  30. NSString *path_5 = [self getPath:@"Segmentation.caffemodel"];
  31. NSString *path_4 = [self getPath:@"Segmentation.prototxt"];
  32. std::string *cpath_1 = new std::string([path_1 UTF8String]);
  33. std::string *cpath_2 = new std::string([path_2 UTF8String]);
  34. std::string *cpath_3 = new std::string([path_3 UTF8String]);
  35. std::string *cpath_4 = new std::string([path_4 UTF8String]);
  36. std::string *cpath_5 = new std::string([path_5 UTF8String]);
  37. std::string *cpath_6 = new std::string([path_6 UTF8String]);
  38. std::string *cpath_7 = new std::string([path_7 UTF8String]);
  39. PipelinePR pr2 = PipelinePR::PipelinePR(*cpath_1,*cpath_2,*cpath_3,*cpath_4,*cpath_5,*cpath_6,*cpath_7);
  40. std::vector<pr::PlateInfo> list_res = pr2.RunPiplineAsImage(src);
  41. std::string concat_results="";
  42. for(auto one:list_res){
  43. if(one.confidence>0.7){
  44. concat_results += one.getPlateName()+",";
  45. //std::cout<<"1-----"+one.getPlateName()+"----1";
  46. }
  47. }
  48. //std::cout<<"2--"+concat_results+"--2";
  49. NSString *str=[NSString stringWithCString:concat_results.c_str() encoding:NSUTF8StringEncoding];
  50. if(str.length > 0){
  51. str = [str substringToIndex:str.length-1];
  52. str = [NSString stringWithFormat:@"result:%@",str];
  53. }else{
  54. str = [NSString stringWithFormat:@"result:null"];
  55. }
  56. [self.textLabel setText:str];
  57. }
  58. - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  59. {
  60. [picker dismissViewControllerAnimated:YES completion:nil];
  61. UIImage* temp = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  62. UIImage *temp_image=[UIImageCVMatConverter scaleAndRotateImageBackCamera:temp];
  63. source_image=[UIImageCVMatConverter cvMatFromUIImage:temp_image];
  64. [self simpleRecognition:source_image];
  65. imageView.image = temp;
  66. //cv::Mat cvImage,cv2;
  67. //UIImageToMat(temp, cvImage);
  68. // if(!cvImage.empty()){
  69. // cv::Mat gray;
  70. // // 将图像转换为灰度显示
  71. // cv::cvtColor(cvImage,gray,CV_RGB2GRAY);
  72. // // 应用高斯滤波器去除小的边缘
  73. // cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
  74. // // 计算与画布边缘
  75. // cv::Mat edges;
  76. // cv::Canny(gray, edges, 0, 50);
  77. // // 使用白色填充
  78. // cvImage.setTo(cv::Scalar::all(225));
  79. // // 修改边缘颜色
  80. // cvImage.setTo(cv::Scalar(0,128,255,255),edges);
  81. // // 将Mat转换为Xcode的UIImageView显示
  82. // self.imageView.image = MatToUIImage(cvImage);
  83. // }
  84. }
  85. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  86. {
  87. [picker dismissViewControllerAnimated:YES completion:nil];
  88. }
  89. - (void)loadButtonPressed:(id)sender
  90. {
  91. UIImagePickerController* picker = [[UIImagePickerController alloc] init];
  92. picker.delegate = self;
  93. if (![UIImagePickerController isSourceTypeAvailable:
  94. UIImagePickerControllerSourceTypePhotoLibrary])
  95. return;
  96. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  97. [self presentViewController:picker animated:YES completion:nil];
  98. }
  99. - (void)viewDidLoad {
  100. [super viewDidLoad];
  101. // Do any additional setup after loading the view, typically from a nib.
  102. CGRect bounds = [UIScreen mainScreen].bounds;
  103. imageView = [[UIImageView alloc] init];
  104. imageView.frame = CGRectMake(0, 160, bounds.size.width, bounds.size.height-210);
  105. imageView.contentMode=UIViewContentModeScaleAspectFit;
  106. imageView.backgroundColor = [UIColor clearColor];
  107. [self.view addSubview:imageView];
  108. /* Add the fps Label */
  109. UILabel *fps = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 180, 20)];
  110. fps.font=[UIFont fontWithName:@"华文细黑" size:14.0f];
  111. fps.backgroundColor=[UIColor clearColor];
  112. fps.textColor=[UIColor redColor];
  113. fps.textAlignment=NSTextAlignmentLeft;
  114. // fps.transform = CGAffineTransformMakeRotation(90);
  115. fps.text=@"result";
  116. self.textLabel = fps;
  117. [self.view addSubview:self.textLabel];
  118. [self.view bringSubviewToFront:self.textLabel];
  119. toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, bounds.size.height- 44, bounds.size.width, 44)];
  120. [toolbar setBackgroundColor:[UIColor clearColor]];
  121. // toolbar.barStyle=UIBarStyleDefault;
  122. toolbar.tintColor=[UIColor blackColor];
  123. toolbar.translucent=YES;
  124. // [toolbar setTranslucent:YES];
  125. [self.toolbar setBackgroundImage:[UIImage new]
  126. forToolbarPosition:UIBarPositionAny
  127. barMetrics:UIBarMetricsDefault];
  128. toolbar.delegate=self;
  129. UIBarButtonItem*flexitem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  130. UIBarButtonItem*albumitem=[[UIBarButtonItem alloc]
  131. initWithTitle:@"相册"
  132. style:UIBarButtonItemStylePlain
  133. target:self
  134. action:@selector(loadButtonPressed:)];
  135. [toolbar setItems:[NSArray arrayWithObjects:albumitem,flexitem,nil]];
  136. [self.view addSubview:toolbar];
  137. // Do any additional setup after loading the view, typically from a nib
  138. toolbar.autoresizingMask = UIViewAutoresizingNone;
  139. }
  140. - (void)didReceiveMemoryWarning {
  141. [super didReceiveMemoryWarning];
  142. // Dispose of any resources that can be recreated.
  143. }
  144. @end