Move the sources to trunk
[opencv] / tests / 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 import python
22 from python.highgui import *
23 from python.cv import *
24
25 # global variable which stores information about the pressed mousebuttons
26 mouse_events = [False,False,False,False,False,False,False,False,False,False]
27 event_detected = False
28
29 # some definitions
30 win_name = "testing..."
31 EVENTS = ['CV_EVENT_MOUSEMOVE', 'CV_EVENT_LBUTTONDOWN', 'CV_EVENT_RBUTTONDOWN',  'CV_EVENT_MBUTTONDOWN',  'CV_EVENT_LBUTTONUP',
32           'CV_EVENT_RBUTTONUP', 'CV_EVENT_MBUTTONUP'  , 'CV_EVENT_LBUTTONDBLCLK','CV_EVENT_RBUTTONDBLCLK','CV_EVENT_MBUTTONDBLCLK']
33
34
35 # our callback function, 5th parameter not used here.
36 def callback_function(event,x,y,flag,param):
37         globals()["event_detected"] = True
38         # check if event already occured; if not, output info about new event.
39         if globals()["mouse_events"][event] == False:
40                 print "Event "+globals()["EVENTS"][event]+" detected."
41                 globals()["mouse_events"][event] = True
42         return
43
44
45 # create a window ('cvNamedWindow.works' exists, so it must work)
46 cvNamedWindow(win_name,CV_WINDOW_AUTOSIZE)
47 # show the baboon in the window
48 PREFIX = os.environ["top_srcdir"]+"/tests/python/testdata/images/"
49 cvShowImage(win_name, cvLoadImage(PREFIX+"cvSetMouseCallback.jpg"))
50 # assign callback function 'callback_function' to window, no parameters used here
51 cvSetMouseCallback( win_name, callback_function )
52
53 # give the user information about the test and wait for input
54 print "(INFO) Please hover the mouse over the baboon image and press"
55 print "(INFO) your available mousebuttons inside the window to 'PASS' this test."
56 print "(INFO) You may also perform double-clicks."
57 print "(INFO) Press a key on your keyboard ot wait 20 seconds to continue."
58 print "(HINT) If no mouseevent was detected this test will be remarked as 'FAIL'."
59
60 # now wait 20 seconds for user to press a key
61 cvWaitKey(20000)
62
63 # reset mouse callback
64 cvSetMouseCallback( win_name, 0 )
65 # destroy the window
66 cvDestroyWindow( win_name )
67
68 # check if a mouse event had beed detected
69 if not event_detected:
70         # user didn't interact properly or mouse functionality doesn't work correctly
71         print "(ERROR) No mouse event detected."
72         sys.exit(1)
73
74 # create flag file for following tests
75 works.set_file(TESTNAME)
76
77 # return 0 (success)
78 sys.exit(0)