I'm trying to develop a javascript code using opencv.js, I have python code with the same requirement, I converted many lines but some are very hard to find, please guide me.
last 3 lines from python code unable to find for javascript opencv.js.
def find_marker(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 35, 125)
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts) //these three line are unable to find for javascript.
c = max(cnts, key = cv2.contourArea)
return cv2.minAreaRect(c)
In the first line, the code is using the function in imutils python package. If you see the grab_contours method in imutils.convenience file located at https://github.com/jrosebr1/imutils/blob/master/imutils/convenience.py, you can see how it is implemented.
This is very simple to implement as a one liner in js.
cnts = (cnts.length == 2) ? cnts[0] : (cnts.length == 3) ? cnts[1] : cnts
In second line, max is the inbuilt function of python for iterating through an iterable and to find the maximum based on the key.
This same functionality can be achieved in js as follows
c = cnts.reduce(function(max, cur) {
// here key is the cv2.contourArea function,
// we apply that on the cnts[i] and finds the cnts[i]
// such that cv2.contourArea(cnts[i]) is maximum
if(cv2.contourArea(max) < cv2.countourArea(cur)) {
return cur
} else {
return max
}
});
Now for the third line I assume cv2.minAreaRect function is present in the js version too. I'm not sure though. But hope the above code works for you. Thank you.
I'm trying to debug some code that another programmer has left for me to maintain. I've just attempted to upgrade from node.js 5 to node.js 8 and my database queries are for some requests coming back with key not found errors
We're using couchbase for the database and our document keys are "encrypted" for security. So we may have a key that starts like this "User_myemail#gmail.com" but we encrypt it using the following method:
function _GetScrambledKey(dbKey)
{
//select encryption key based on db key content
var eKeyIndex = CalculateEncryptionKeyIndex(dbKey, eKeys.length);
var sha = CalculateSHA512(dbKey + eKeyIndex);
return sha;
}
function CalculateEncryptionKeyIndex(str, max)
{
var hashBuf = CalculateSHA1(str);
var count = 0;
for (var i = 0; i < hashBuf.length; i++)
{
count += hashBuf[i];
count = count % max;
}
return count;
}
We then query couchbase for the document with
cb.get("ECB_"+encryptedKey, opts, callback);
In node5 this worked but in node8 we're getting some documents return fine and others return as missing. I outputted the "ECB_"+encryptedKey as an int array and the results have only confused me more. They are different on node5 to node8 but only by one character right in the middle of the array.
Outputting the encryptedKey as an int array on both versions shows this
188,106,14,227,211,70,94,97,63,130,78,246,155,65,6,148,62,215,47,230,211,109,35,99,21,60,178,74,195,13,233,253,187,142,213,213,104,58,168,60,225,148,25,101,155,91,122,77,2,99,102,235,26,71,157,99,6,47,162,152,58,181,21,175
Then outputting the concatenated string, in the same way, shows slightly different results
This is the node8 output
Node8 key: 69,67,66,95,65533,106,14,65533,65533,70,94,97,63,65533,78,65533,65533,65,6,65533,62,65533,47,65533,65533,109,35,99,21,60,65533,74,65533,13,65533,65533,65533,65533,65533,65533,104,58,65533,60,65533,25,101,65533,91,122,77,2,99,102,65533,26,71,65533,99,6,47,65533,65533,58,65533,21,65533
And this is the node5 output
Node5 key: 69,67,66,95,65533,106,14,65533,65533,70,94,97,63,65533,78,65533,65533,65,6,65533,62,65533,47,65533,65533,109,35,99,21,60,65533,74,65533,13,65533,65533,65533,65533,65533,65533,104,58,65533,60,65533,65533,25,101,65533,91,122,77,2,99,102,65533,26,71,65533,99,6,47,65533,65533,58,65533,21,65533
I had to run it through a diff tool to see the difference
Comparing that to the original pre-append array it looks like the 225 has just been dropped in node8. Is 225 significant? I can't understand how that would be possible otherwise unless it's a bug. Does anyone have any ideas?
Looks like this was a change in v8 5.5 https://github.com/nodejs/node/issues/21278
A lot of the issues you are facing, including the concatenation can be cleaned up using newer features from ES6 that are available in node 8.
In general, you should avoid doing string concatenations with the + operator and should use string literals instead. In your case, you should replace the "ECB_"+encryptedKey with `ECB_${encryptedKey}`.
Additionally, if you want to output the contents of the integers values from this concatenated string, then you are better off using .join, the spread operator (...) and the Buffer class from Node as follows:
let encKey = `ECB_${encryptedKey}`;
let tmpBuff = Buffer.from(encKey);
let buffArrVals = [...tmpBuff];
console.log(buffArrVals.join(','));
Also, if you can help it, you really should avoid using var inside of function blocks like it exists in your sample code. var performs something called variable hoisting and causes the variable to become available outside the scope it was declared, which is seldom intended. From node 6+ onward the recommendation is to use let or const for variable declarations to ensure they stay scoped to the block they are declared.
Let's say I have the following JS on my page (compare is a simple comparator function not relevant to the question):
function sortArray(a) {
a.sort(compare);
}
function sortJQuerySet(b) {
b.sort(compare);
}
$(document).ready(function(){
var a = [], b = [], i = 0, n = 1000;
for(i=0; i<n; ++i) {
a.push($('<div>' + i.toString() + '</div>'));
b.push($('<div>' + i.toString() + '</div>'));
}
b = $(b);
$('#runner').click(function(){
sortArray(a);
sortJQuerySet(b);
});
});
As you can see, a and b are essentially the same array, the only difference is that b is turned into jQuery set. I'm trying to sort both of those arrays and to profile the sorting. Please note that the number of elements in both arrays is 1000.
Here is the result of profiling of sorting for both containers in Safari:
Safari makes about half a million comparisons on jQuery set with 1000 elements. This looks much more like quadratic sort than like O(n log n) sort. In a meantime, sorting the native array is just fine.
Sorting in Chrome browser works in about equal time for both container types.
P.S. I used Safari 6.0.4, jQuery 1.7.1 and jQuery 1.10.1.
Code: https://gist.github.com/ikostia/5925715
Perhaps this discussion regarding WebKit engine could shed some light.
It appears to me from the line:
631 if (thisObj->classInfo() == &JSArray::s_info && !asArray(thisObj)->inSparseMode()) { that only non-arrays, or arrays
in some kind of "sparse" mode are sorted inefficiently. I'm not sure
what sparse mode is, but let's try my Raymond Chen inspired psychic
powers: if you assign a[0]=1 and a[1000000]=2, you don't want a 1 to
999999 to be stored, so an array like that will end up in a sparse
mode which functions more like a hash table keyed by integers. The
same applies to non-arrays...
(the source code discussed in the aforementioned citation is here)
So let's now take a look at the jQuery source code.
Calling $(someArrayOrSelectorString) results in invoking the init method (Line 43).
The init method returns jQuery.makeArray( selector, this ), where this is an instance of jQuery (please, correct me if I'm wrong).
And, finally, inside makeArray method the jQuery.merge (line 600) is invoked, which will populate the jQuery instance with the passed array elements.
So it looks like wrapping an array results in an array-like object (jQuery instance), but not a real array. And Safari treats this object like a sparse one.
(You might be interested in this answer and comments. That's where I've borrowed the idea for my answer)