cv2.groupRectangles() returning empty tuple - Python - python-3.x

I'm trying to group rectangles together using cv2.groupRectangles() but it returns empty tuple ()
I tried using 2 different versions of opencv-python (4.6.0.66 and 3.4.18.65)
‎This is the code:
# "do" object detection
rectangles = np.array([
[530, 119, 47, 47],
[641, 117, 53, 53],
[531, 89, 117, 117]])
print(rectangles)
print()
(rect, weights) = cv2.groupRectangles(rectangles, groupThreshold=1, eps=0.5)
print("rect after cv.groupRectangles:", rect)
And this is the output:
[[530 119 47 47]
[641 117 53 53]
[531 89 117 117]]
rect after cv.groupRectangles: ()

Here are your initial rectangles for reference. How were you wanting those to be merged?
You might have chosen groupThreshold badly.
If that is more than 0, the function will remove any rectangles that aren't "confirmed" by nearby neighbors. With 0, it's allowed to leave single rectangles alone...
>>> cv.groupRectangles(rectangles, groupThreshold=0, eps=0)
(array([[530, 119, 47, 47],
[641, 117, 53, 53],
[531, 89, 117, 117]]), array([1, 1, 1]))
But then it also doesn't seem to want to merge any of them. That eps doesn't seem to behave all that "relatively". I get effects when I increase it beyond 1.0.
Documentation is very poor about this. Perhaps this function is broken. If not broken, then it's at least not documented well enough for me to make sense of it and I think I'm proficient with OpenCV.

Related

Excel - how to select the top 10 duplicates only if 10 or more are present

I have a 500k line excel like the sample below:
ref, RSRQ, Signal, SNR, TestDate
ST782347, -13, -116, 40, 01/04/2020
ST782347, -9, -111, 110, 22/02/2020
ST782347, -10, -115, 70, 22/02/2020
ST782347, -9, -111, 110, 22/02/2020
ST782349, -10, -110, 90, 22/02/2020
ST782349, -10, -114, 50, 22/02/2020
Now the "ref" can have 1 to 800 equal values, but I only want the most recent 10 if it has 10 or more, otherwise should be discarded. I have been stuck here for a couple of days now, any help will be extremely appreciated.
I am assuming that you want to do this process once only?
order by date, the most recent up (you should be able to use the filter on this,
maybe your data is already ordered?)
run a count on ref
mark and filter results below 10
see image for steps 2 and 3
Hope this helps!
PS: always helps to give a sample of your data in Excel format, so that we don't have to reproduce it... cheers

How to pass a function argument as a key in a dictionary in Python

I am creating custom functions in which a user can specify an argument value in the custom function. The argument will be a key in a dictionary that is local to the custom function.The value associated with the key will an argument to an internal function that is nested within the custom function.
I have attached a simple example where the custom function is called Average. If I can get help on this simple example, I can use the logic to solve the more complex examples that I am working with.
# Python program to get average of a list
def Average(lst,rnd = 'three'):
rndtype = {'three':3,'four':4}
return round(sum(lst) / len(lst),rndtype[rnd].values())
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst,'three')
The error I get is the following:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-a7c034956fd8> in <module>
----> 1 average = Average(lst,'three')
<ipython-input-6-b1d1ab69b2a6> in Average(lst, rnd)
2 def Average(lst,rnd = 'three'):
3 rndtype = {'three':3,'four':4}
----> 4 return round(sum(lst) / len(lst),rndtype[rnd].values())
AttributeError: 'int' object has no attribute 'values'
The values() function applies to a dictionary, you are applying it to an element of the dictionary. You just have to remove the values() call from rndtype[rnd].
def Average(lst,rnd = 'three'):
rndtype = {'three':3,'four':4}
return round(sum(lst) / len(lst),rndtype[rnd])
lst = [15, 9, 55, 41, 35, 20, 62, 49]
average = Average(lst,'three')
A little confused on what you are trying to do but is this along the lines of what you are looking for? This should be right. In your parameters you should pass in just the lst and the rnd value.
# Python program to get average of a list
def Average(lst,rnd):
rndtype = {'three':3,'four':4}
return round(sum(lst)/len(lst), rndtype[rnd])
lst = [15, 10, 55, 41, 35, 20, 62, 49]
average = Average(lst,'three')

why is my second to last number not visible in output?

