Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Last active July 30, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaniksahni/fa0c86b5530708a500c81cc8907de83a to your computer and use it in GitHub Desktop.
Save dhaniksahni/fa0c86b5530708a500c81cc8907de83a to your computer and use it in GitHub Desktop.
Logo Recognition Using Google in Salesforce
public class LogoRecognizer {
public static string getAccessToken()
{
GoogleAuthSetting__mdt mapping =
[SELECT AccessToken__c, Label FROM GoogleAuthSetting__mdt WHERE Label='Fitness' and DeveloperName='Fitness'];
return mapping.AccessToken__c;
}
@auraenabled(cacheable=true)
public static List<Case> getLogoCases()
{
return [select id from case where RecordType.Name='Logo Recognition'];
}
@auraenabled(cacheable=false)
public static ImageResponse getImage(string id)
{
if(string.isEmpty(id))
{
throw new BaseException('Bad Input');
}
List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:id];
Set<Id> ids=new Set<Id>();
for(ContentDocumentLink link:links)
{
ids.add(link.ContentDocumentId);
}
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
List<ContentDistribution> cdts=[select ContentDocumentId,DistributionPublicUrl,ContentDownloadURL from ContentDistribution where ContentDocumentId =:ids];
Map<String, ContentDistribution> contentList= new Map <String, ContentDistribution>();
for(ContentDistribution cdt:cdts)
{
contentList.put(cdt.ContentDocumentId, cdt);
}
for(ContentVersion attach:versions)
{
ContentDistribution image=contentList.get(attach.ContentDocumentId);
if(image!=null)
{
ImageResponse response=new ImageResponse();
response.PublicUrl=image.DistributionPublicUrl;
response.DownloadableUrl=image.ContentDownloadUrl;
return response;
}
}
return null;
}
@auraenabled(cacheable=true)
public static List<LogoInformation> getLogoInformation(string record)
{
if(string.isEmpty(record))
{
throw new BaseException('Bad Input');
}
List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:record];
Set<Id> ids=new Set<Id>();
for(ContentDocumentLink link:links)
{
ids.add(link.ContentDocumentId);
}
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
for(ContentVersion attach:versions)
{
if(attach!=null)
{
List<LogoInformation> logos=getLogo(attach.VersionData);
system.debug('list:'+ logos);
return logos;
}
}
return null;
}
@AuraEnabled
public static List<LogoInformation> getLogo(Blob imageBlob)
{
string strToken=getAccessToken();
if(string.isEmpty(strToken))
{
return null;
}
system.debug(strToken);
HttpRequest req = new HttpRequest();
req.setHeader('Authorization','Bearer '+ strToken);
req.setHeader('Content-Type','application/json; charset=utf-8');
req.setMethod('POST');
req.setEndpoint('https://vision.googleapis.com/v1/images:annotate');
req.setTimeout(120000);
LogoRequest.request request=new LogoRequest.request();
LogoRequest.feature feature=new LogoRequest.feature();
feature.type='LOGO_DETECTION';
feature.maxResults=30;
LogoRequest.image image=new LogoRequest.image();
image.content=imageBlob;
request.image =image;
request.features =new List<LogoRequest.feature>();
request.features.add(feature);
List<LogoRequest.request> requests=new List<LogoRequest.request>();
requests.add(request);
LogoRequest logoReq=new LogoRequest();
logoReq.requests=requests;
system.debug(JSON.serialize(logoReq));
req.setBody(JSON.serialize(logoReq));
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug('res.getStatus().....'+res.getStatus());
system.debug('Authenticate.....'+res.getBody());
if(res.getStatus() =='Unauthorized')
{
system.debug('Authenticate.....');
return null;
}
else
{
LogoResponse logos=LogoResponse.parse(res.getBody());
LogoInformation info;
List<LogoInformation> infos=new List<LogoInformation>();
for(LogoResponse.response logo:logos.responses)
{
if(logo!=null && !logo.logoAnnotations.isEmpty())
{
for(LogoResponse.logoAnnotation annot:logo.logoAnnotations)
{
info=new LogoInformation();
info.BrandName=annot.description;
info.Score=annot.score;
infos.add(info);
}
}
}
system.debug('logos:' + infos);
return infos;
}
}
public class ImageResponse
{
@auraenabled
public string PublicUrl;
@auraenabled
public string DownloadableUrl;
}
public class LogoInformation
{
@auraenabled
public string BrandName;
@auraenabled
public decimal Score;
}
}
public class LogoRequest {
public list<request> requests{get;set;}
public class request{
public image image{get;set;}
public list<feature> features{get;set;}
}
public class image{
public Blob content{get;set;}
}
public class feature{
public String type{get;set;}
public integer maxResults{get;set;}
}
}
public class LogoResponse {
@AuraEnabled
public list<response> responses{get;set;}
public class vertice{
@AuraEnabled
public Integer y{get;set;}
@AuraEnabled
public Integer x{get;set;}
}
public class response{
@AuraEnabled
public list<logoAnnotation> logoAnnotations{get;set;}
}
public class logoAnnotation{
@AuraEnabled
public boundingPoly boundingPoly{get;set;}
@AuraEnabled
public Decimal score{get;set;}
@AuraEnabled
public String description{get;set;}
@AuraEnabled
public String mid{get;set;}
}
public class boundingPoly{
@AuraEnabled
public list<vertice> vertices{get;set;}
}
public static LogoResponse parse(String json) {
return (LogoResponse) System.JSON.deserialize(json, LogoResponse.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment