React App Using TensorFlow Detects Wrong Object - reactjs

I am following a tutorial on Object Detection with Javascript.
Here's a code snippet that bothers me right now:
let classesDir = {
1: {
name: 'Kangaroo',
id: 1,
},
2: {
name: 'Other',
id: 2,
}
}
So far, the app should detect object Kangaroo or it'll detect other objects as Other. Interesting thing is whatever object I try to detect using device camera, it detects that as Kangaroo. Right now, it uses the following model:
const model = await loadGraphModel("https://raw.githubusercontent.com/hugozanini/TFJS-object-detection/master/models/kangaroo-detector/model.json");
When I tried instead to use the following model that's bit different:
const model = await loadGraphModel("https://storage.googleapis.com/tfjs-models/savedmodel/ssdlite_mobilenet_v2/model.json");
it throws an exception:
Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'arraySync')
Here's the code snippet where the exception starts in the index.js file:
//Getting predictions
const boxes = predictions[4].arraySync(); //Exception here
const scores = predictions[5].arraySync();
const classes = predictions[6].dataSync();
For the above scenario, I changed the object name to something else to detect but every time, it detects every object as the same one even that's different. Is there anything that requires to do with the model, I am not sure?

Related

Error running AutoML Edge model with tfjs-node

I am still very new to Tensorflow but when trying to run an AutoML Object Detection edge model with tfjs-node I run into this error.
Error: Session fail to run with error: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
[[{{node map/while/DecodeJpeg}}]]
I cannot figure out why Tensorflow is throwing this error when I have checked the string is a JPEG in Base64. To further elaborate this was the example I was following but had to deviate as I was getting errors about inputs needing to be image_bytes and key.
import { readFileSync } from 'fs';
import { node, scalar } from '#tensorflow/tfjs-node';
const model = await node.loadSavedModel("[model directory path]", ['serve'], 'serving_default');
const input = readFileSync("[image path]", "base64");
const output = model.predict({
image_bytes: scalar(input).expandDims(0),
key: scalar("").expandDims(0),
});
const scores = await output['detection_scores'].arraySync();
const boxes = await output['detection_boxes'].arraySync();
const names = await output['detection_classes'].arraySync();
output['detection_scores'].dispose();
output['detection_boxes'].dispose();
output['detection_classes'].dispose();
output['num_detections'].dispose();
console.log(scores);
console.log(boxes);
console.log(names);
EDIT: I've found the file in Tensorflow that throws this error but I am still unsure of how to fix it as I am not familiar with C++.

How do you define the interaction properly to modify it's permissions in discord.js

I am trying to (in discord.js) modify the perms of who can access a slash command to allow mods to access one that is (by default) locked.
The code below is what I'm using (in my index.js file) and it's been built using the guide. The only real change was removing the await's because I got an error:
await is only valid in async functions and the top level bodies of
modules
The error I keep getting now is:
TypeError: Cannot read properties of undefined (reading 'permissions')
if (!client.application?.owner) client.application?.fetch();
const aboutcommand = client.guilds.cache.get('aguildid')?.commands.fetch('acommandid');
const modperms = [
{
id: 'aroleid',
type: 'ROLE',
permission: false,
},
];
aboutcommand.permissions.add({ modperms });

Pdf Tron error " Exception error: Pdf error not found" (React App)

