Object Detection API
We call it AIM and this stands for Artificial Intelligence Machine.
The Object Detection API provides fast and accurate image object recognition using advanced neural networks developed by machine learning experts.
https://api-ninjas.com/api/objectdetection https://github.com/maxkleiner/HttpComponent
After you got you API-Key (API Key associated with your account) we need a httpcomponent of classes which are enable to post multipart-formdata-feed or streams. An HTTP multipart request is an HTTP request that HTTP clients construct to send files and data over to an HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server.
The content type “multipart/form-data” should be used for submitting forms that contain files, non-ASCII data, and binary data combined in a single body.
Behind the is the complicated configuration of a multipartformdata mechanism. On the other hand, multipart/form-data
is the encoding used when an HTML form has a file upload field. When you make a POST or Put request, you have to encode the data that forms the body of the request in some way.
So a multipart request is a request containing several packed requests inside its entity or part and we can also script that (maXbox5):
const URL_APILAY_DETECT = 'https://api.api-ninjas.com/v1/objectdetection/'; function TestHTTPClassComponentAPIDetection2(AURL, askstream, aApikey: string): string; var HttpReq1: THttpRequestC; Body: TMultipartFormBody; Body2: TUrlEncodedFormBody; begin Body:= TMultipartFormBody.Create; Body.ReleaseAfterSend:= True; //Body.Add('code','2','application/octet-stream'); Body.AddFromFile('image', exepath+'randimage01.jpg'); HttpReq1:= THttpRequestC.create(self); HttpReq1.headers.add('X-Api-Key:'+AAPIKEY); HttpReq1.headers.add('Accept:application/json'); try if HttpReq1.Post1Multipart(AURL, body) then writeln(HttpReq1.Response.ContentAsString) else Writeln('APIError '+inttostr(HttpReq1.Response.StatusCode2)); finally writeln('Status3: '+gethttpcod(HttpReq1.Response.statuscode2)) HttpReq1.Free; sleep(200) // if assigned(body) then body.free; end; end; print(TestHTTPClassComponentAPIDetection2(URL_APILAY_DETECT,' askstream',N_APIKEY));
const URL_APILAY_DETECT = 'https://api.api-ninjas.com/v1/objectdetection/';
function TestHTTPClassComponentAPIDetection2(AURL, askstream, aApikey:string):string;
var HttpReq1: THttpRequestC;
Body: TMultipartFormBody;
Body2: TUrlEncodedFormBody;
begin
Body:= TMultipartFormBody.Create;
Body.ReleaseAfterSend:= True;
//Body.Add('code','2','application/octet-stream');
Body.AddFromFile('image', exepath+'randimage01.jpg');
HttpReq1:= THttpRequestC.create(self);
HttpReq1.headers.add('X-Api-Key:'+AAPIKEY);
HttpReq1.headers.add('Accept:application/json');
try
if HttpReq1.Post1Multipart(AURL, body) then
writeln(HttpReq1.Response.ContentAsString)
else Writeln('APIError '+inttostr(HttpReq1.Response.StatusCode2));
finally
writeln('Status3: '+gethttpcod(HttpReq1.Response.statuscode2))
HttpReq1.Free;
sleep(200)
// if assigned(body) then body.free;
end;
end;
print(TestHTTPClassComponentAPIDetection2(URL_APILAY_DETECT,
' askstream',N_APIKEY));
The result returns a return a list of detected objects labels, confidence percentages and bounding boxes. Objects with confidence less than 0.3 (30%) are filtered out:
🙂 True
[{“label”: “ person”, “confidence”: “0.88”, “bounding_box”: {“x1”: “329”, “y1”: “278”, “x2”: “423”, “y2”: “420”}}, {“label”: “ surfboard”, “confidence”: “0.46”, “bounding_box”: {“x1”: “316”, “y1”: “407”, “x2”: “402”, “y2”: “424”}}, {“label”: “ skis”, “confidence”: “0.42”, “bounding_box”: {“x1”: “316”, “y1”: “407”, “x2”: “402”, “y2”: “424”}}]
Status3: SC_OK
Discussion of Topic: Yeah the machine learnings got the person in the image with high confidence and the surfboard is more likely than the skis, which are out of probability context; I mean do you see any sea or snow?!
mX5🐞 executed: 27/04/2024 10:21:59 Runtime: 0:0:6.160 Memload: 74% use
RemObjects Pascal Script. Copyright © 2004–2024 by RemObjects Software & maXbox5
The same goes of course in Python as an embedding Procedure:
Procedure PyCodeObjectDetect(imgpath, aAPIKey: string);
begin
with TPythonEngine.Create(Nil) do begin
//pythonhome:= 'C:\Users\User\AppData\Local\Programs\Python\Python312\';
try
loadDLL;
ExecString('import requests');
ExecStr('url= "https://api.api-ninjas.com/v1/objectdetection"');
ExecStr('image_file_descriptor = open("'+imgpath+'", "rb")');
ExecStr('headers= {"X-Api-Key": "'+aAPIKey+'"}');
ExecStr('files = {"image": image_file_descriptor} ');
ExecStr('r=requests.post(url, headers=headers, files=files)');
println(EvalStr('r.json()'));
except
raiseError;
finally
free;
end;
end;
end;
Originally published at http://softwareschule.code.blog on April 27, 2024.