Suck the cat with code! This paper is participating in[Cat Essay Campaign]

What if you meet a cat you can’t name? Just pull out your phone

In order to facilitate the vast number of shovel excrement officer to identify the type and name of the cat at any time, only need to simple 3 steps, can avoid recognition or mistake embarrassing scene: 1, take out the mobile phone; 2, aim at the cat (please be careful not to scare the cat with the flash) 3, take a picture, the cat’s type will be displayed on the screen

Finished the operation, talk about technology, using the baidu cloud provided the interface of the animal identification API. Select photo/album to easily identify the type. The first is the interface layout, simply write a title, image preview, and take photo and album two buttons interface is roughly like this, a clear layout, easy to operate (you write without difficulty)Copy the code

In fact, there is a small technical problem, Baidu did not provide ios SDK and sample code, but the official animal recognition interface. The first thing to do is get authorization for accessToken and cache it for further operations. This is the official documentationCopy the code

No example code for ios? It doesn’t matter. When you create a project in Baidu Cloud, you get the required parameters and get accessToken smoothly

The next step is the most important and critical: upload the image for verification, get the returned data and display it, and proceed with the documentation:

I still don’t have an ios example, I didn’t get it the first time, but this time I’m pretty good at it, so just keep following the process. The image is first obtained, base64 encoded, and then urlencode encoded, and then something unexpected happens:

Prompt image format error, no abnormality found after check. Then change the URL upload method, return all normal. But because I don’t have my own server, so using the URL upload method is not possible (if you want to build your own server, that will have to be seen in another article, but I am too lazy, probably may or may not have that article). After repeated testing with Postman, the same error was still prompted. When I was about to give up, I used the base64 transcoding file to upload on a whim, and unexpectedly saw the expected return result:

Baidu unexpectedly left such an obvious misleading operation, according to the tutorial after urlencode unexpectedly abnormal, up to here has been 99% successful, although a lot of time, or get the desired results, the rest is the display of the interface. Although it is not a very complicated project, the result is wrapped in a model to call. Show the result of the call with a few screenshots.

#import <Foundation/Foundation.h>
@class CatResultModel;

NS_ASSUME_NONNULL_BEGIN

@interface CatModel : NSObject

@property (nonatomic, copy) NSString *log_id;

@property (nonatomic, strong) NSArray <CatResultModel *> *result;

@end

@interface CatResultModel : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *score;

@end
Copy the code

Then refresh the UI and display the results on the interface // display the scan results

CatResultModel *resultModel = [CatResultModel yy_modelWithJSON:self.catModel.result.firstObject];

        [self refreshUI:resultModel imageData:imageData];

    } failure:^(NSError * _Nonnull error) {
    
    }];
    
    [picker dismissViewControllerAnimated:YES completion:^{
    
    }];
}

- (void)refreshUI:(CatResultModel *)resultModel imageData:(NSData *)imageData  {

    [self.nameLab setText:resultModel.name];

    [self.imagePreview setImage:[UIImage imageWithData:imageData]];

}
Copy the code

Here are some screenshots to demonstrate:

Finally, to sum up, the interface function provided by Baidu Cloud is a little bit simpler, returning only the name and a string of numbers representing approximation. However, it can identify the results very quickly, with relatively high accuracy (the orange cat uploaded in the middle was identified as sichuan Jianzhou cat).

Focus on the big orange