I'm trying to embed Pdf tron to my React application. I'm receiving this error when I'm clicking on the tab I want to filter to find the relative pdf file.
const handleFilteredDocs = (id)=>{
const filteredDoc = props.location.documents && props.location.documents.filter(doc=>{
return doc.controlId === id
})
setFileteredDoc(filteredDoc)
setPdfPath(filteredDoc[0].filePath)
WebViewer(
{
path: 'lib',
initialDoc: `lib/pdf/${pdfPath}`,
extension: "pdf"
},
viewer.current,
).then((instance) => {
const { docViewer, Annotations } = instance;
const annotManager = docViewer.getAnnotationManager();
docViewer.on('documentLoaded', () => {
const rectangleAnnot = new Annotations.RectangleAnnotation();
rectangleAnnot.PageNumber = 1;
// values are in page coordinates with (0, 0) in the top left
rectangleAnnot.X = 100;
rectangleAnnot.Y = 150;
rectangleAnnot.Width = 200;
rectangleAnnot.Height = 50;
rectangleAnnot.Author = annotManager.getCurrentUser();
annotManager.addAnnotation(rectangleAnnot);
// need to draw the annotation otherwise it won't show up until the page is refreshed
annotManager.redrawAnnotation(rectangleAnnot);
});
});
}
I'm thinking is because the ref component didn't receive in time the pdfPath state and then throw the error. I've tried to place a separate button to load the pdf with the pdfPath correctly updated and in that case worked. What can i do make it render correctly there?
this is the error I get from the console:
(index)
Value
UI version "7.3.0"
Core version "7.3.0"
Build "Mi8yMi8yMDIxfDZmZmNhOTdmMQ=="
WebViewer Server false
Full API false
Object
CoreControls.js:189 Could not use incremental download for url /lib/pdf/. Reason: The file is not linearized.
CoreControls.js:189
{message: "The file is not linearized."}
CoreControls.js:189 There may be some degradation of performance. Your server has not been configured to serve .gz. and .br. files with the expected Content-Encoding. See http://www.pdftron.com/kb_content_encoding for instructions on how to resolve this.
CoreControls.js:189 There may be some degradation of performance. Your server has not been configured to serve .gz. and .br. files with the expected Content-Encoding. See http://www.pdftron.com/kb_content_encoding for instructions on how to resolve this.
CoreControls.js:189 There may be some degradation of performance. Your server has not been configured to serve .gz. and .br. files with the expected Content-Encoding. See http://www.pdftron.com/kb_content_encoding for instructions on how to resolve this.
81150ece-4c18-41b0-b551-b92f332bd17f:1
81150ece-4c18-41b0-b551-b92f332bd17f:1 PDFNet is running in demo mode.
81150ece-4c18-41b0-b551-b92f332bd17f:1 Permission: read
CoreControls.js:922 Uncaught (in promise)
{message: "Exception: ↵ Message: PDF header not found. The f… Function : SkipHeader↵ Linenumber : 1139↵", type: "InvalidPDF"}
Thank you guys for any help I will get on this!
The value of "pdfPath" isn't set to "filteredDoc[0].filePath" yet after you call "setPdfPath" (it'll still be the initial state till the next render). One thing you can do is pass a callback function when using "setState" to call "WebViewer()" after "pdfPath" has been updated
https://reactjs.org/docs/react-component.html#setstate
Also there is a guide on how to add PDFtron to a React project in the following link
https://www.pdftron.com/documentation/web/get-started/react/
One thing to note, is it's does the following
useEffect(() => {
// will only run once
WebViewer()
}, [])
By doing the above, "WebViewer" is only initialized once. It might be a good idea to do something similar and use "loadDocument" (https://www.pdftron.com/documentation/web/guides/best-practices/#loading-documents-with-loaddocument) when switching between documents instead of reinitializing WebViewer each time the state changes

drive realtime api model toJson not populating fields in custom type

I'm building a Drive Realtime project using custom types: https://developers.google.com/google-apps/realtime/custom-objects.
I'm having an issue where the fields in my custom objects are not exported in the model.toJson() output. I'm sure I'm just missing something, but I haven't been able to find any differences with how I'm constructing the custom object vs. the realtime playground or the realtime API documentation.
Sample repro case using the realtime playground is below.
1) go to realtime playground: https://realtimeplayground.appspot.com/
2) open developer console
3) Run the following code
test = function () {}
test.prototype = { init: function() { this.name = 'testName';}};
test.prototype.name = gapi.drive.realtime.custom.collaborativeField('name');
gapi.drive.realtime.custom.registerType(test, 'testType')
gapi.drive.realtime.custom.setInitializer(test, test.prototype.init);
var model = window.doc.getModel()
model.getRoot().set('myTest', model.create(test));
model.toJson()
observed output:
"{"id":"root","type":"Map","value":
{"demo_string":
{"id":"Tq50c9iybcXi","type":"EditableString","value":"Edit Me!"},
"demo_list":{"id":"ZmjclOeUbcXj","type":"List","value":
[{"json":"Cat"},{"json":"Dog"},{"json":"Sheep"},{"json":"Chicken"}]},
"demo_cursors":{"id":"6TJ6Zzd2bcXj","type":"Map","value":{}},
"demo_map":{"id":"ukRRMPHbbcXj","type":"Map","value":
{"key1":{"json":"value 1"},"key2":{"json":"value 2"},"key3":{"json":"value 3"}}},
"demo_custom":{"id":"44nsuMAPbcXk","type":"DemoMovie","value":
{"name":{"json":"Minority Report"},
"director":{"json":"Steven Spielberg"},
"notes":{"json":""},"rating":{"json":""}}},
"myTest":{"id":"Kq4hcV4UbcvW","type":"testType","value":{}}}}"
Expected:
"{"id":"root","type":"Map","value":
{"demo_string":
{"id":"Tq50c9iybcXi","type":"EditableString","value":"Edit Me!"},
"demo_list":{"id":"ZmjclOeUbcXj","type":"List","value":
[{"json":"Cat"},{"json":"Dog"},{"json":"Sheep"},{"json":"Chicken"}]},
"demo_cursors":{"id":"6TJ6Zzd2bcXj","type":"Map","value":{}},
"demo_map":{"id":"ukRRMPHbbcXj","type":"Map","value":
{"key1":{"json":"value 1"},"key2":{"json":"value 2"},"key3":{"json":"value 3"}}},
"demo_custom":{"id":"44nsuMAPbcXk","type":"DemoMovie","value":
{"name":{"json":"Minority Report"},
"director":{"json":"Steven Spielberg"},
"notes":{"json":""},"rating":{"json":""}}},
"myTest":{"id":"Kq4hcV4UbcvW","type":"testType","value":{"json":"testName"}}}}}"
Registering custom types can only occur during the "Pre-Load" phase of the document life cycle. Your code is correct, but is being executed on the document after the document has loaded. This causes the custom object to not be properly constructed, which is why it is lacking the JSON value that you have specified in the init function.
To see the correct flow in action, put a break point on line 88 of static/elements/playground-app.js in the Realtime Playground application. Refresh the page and when execution has paused, run this code from the console:
test = function () {}
test.prototype = { init: function() { this.name = 'testName';}};
test.prototype.name = gapi.drive.realtime.custom.collaborativeField('name');
gapi.drive.realtime.custom.registerType(test, 'testType')
gapi.drive.realtime.custom.setInitializer(test, test.prototype.init);
Resume execution. When the application has fully loaded, run the rest of your code:
var model = window.doc.getModel()
model.getRoot().set('myTest', model.create(test));
model.toJson()
You will see that the outputted JSON matches what you are expecting:
"myTest":{"id":"1OiQd2QoEqBs","type":"testType","value":{"name":{"json":"testName"}}}
This workflow is documented under the "Registering custom types and fields" title of the Custom Collaborative Objects guide.

Unable to run a fetch operation in backboneJS

Here's a fetch operation I've written in a view:
this.collection.fetch ({
data:{
values: 100,
type: "normal"
},
success:(collection, response) => {
_.each(collection.models, (model) => {
Log.show(model.get("question"));
})
},
error: (err) => {
Log.error("couldn't receive data");
}
});
My Webstorm throws an error on fetch({}) that says Supplied Parameters are incorrect
I'm not able to find any other api specification for the fetch call. The code is in and the typescript definition for backbone I'm using is here:
https://github.com/borisyankov/DefinitelyTyped/blob/master/backbone/backbone.d.ts
UPDATE==
The result I see in the log is this:
Triple
Triple
Triple
Triple
Triple
Here "triple" is the value of the "question" attribute in the last model added to the collection. There are 5 models in the collection (in the persistent) database and if there were 6 it would display "Triple" 6 times. There is some problem in the API call I made to get the value of the question object
I'm probably not calling the function right. I need to know whats the appropriate call for getting the value of an attribute from the model. Or this could be a problem actually retrieving the values from the server.
I've tried the following to actually get the right value in the log:
Log.show(model.toJSON().question);
Log.show(model.toString());
Log.show(model.question);
The success callback should look like this:
success:function(collection, response){}

Resources