Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / swig_python / highgui / cvSetMouseCallback.py
1 #! /usr/bin/env python
2 """
3 This script will test highgui's mouse functionality
4 """
5
6 # name of this test and it's requirements
7 TESTNAME = "cvSetMouseCallback"
8 REQUIRED = ["cvShowImage"]
9
10  
11 # needed for sys.exit(int) and .works file handling
12 import os
13 import sys
14 import works
15
16 # check requirements and delete old flag file, if it exists
17 if not works.check_files(REQUIRED,TESTNAME):
18         sys.exit(77)
19
20 # import the necessary things for OpenCV
21 from highgui import *
22 from cv import *
23
24 # global variable which stores information about the pressed mousebuttons
25 mouse_events = [False,False,False,False,False,False,False,False,False,False]
26 event_detected = False
27
28 # some definitions
29 win_name = "testing..."
30 EVENTS = ['CV_EVENT_MOUSEMOVE', 'CV_EVENT_LBUTTONDOWN', 'CV_EVENT_RBUTTONDOWN',  'CV_EVENT_MBUTTONDOWN',  'CV_EVENT_LBUTTONUP',
31           'CV_EVENT_RBUTTONUP', 'CV_EVENT_MBUTTONUP'  , 'CV_EVENT_LBUTTONDBLCLK','CV_EVENT_RBUTTONDBLCLK','CV_EVENT_MBUTTONDBLCLK']
32
33
34 # our callback function, 5th parameter not used here.
35 def callback_function(event,x,y,flag,param):
36         globals()["event_detected"] = True
37         # check if event already occured; if not, output info about new event.
38         if globals()["mouse_events"][event] == False:
39                 print "Event "+globals()["EVENTS"][event]+" detected."
40                 globals()["mouse_events"][event] = True
41         return
42
43
44 # create a window ('cvNamedWindow.works' exists, so it must work)
45 cvNamedWindow(win_name,CV_WINDOW_AUTOSIZE)
46 # show the baboon in the window
47 PREFIX = os.path.join(os.environ["srcdir"],"../../opencv_extra/testdata/python/images/")
48 cvShowImage(win_name, cvLoadImage(PREFIX+"cvSetMouseCallback.jpg"))
49 # assign callback function 'callback_function' to window, no parameters used here
50 cvSetMouseCallback( win_name, callback_function )
51
52 # give the user information about the test and wait for input
53 print "(INFO) Please hover the mouse over the baboon image and press"
54 print "(INFO) your available mousebuttons inside the window to 'PASS' this test."
55 print "(INFO) You may also perform double-clicks."
56 print "(INFO) Press a key on your keyboard ot wait 20 seconds to continue."
57 print "(HINT) If no mouseevent was detected this test will be remarked as 'FAIL'."
58
59 # now wait 20 seconds for user to press a key
60 cvWaitKey(20000)
61
62 # reset mouse callback
63 cvSetMouseCallback( win_name, 0 )
64 # destroy the window
65 cvDestroyWindow( win_name )
66
67 # check if a mouse event had beed detected
68 if not event_detected:
69         # user didn't interact properly or mouse functionality doesn't work correctly
70         print "(ERROR) No mouse event detected."
71         sys.exit(1)
72
73 # create flag file for following tests
74 works.set_file(TESTNAME)
75
76 # return 0 (success)
77 sys.exit(0)