When running
>>> a = np.linspace(0, 330, 330, 1, dtype=int)
>>> print(a)
[ 0, 1, 2, ..[skipped for readability].. 323, 324, 325, 326, 327, 328, 330])], dtype=int
I expect the second last number to be 329 instead of 328. Why is this not the case? It's probably because that number in a float will be 328.99696049 but I do wonder how I can include it into my output, and if it does matter for my data purity when I do calculations on that number.
Yes, your assumption is correct. np.linspace distributes 330 values between 0 and 330 which means the stepsize between two neighboring values is (end - start) / (steps - 1) = 330 / 329. Since you coerce to int, the decimal part is truncated.
If you would like a stepsize of 1 continously, you need 331 steps:
a = np.linspace(0, 330, 331, 1, dtype=int)
Of course it's even simpler to get the same result using np.arange:
a = np.arange(331)

Python Deap GP Evaluating individual causes error

I am currently experiencing an issue whenever I try to evaluate an individual using the GP portion of DEAP.
I receive the following error:
Traceback (most recent call last):
File "ImageGP.py", line 297, in <module>
pop, logs = algorithms.eaSimple(pop, toolbox, 0.9, 0.1, 60, stats=mstats, halloffame=hof, verbose=True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/deap/algorithms.py", line 148, in eaSimple
for ind, fit in zip(invalid_ind, fitnesses):
File "ImageGP.py", line 229, in evalFunc
func = toolbox.compile(expr=individual)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/deap/gp.py", line 451, in compile
return eval(code, pset.context, {})
File "<string>", line 1
lambda oValue,oAvg13,oAvg17,oAvg21,sobelVal(v),sobelVal(h),edgeVal,blotchVal: [[[0, 75, 82.2857142857, 83.0, 82.9090909091, 4, 12, 4, 180], ... Proceed to print out all of my data ... [0, 147, 151.244897959, 150.728395062, 150.73553719, 248, 244, 5, 210]]]
^
SyntaxError: invalid syntax
If anyone has any ideas about what could be causing this problem, then I would really appreciate some advice. My current evaluation function looks like this:
def evalFunc(individual, data, points):
func = toolbox.compile(expr=individual)
total = 1.0
for point in points:
tmp = [float(x) for x in data[point[1]][point[0]][1:9]]
total += int((0 if (func(*tmp)) < 0 else 1) == points[2])
print ("Fitness: " + str(total))
return total,
Where the data contains the data being used (the values for the 8 variables listed in the error) and point specifying the x and y co-ordinates from which to get those 8 values. Thank you for your suggestions!

Can't read integers value in Cassandra

I have a simple column family 'Users'
System.out.println(" read User");
long timestamp = System.currentTimeMillis();
clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"),
new Column(ByteBuffer.wrap("email".getBytes())).setValue(ByteBuffer.wrap("mike#gmail.com".getBytes())).setTimestamp(timestamp)
, ConsistencyLevel.ONE);
clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"),
new Column(ByteBuffer.wrap("totalPosts".getBytes())).setValue(ByteBuffer.allocate(4).putInt(27).array()).setTimestamp(timestamp)
, ConsistencyLevel.ONE);
SlicePredicate predicate = new SlicePredicate();
predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 10));
ColumnParent parent = new ColumnParent("Users");
List<ColumnOrSuperColumn> results = clientA.get_slice(ByteBuffer.wrap("mike".getBytes()),
parent,
predicate,
ConsistencyLevel.ONE);
for (ColumnOrSuperColumn result : results) {
Column column = result.column;
System.out.println(new String(column.getName()) + " -> "+ new String(column.getValue())+", "+column.getValue().length);
}
it returns
read User
email -> mike#gmail.com, 14
totalPosts ->
So totalPosts can't be read with the thrift client
with cassandra-cli
[default#test] get Users['mike']['totalPosts'];
=> (column=totalPosts, value=0000001b, timestamp=1336493080621)
Elapsed time: 10 msec(s).
How can I retrieve this Integer value with Java thrift client?
using cassandra 1.1
edit:
it seems due to this part
for (ColumnOrSuperColumn result : results) {
Column column = result.column;
System.out.println(new String(column.getName()) + " -> "+Arrays.toString(column.getValue()));
}
returns
email -> [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109]
totalPosts -> [0, 0, 0, 27]
Your column values are being returned as bytes, the same way you're inserting them. So the value for the 'email' column is [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109], which interpreted as ascii is mike#gmail.com. And the number 27 which you write into the ByteBuffer as [0, 0, 0, 27] (via the putint call) comes out the same way.
If you absolutely have to be using the raw thrift interface, you'll probably want to retrieve your totalPosts int using ByteBuffer.getInt(). But if at all possible, I recommend using a library to wrap around the ugliness of the thrift interface, which should take care of value serialization issues like this. Maybe look at Hector, or skip the old interface entirely and go straight to CQL with Cassandra-JDBC.

Resources