conta-bolas
conta-bolas copied to clipboard
Ball calibration?
I tried this myself, and i had a hard time getting the lighting right, and tweaking the red color so that it would recognize my ball. Could we have a mode where the program draws a square on the screen, and you hold your red (or whatever color) ball in the square, then click a button to say, "This is it, keep your eye on it."
It's a good idea. I've been thinking about ways to do this. I've used a BGR color scheme (the same as RGB, but with blue coming first). This guy uses HSV: https://pythonprogramming.net/color-filter-python-opencv-tutorial/ It's another thing to try out. For some reason, my setup worked better in BGR.
I've created a branch with a tkinter calibrator. You press c while count_throws.py is running and fine tune the color settings. When you think you're done, you just close tkinter window and it gets back to the main program with the new settings. If you don't get it right, you can press c and try again. It's not ideal, but it's better than change the code until you get the settings right. Let's keep working on this.
@ocarneiro thanks for adding it! I tried the branch, and it would not run on my Mac. I get:
Warning, camera failed to properly initialize!
Cleaned up camera.
OpenCV Error: Sizes of input arguments do not match (The lower bounary is neither an array of the same size and same type as src, nor a scalar) in inRange, file /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/core/src/arithm.cpp, line 2703
Traceback (most recent call last):
File "count_throws.py", line 59, in <module>
mask = cv2.inRange(im, min_target_color, max_target_color)
cv2.error: /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/core/src/arithm.cpp:2703: error: (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange
I tried changing the 1 to a 0 in this line:
capture = cv2.VideoCapture(1)
which made the camera work, but then when I pressed 'c' for calibration, I got a hard crash:
[ 0 0 80]
[ 30 30 255]
2016-02-06 18:32:33.528 python[41400:3597463] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fe94bc8f170
2016-02-06 18:32:33.534 python[41400:3597463] An uncaught exception was raised
2016-02-06 18:32:33.534 python[41400:3597463] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fe94bc8f170
2016-02-06 18:32:33.534 python[41400:3597463] (
0 CoreFoundation 0x00007fff8fceb03c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff89c7f76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8fcee0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff8fc33e24 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff8fc33998 _CF_forwarding_prep_0 + 120
5 Tk 0x000000010462a527 TkpInit + 476
6 Tk 0x00000001045a4aca Tk_Init + 1788
7 _tkinter.so 0x0000000104480ba2 Tcl_AppInit + 82
8 _tkinter.so 0x000000010447b7b0 Tkinter_Create + 1120
9 python 0x0000000102614fbd PyEval_EvalFrameEx + 19597
10 python 0x000000010260fed2 PyEval_EvalCodeEx + 1666
11 python 0x000000010259961c function_call + 364
12 python 0x0000000102573843 PyObject_Call + 99
13 python 0x0000000102580ae7 instancemethod_call + 247
14 python 0x0000000102573843 PyObject_Call + 99
15 python 0x000000010261704d PyEval_CallObjectWithKeywords + 93
16 python 0x000000010257ea9d PyInstance_New + 141
17 python 0x0000000102573843 PyObject_Call + 99
18 python 0x0000000102614bb7 PyEval_EvalFrameEx + 18567
19 python 0x0000000102617936 fast_function + 342
20 python 0x0000000102613a1f PyEval_EvalFrameEx + 14063
21 python 0x000000010260fed2 PyEval_EvalCodeEx + 1666
22 python 0x000000010260f846 PyEval_EvalCode + 54
23 python 0x00000001026392c4 PyRun_FileExFlags + 164
24 python 0x0000000102638de7 PyRun_SimpleFileExFlags + 711
25 python 0x000000010264ed1c Py_Main + 3084
26 libdyld.dylib 0x00007fff88b315c9 start + 1
)
2016-02-06 18:32:33.535 python[41400:3597463] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fe94bc8f170'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8fceb03c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff89c7f76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8fcee0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff8fc33e24 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff8fc33998 _CF_forwarding_prep_0 + 120
5 Tk 0x000000010462a527 TkpInit + 476
6 Tk 0x00000001045a4aca Tk_Init + 1788
7 _tkinter.so 0x0000000104480ba2 Tcl_AppInit + 82
8 _tkinter.so 0x000000010447b7b0 Tkinter_Create + 1120
9 python 0x0000000102614fbd PyEval_EvalFrameEx + 19597
10 python 0x000000010260fed2 PyEval_EvalCodeEx + 1666
11 python 0x000000010259961c function_call + 364
12 python 0x0000000102573843 PyObject_Call + 99
13 python 0x0000000102580ae7 instancemethod_call + 247
14 python 0x0000000102573843 PyObject_Call + 99
15 python 0x000000010261704d PyEval_CallObjectWithKeywords + 93
16 python 0x000000010257ea9d PyInstance_New + 141
17 python 0x0000000102573843 PyObject_Call + 99
18 python 0x0000000102614bb7 PyEval_EvalFrameEx + 18567
19 python 0x0000000102617936 fast_function + 342
20 python 0x0000000102613a1f PyEval_EvalFrameEx + 14063
21 python 0x000000010260fed2 PyEval_EvalCodeEx + 1666
22 python 0x000000010260f846 PyEval_EvalCode + 54
23 python 0x00000001026392c4 PyRun_FileExFlags + 164
24 python 0x0000000102638de7 PyRun_SimpleFileExFlags + 711
25 python 0x000000010264ed1c Py_Main + 3084
26 libdyld.dylib 0x00007fff88b315c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
Hmm... Looks like tkinter isn't a viable choice as calibrator gui for you...
A friend of mine suggested a machine learning approach to the problem and I may try different things in the near future.
Maybe you should abandon the branch all together and try changing the color settings manually, for now. A pure (vivid) red as max color may work, changing line 37 on the master branch to this:
max_target_color = np.array([0, 0, 255]) # was 30,30,255
In a bright environment, it should catch the red ball more often.
Thank you so much for trying. Your feedback will help me make it easier for others to get good results.
I did a video showing some calibration tests: https://youtu.be/HAS1nLJqFGM I also added a debug mode in the calibrator branch so that the contours generated by open cv are visible. I was wrong about the settings I suggested earlier. It's better to go with something like: min: [0,0,34] max: [30,30,255] That is, to reduce the amount of red in the min instead of reducing the other colors in the max value.