Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Last active August 8, 2020 18:23
Show Gist options
  • Save dhaniksahni/3ab394e4c290abe9f2c11608022626a3 to your computer and use it in GitHub Desktop.
Save dhaniksahni/3ab394e4c290abe9f2c11608022626a3 to your computer and use it in GitHub Desktop.
Google Cloud Object Recognition
public class ObjectRecognizer {
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> getObjectCases()
{
system.debug('Case:' + [select id from case where RecordType.Name='Logo Recognition']);
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<ObjectInformation> getObjectInformation(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<ObjectInformation> objects=getImageObject(attach.VersionData);
system.debug('objects:'+ objects);
return objects;
}
}
return null;
}
@AuraEnabled
public static List<ObjectInformation> getImageObject(Blob data)
{
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);
ObjectRecogRequest.request request=new ObjectRecogRequest.request();
ObjectRecogRequest.feature feature=new ObjectRecogRequest.feature();
feature.type='OBJECT_LOCALIZATION';
feature.maxResults=10;
ObjectRecogRequest.image img=new ObjectRecogRequest.image();
img.content =data;
request.image =img;
request.features =new List<ObjectRecogRequest.feature>();
request.features.add(feature);
List<ObjectRecogRequest.request> requests=new List<ObjectRecogRequest.request>();
requests.add(request);
ObjectRecogRequest objReq=new ObjectRecogRequest();
objReq.requests=requests;
req.setBody(JSON.serialize(objReq));
Http http = new Http();
HTTPResponse res = http.send(req);
if(res.getStatus() =='Unauthorized')
{
system.debug('Authenticate.....');
return null;
}
else
{
ObjectRecogResponse objects=ObjectRecogResponse.parse(res.getBody());
ObjectInformation info;
List<ObjectInformation> infos=new List<ObjectInformation>();
for(ObjectRecogResponse.Response obj:objects.responses)
{
if(obj!=null && !obj.localizedObjectAnnotations.isEmpty())
{
for(ObjectRecogResponse.LocalizedObjectAnnotation annot:obj.localizedObjectAnnotations)
{
info=new ObjectInformation();
info.ObjectName=annot.name;
info.Score=annot.score;
infos.add(info);
}
}
}
return infos;
}
}
public class ImageResponse
{
@auraenabled
public string PublicUrl;
@auraenabled
public string DownloadableUrl;
}
public class ObjectInformation
{
@auraenabled
public string ObjectName;
@auraenabled
public decimal Score;
}
}
public class ObjectRecogRequest {
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 ObjectRecogResponse {
public Response[] responses;
public Class Response {
public LocalizedObjectAnnotation[] localizedObjectAnnotations;
}
public class LocalizedObjectAnnotation {
public String mid;
public String name;
public Double score;
public BoundingPoly boundingPoly;
}
public class BoundingPoly {
public NormalizedVertice[] normalizedVertices;
}
public class NormalizedVertice {
public Double x;
public Double y;
}
public static ObjectRecogResponse parse(String json){
return (ObjectRecogResponse) System.JSON.deserialize(json, ObjectRecogResponse.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment